Last active
December 13, 2015 18:29
-
-
Save sam/4956177 to your computer and use it in GitHub Desktop.
Fold is awesome.
This file contains hidden or 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
val things = Seq(1,2,3,4,5,6).map(Some(_)) ++ Seq(None, None) | |
for(thing <- things) | |
yield thing.fold { | |
println("Bummer Dude") | |
} { value => | |
value % 2 | |
println("whatever man") | |
println(""" | |
See? Long things can happen in the "success" scenario. | |
So having the error scenario first means that you're | |
both forced to handle the error (you don't have the | |
option not to), and also that your code is structured | |
nicely because this order is generally just good style | |
and how you'd expect to see something like an if() | |
statement structured. | |
I don't know about you, but I hate it when someone writes: | |
if some_condition? | |
lots | |
of | |
success | |
code | |
goes | |
here | |
else | |
handle_the_error_in_a_one_liner_at_the_bottom_here | |
end | |
""") | |
value * 35 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment