Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- println("hello there") //hello there
- //res0: Unit = ()
- // Int => Int lambda //
- val cube = (i: Int) => i * i * i //
- //cube: Int => Int = <function1>
- // `def` with curried params //
- def mapRangeSumCurried(f: Int => Int)(start: Int, end: Int) = { //
- start to end map f sum //mapRangeSumCurried: mapRangeSumCurried[](val f: Int => Int)(val start: Int,val end: Int) => Int
- } //
- //
- var result = mapRangeSumCurried(cube)(1, 10) //
- //result: Int = 3025
- // `def` function without currying params //
- def mapRangeSumNonCurried(f: Int => Int) : (Int, Int) => Int = { //
- (start, end) => { //mapRangeSumNonCurried: mapRangeSumNonCurried[](val f: Int => Int) => (Int, Int) => Int
- start to end map f sum //
- } //
- } //
- //
- assert(result == mapRangeSumNonCurried(cube)(1, 10)) //
- result = mapRangeSumNonCurried(cube)(1, 10) //res1: Unit = ()
- //result: Int = 3025
- // `val` function with curried params //
- val mapRangeSumCurried1 = (f: Int => Int) => (start: Int, end: Int) => { //
- start to end map f sum //mapRangeSumCurried1: (Int => Int) => ((Int, Int) => Int) = <function1>
- } //
- assert(result == mapRangeSumCurried1(cube)(1, 10)) //
- result = mapRangeSumCurried1(cube)(1, 10) //res2: Unit = ()
- //result: Int = 3025
- // `val` function without curried params //
- val mapRangeSumNonCurried1 = (f: Int => Int) => { //
- (start: Int, end: Int) => { //mapRangeSumNonCurried1: (Int => Int) => ((Int, Int) => Int) = <function1>
- (start to end).map(x => f(x)).sum //
- } //
- } //
- assert(result == mapRangeSumNonCurried1(cube)(1, 10)) //
- //res3: Unit = ()
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement