Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zipAll to Align typeclass #3372

Merged
merged 1 commit into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions core/src/main/scala/cats/Align.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ import cats.data.Ior
val (oa, ob) = ior.pad
f(oa, ob)
}

/**
* Pairs elements of two structures along the union of their shapes, using placeholders for missing values.
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> Align[List].zipAll(List(1, 2), List(10, 11, 12), 20, 21)
* res0: List[(Int, Int)] = List((1,10), (2,11), (20,12))
* }}}
*/
def zipAll[A, B](fa: F[A], fb: F[B], a: A, b: B): F[(A, B)] =
alignWith(fa, fb) {
case Ior.Left(x) => (x, b)
case Ior.Right(y) => (a, y)
case Ior.Both(x, y) => (x, y)
}
}

object Align {
Expand Down
4 changes: 4 additions & 0 deletions tests/src/test/scala/cats/tests/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ object SyntaxSuite {
val fb = mock[F[B]]
val f = mock[A Ior B => C]
val f2 = mock[(Option[A], Option[B]) => C]
val a = mock[A]
val b = mock[B]

val fab = fa.align(fb)
val fc = fa.alignWith(fb)(f)
Expand All @@ -478,6 +480,8 @@ object SyntaxSuite {

implicit val sa: Semigroup[A] = mock[Semigroup[A]]
val fa2 = fa.alignCombine(fa)

val zippedAll = fa.zipAll(fb, a, b)
}

def testNonEmptyList[A, B: Order]: Unit = {
Expand Down