2013-02-13

juan_gandhi: (VP)
2013-02-13 11:26 am
Entry tags:

is there anything like this already?

  implicit def f2tof1[X,Y,Z](f2: Function2[X,Y,Z]) = (xy:(X,Y)) => f2(xy._1, xy._2)


Update: f2.tupled
juan_gandhi: (VP)
2013-02-13 01:01 pm

stuffchik

object Functions {
  implicit def nameIt[From, To](f: From => To) = new {
    def named(name: String) = new Function[From, To](name, f)
  }
}
//...
  class ValidatingFunction[X, Y](name: String, f: X => Y) extends CheckThis[X, Y] {

    private def expectMatch[X, Y](f: X => Y)(x: X, y: Y) {
      f(x) aka (name + "(" + x + ")=" + y) must_== y
    }

    def maps(samples:(X, Y)*) {
      check(expectMatch(f), samples:_*)
    }
  }

  implicit def funValidates[X, Y](f: Function[X, Y]) = new ValidatingFunction[X, Y](f.name, f)

//... and in the tests:
  "SomeWeirdFunction()" should {
    "do its job properly" in {
      (SomeWeirdFunction _) named "SomeWeirdFunction" maps (
        "1234500000" -> "1234500000",
        "012"        -> "12",
        "0000000440" -> "440",
        "00000000"   -> "",
        ""           -> "",
        "the end"    -> "the end")
    }
  }