Created
July 29, 2017 16:04
-
-
Save knightpop/03e5eb94851939455ae238fdda0f677f to your computer and use it in GitHub Desktop.
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
def getSome(a: Int): Option[Int] = Some(a) | |
def getNone(a: Int): Option[Int] = None | |
def add(a: Int, b: Int): Int = a + b | |
val aOpt = getSome(1) | |
val bOpt = getSome(2) | |
aOpt.flatMap(a => bOpt.map(b => add(a, b))) | |
import cats.instances.option._ | |
import cats.Applicative | |
Applicative[Option].map2(aOpt, bOpt)(add) | |
import cats.syntax.all._ | |
(aOpt |@| bOpt).map(add) | |
val ints = List(1,2,3,4,5) | |
ints.map(getSome) | |
import cats.Traverse | |
import cats.instances.list._ | |
import cats.instances.option._ | |
Traverse[List].traverse(ints)(getSome) | |
def getSomeOrNone(a: Int): Option[Int] = | |
if(a % 2 == 0) Some(a) | |
else None | |
Traverse[List].traverse(ints)(getSomeOrNone) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment