Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created June 9, 2020 11:15
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/2a602e5f3273aa1bac14227ef02d80e3 to your computer and use it in GitHub Desktop.
Save fancellu/2a602e5f3273aa1bac14227ef02d80e3 to your computer and use it in GitHub Desktop.
Cats Apply, does cartesian maps, extends Functor and Semigroupal (which does cartesian joins, unlike pairwise Align)
import cats._
import cats.implicits._
// Cats Apply, does cartesian maps, extends Functor and Semigroupal (which does cartesian joins, unlike pairwise Align)
object CatsAp extends App {
val apOption=Apply[Option]
val option1: Option[(Int, Int)] =apOption.product(Option(1), Option(2))
println(option1)
val option2: Option[(Int, Nothing)] =apOption.product(Option(1), None)
println(option2)
val option3: Option[(Nothing, Nothing)] =apOption.product(None, None)
println(option3)
val apList=Apply[List]
val l1=List(1,2)
val l2=List(10,11,12)
val tupleList: List[(Int, Int)] =apList.product(l1, l2)
println(tupleList)
val out1=apOption.ap[Int,Int](Option(_+100))(23.some)
println(out1)
val out2=apList.ap[Int,Int](List(_+1,_+100))(l1)
println(out2)
}
Some((1,2))
None
None
List((1,10), (1,11), (1,12), (2,10), (2,11), (2,12))
Some(123)
List(2, 3, 101, 102)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment