Last active
August 15, 2022 14:45
-
-
Save fancellu/052b306e3f3b66c26ef44d4b374d6fd9 to your computer and use it in GitHub Desktop.
Example usage of Cats FlatMap
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._ | |
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)(_+_)) | |
} |
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
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