diff --git a/AUTHORS.md b/AUTHORS.md index 9f4aeac421..38c55066ba 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -191,6 +191,7 @@ possible: * Paulo "JCranky" Siqueira * Pavel Chlupacek * Pavkin Vladimir + * Paweł Kiersznowski * Paweł Lipski * Pepe García * Pere Villega diff --git a/core/src/main/scala/cats/SemigroupK.scala b/core/src/main/scala/cats/SemigroupK.scala index f3ca1b5965..ef1e1042b1 100644 --- a/core/src/main/scala/cats/SemigroupK.scala +++ b/core/src/main/scala/cats/SemigroupK.scala @@ -67,4 +67,18 @@ import simulacrum.typeclass new ComposedSemigroupK[F, G] { val F = self } + + /** + * Combines `F[A]` and `F[B]` into a `F[Either[A,B]]]`. + * + * Example: + * {{{ + * scala> import cats.SemigroupK + * scala> import cats.data.NonEmptyList + * scala> SemigroupK[NonEmptyList].sum(NonEmptyList.one("abc"), NonEmptyList.one(2)) + * res0: NonEmptyList[Either[String,Int]] = NonEmptyList(Left(abc), Right(2)) + * }}} + */ + def sum[A, B](fa: F[A], fb: F[B])(implicit F: Functor[F]): F[Either[A, B]] = + combineK(F.map(fa)(Left(_)), F.map(fb)(Right(_))) }