Last active
December 14, 2015 14:58
-
-
Save channingwalton/5104053 to your computer and use it in GitHub Desktop.
Iterate a list of functions A => Option[B] returning the first Some
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
object play { | |
import scalaz._ | |
import Scalaz._ | |
def foo(i: Int) = if (i > 0) Some(i) else None | |
def boo(i: Int) = if (i <= 0) Some(0) else None | |
val l = foo _ :: boo _ :: Nil | |
l.foldMap(_.map(_.first)) apply -1 | |
//> res0: scalaz.@@[Option[Int],scalaz.Tags.First] = Some(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nicely done. Purely to demonstrate that a new function is being produced (using the derived monoid as explained by Rahul), I might assign the result of the
foldMap
to a value and then apply it with normal parens syntax.