Skip to content

Instantly share code, notes, and snippets.

@fancellu
Last active August 15, 2022 14:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fancellu/052b306e3f3b66c26ef44d4b374d6fd9 to your computer and use it in GitHub Desktop.
Save fancellu/052b306e3f3b66c26ef44d4b374d6fd9 to your computer and use it in GitHub Desktop.
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