Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def sideEffecting(): Map[String, List[String]] = { println("bazinga"); Map("a" -> List("a", "b")) }
- //doesn't print anything here
- val x1: String => List[String] = sideEffecting().getOrElse(_, Nil)
- //but it'll print bazinga any time you call one of these:
- x1("a") //here
- x1("a") //also here
- x1("b") //here too
- //prints bazinga exactly once, here
- val x2: String => List[String] = { val m = sideEffecting(); m.getOrElse(_, Nil) }
- x2("a") //doesn't run side effect
- x2("a") //neither does this
Add Comment
Please, Sign In to add comment