Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.58 KB | None | 0 0
  1. println("hello there")                                                          //hello there
  2.                                                                                 //res0: Unit = ()
  3. // Int => Int lambda                                                            //
  4. val cube = (i: Int) => i * i * i                                                //
  5.                                                                                 //cube: Int => Int = <function1>
  6. // `def` with curried params                                                    //
  7. def mapRangeSumCurried(f: Int => Int)(start: Int, end: Int) = {                 //
  8.   start to end map f sum                                                        //mapRangeSumCurried: mapRangeSumCurried[](val f: Int => Int)(val start: Int,val end: Int) => Int
  9. }                                                                               //
  10.                                                                                 //
  11. var result = mapRangeSumCurried(cube)(1, 10)                                    //
  12.                                                                                 //result: Int = 3025
  13. // `def` function without currying params                                       //
  14. def mapRangeSumNonCurried(f: Int => Int) : (Int, Int) => Int = {                //
  15.   (start, end) => {                                                             //mapRangeSumNonCurried: mapRangeSumNonCurried[](val f: Int => Int) => (Int, Int) => Int
  16.     start to end map f sum                                                      //
  17.   }                                                                             //
  18. }                                                                               //
  19.                                                                                 //
  20. assert(result == mapRangeSumNonCurried(cube)(1, 10))                            //
  21. result = mapRangeSumNonCurried(cube)(1, 10)                                     //res1: Unit = ()
  22.                                                                                 //result: Int = 3025
  23. // `val` function with curried params                                           //
  24. val mapRangeSumCurried1 = (f: Int => Int) => (start: Int, end: Int)  => {       //
  25.   start to end map f sum                                                        //mapRangeSumCurried1: (Int => Int) => ((Int, Int) => Int) = <function1>
  26. }                                                                               //
  27. assert(result == mapRangeSumCurried1(cube)(1, 10))                              //
  28. result = mapRangeSumCurried1(cube)(1, 10)                                       //res2: Unit = ()
  29.                                                                                 //result: Int = 3025
  30. // `val` function without curried params                                        //
  31. val mapRangeSumNonCurried1 = (f: Int => Int) => {                               //
  32.   (start: Int, end: Int) => {                                                   //mapRangeSumNonCurried1: (Int => Int) => ((Int, Int) => Int) = <function1>
  33.     (start to end).map(x => f(x)).sum                                           //
  34.   }                                                                             //
  35. }                                                                               //
  36. assert(result == mapRangeSumNonCurried1(cube)(1, 10))                           //
  37.                                                                                 //res3: Unit = ()
  38.                                                                                 //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement