Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created June 27, 2019 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fancellu/eeffac072f79b5fed0a4d3370af9b6ed to your computer and use it in GitHub Desktop.
Save fancellu/eeffac072f79b5fed0a4d3370af9b6ed to your computer and use it in GitHub Desktop.
Simple example of Cats OptionT monad transformer usage vs plain Scala
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