-
-
Save seanparsons/5106957 to your computer and use it in GitHub Desktop.
Cunning use of monoids with Function1.
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
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