Skip to content

Instantly share code, notes, and snippets.

@channingwalton
Last active December 14, 2015 14:58
Show Gist options
  • Save channingwalton/5104053 to your computer and use it in GitHub Desktop.
Save channingwalton/5104053 to your computer and use it in GitHub Desktop.
Iterate a list of functions A => Option[B] returning the first Some
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)
}
@bmjames
Copy link

bmjames commented Mar 7, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment