Skip to content

Instantly share code, notes, and snippets.

@seanparsons
Forked from channingwalton/FirstSome.scala
Last active December 14, 2015 15:19
Show Gist options
  • Save seanparsons/5106957 to your computer and use it in GitHub Desktop.
Save seanparsons/5106957 to your computer and use it in GitHub Desktop.
Cunning use of monoids with Function1.
scala> import scalaz._,Scalaz._
import scalaz._
import Scalaz._
scala> val isOne = (i: Int) => {println("isOne"); if (i == 1) Some(1) else None}
isOne: Int => Option[Int] = <function1>
scala> val isTwo = (i: Int) => {println("isTwo"); if (i == 2) Some(2) else None}
isTwo: Int => Option[Int] = <function1>
scala> List(isOne, isTwo).foldMap(check => check(2).first)
isOne
isTwo
res2: scalaz.package.@@[Option[Int],scalaz.Tags.First] = Some(2)
scala> List(isOne, isTwo).foldMap(check => check(1).first)
isOne
res3: scalaz.package.@@[Option[Int],scalaz.Tags.First] = Some(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment