Created
November 29, 2016 09:42
-
-
Save hejfelix/bf16c79c5a6b3cd39c3aee50cfb7367d 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 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