Guest User

Untitled

a guest
May 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. def sideEffecting(): Map[String, List[String]] = { println("bazinga"); Map("a" -> List("a", "b")) }
  2.  
  3. //doesn't print anything here
  4. val x1: String => List[String] = sideEffecting().getOrElse(_, Nil)
  5.  
  6. //but it'll print bazinga any time you call one of these:
  7. x1("a") //here
  8. x1("a") //also here
  9. x1("b") //here too
  10.  
  11. //prints bazinga exactly once, here
  12. val x2: String => List[String] = { val m = sideEffecting(); m.getOrElse(_, Nil) }
  13.  
  14. x2("a") //doesn't run side effect
  15. x2("a") //neither does this
Add Comment
Please, Sign In to add comment