Skip to content

Instantly share code, notes, and snippets.

@hejfelix
Created November 29, 2016 09:42
Show Gist options
  • Save hejfelix/bf16c79c5a6b3cd39c3aee50cfb7367d to your computer and use it in GitHub Desktop.
Save hejfelix/bf16c79c5a6b3cd39c3aee50cfb7367d to your computer and use it in GitHub Desktop.
def mySideEffect(i:Int ):Boolean = {
try {
//do something with i
if (scala.util.Random.nextBoolean) {
throw new Exception("Ooops")
}
} catch {
case _: Throwable => return false
}
return true
}
def yourSideEffect(i:Int ):Unit = {
try {
//do something with i
if (scala.util.Random.nextBoolean) {
throw new Exception("Ooops")
}
} catch {
case _: Throwable => println("Something failed, good luck fixing it")
}
}
println( (1 to 10 map mySideEffect) )
println( (1 to 10 map yourSideEffect) )
/*
output:
Desktop → scala UnitVSBoolean.scala
Vector(true, true, true, false, false, true, true, false, false, false)
Something failed, good luck fixing it
Something failed, good luck fixing it
Something failed, good luck fixing it
Something failed, good luck fixing it
Something failed, good luck fixing it
Vector((), (), (), (), (), (), (), (), (), ())
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment