Skip to content

Instantly share code, notes, and snippets.

@kubukoz
Last active May 21, 2018 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubukoz/833b4a65f06a04c5da01b2b1d5481eec to your computer and use it in GitHub Desktop.
Save kubukoz/833b4a65f06a04c5da01b2b1d5481eec to your computer and use it in GitHub Desktop.
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