Feb. 13th, 2013

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


Update: f2.tupled

stuffchik

Feb. 13th, 2013 01:01 pm
juan_gandhi: (VP)
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")
    }
  }

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

August 2025

S M T W T F S
      12
3456789
10 11 12 13141516
171819 20212223
24252627282930
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 23rd, 2025 10:18 pm
Powered by Dreamwidth Studios