Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Enables (2)-Tuples to apply themselves to a function
- implicit class Tuple2Apply[T1,T2](t: (T1,T2)) {
- def apply[R](f: (T1,T2) => R) = f(t._1, t._2)
- }
- // a demo function
- def perimeter(width: Int, height: Int) = 2*width + 2*height
- // some demo tuples
- val tuples = println((1 to 5).zipWithIndex
- // ––– demo time –––
- // .tupled requires function types to be known and
- // makes it impossible to apply a function without
- // "var =>" or partial function syntax
- tuples.map(t => ((_:Int) + (_:Int)).tupled(t))
- // Shorter, but still a little verbose
- tuples.map(t => perimeter(t._1, t._2))
- // Again, a lot shorter :)
- tuples.map(_(perimeter))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement