Last active
May 21, 2018 22:05
-
-
Save kubukoz/833b4a65f06a04c5da01b2b1d5481eec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment