Advertisement
Guest User

Untitled

a guest
Sep 28th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Enables (2)-Tuples to apply themselves to a function
  2. implicit class Tuple2Apply[T1,T2](t: (T1,T2)) {
  3.   def apply[R](f: (T1,T2) => R) = f(t._1, t._2)
  4. }
  5.  
  6. // a demo function
  7. def perimeter(width: Int, height: Int) = 2*width + 2*height
  8.  
  9. // some demo tuples
  10. val tuples = println((1 to 5).zipWithIndex
  11.  
  12. // ––– demo time –––
  13.  
  14. // .tupled requires function types to be known and
  15. // makes it impossible to apply a function without
  16. // "var =>" or partial function syntax
  17.  
  18. tuples.map(t => ((_:Int) + (_:Int)).tupled(t))
  19.  
  20. // Shorter, but still a little verbose
  21. tuples.map(t => perimeter(t._1, t._2))
  22.  
  23. // Again, a lot shorter :)
  24. tuples.map(_(perimeter))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement