Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cats SemigroupK vs Semigroup
import cats._
import cats.implicits._
// Cats SemigroupK vs Semigroup
object CatsSemigroupK extends App {
// semigroupK doesn't care about contents
val semListK=SemigroupK[List]
val li=List(1,2,3)
val li2=List(100,200)
println(semListK.combineK(li,li2))
val semOptionK=SemigroupK[Option]
// picks first not failing value
println(semOptionK.combineK(23.some, 100.some))
println(semOptionK.combineK(24.some, None))
println(semOptionK.combineK(None, 12.some))
// sum returns Option[Either[A,B]]
println(semOptionK.sum(25.some, 1000.some))
println(semOptionK.sum(None, 1000.some))
println(semOptionK.sum(None, None))
// vs semigroup, we care about contents
val semOption=Semigroup[Option[Int]]
// combines
println("Semigroup")
println(semOption.combine(26.some, 100.some))
println(semOption.combine(27.some, None))
println(semOption.combine(None, 12.some))
}
List(1, 2, 3, 100, 200)
Some(23)
Some(24)
Some(12)
Some(Left(25))
Some(Right(1000))
None
Semigroup
Some(126)
Some(27)
Some(12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment