wavec022

returning functions

Oct 26th, 2020 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. Returning functions from functions?
  2.  
  3. "currying"
  4. (Int, Int) => Int // uncurried
  5.  
  6. def add(x,y): Int = x+y
  7.  
  8. Int => (Int => (Int => Int)) // curried
  9.  
  10. val add = x => (y => x+y)
  11.  
  12. list.map(add(1))
  13. list.map(1+_)
  14.  
  15. matrix.map(_.map(1+_))
  16. map(map(add(1)))(matrix)
  17. map(f)(list)
  18. map(list)(f)
  19.  
  20. ===
  21.  
  22. "staged computation"
  23.  
  24. def search(word, text)
  25.  
  26. def search(word): String => Boolean = {
  27. ...preprocess word...
  28. text => ...use the processed word...
  29. }
  30.  
  31. def search2(text) = {
  32. ...preprocess text...
  33. word => ...use the processed text...
  34. }
  35.  
  36. ===
  37.  
  38. build big things by combining smaller things
Add Comment
Please, Sign In to add comment