Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example usage of Cats FlatMap
import cats._
import cats.implicits._
// Example usage of Cats Flatmap
object CatsFlatmap extends App {
val listFlatMap=FlatMap[List]
val li=List(1,2,3)
println(listFlatMap.flatten(List(li)))
println(listFlatMap.flatMap(li)(i=>List(i+100)))
println(listFlatMap.mproduct(li)(i=>List(i+100)))
val boolList=listFlatMap.flatMap(li)(i=>List(i>2))
println(boolList)
val strings=listFlatMap.ifM(boolList)(List("true!","really"), List("false!"))
println(strings)
val li2=List(10,15,20)
println(listFlatMap.map2(li,li2)(_+_))
}
List(1, 2, 3)
List(101, 102, 103)
List((1,101), (2,102), (3,103))
List(false, false, true)
List(false!, false!, true!, really)
List(11, 16, 21, 12, 17, 22, 13, 18, 23)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment