Created
June 27, 2019 15:59
-
-
Save fancellu/eeffac072f79b5fed0a4d3370af9b6ed to your computer and use it in GitHub Desktop.
Simple example of Cats OptionT monad transformer usage vs plain Scala
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
import cats.data._ | |
import cats.implicits._ | |
val li=List(Option(1),Option(2), None) | |
println(OptionT(li).map(_+10).value) | |
//List(Some(11), Some(12), None) | |
println(li.map(_.map(_+10))) | |
//List(Some(11), Some(12), None) | |
println(OptionT(li).foldLeft(0)(_+_)) | |
//3 | |
println(li.foldLeft(0){_+_.getOrElse(0)}) | |
//3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment