Skip to content

Commit

Permalink
SemigroupK sum: F[A], F[B] => F[A Either B] (#3124)
Browse files Browse the repository at this point in the history
* #3112 adding implementation

* #3112 added documentation with example

* adding pk044 to AUTHORS.md

* formatting

* fixed doctest

* fixed doctest

* used different type in example
  • Loading branch information
pk044 authored and LukaJCB committed Nov 5, 2019
1 parent 82f89e3 commit 6d191a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ possible:
* Paulo "JCranky" Siqueira
* Pavel Chlupacek
* Pavkin Vladimir
* Paweł Kiersznowski
* Paweł Lipski
* Pepe García
* Pere Villega
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/scala/cats/SemigroupK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ import cats.data.Ior
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(_)))
}

object SemigroupK {
Expand Down

0 comments on commit 6d191a5

Please sign in to comment.