|
| 1 | +package cats |
| 2 | + |
| 3 | +import simulacrum.typeclass |
| 4 | + |
| 5 | +import cats.data.Ior |
| 6 | + |
| 7 | +/** |
| 8 | + * `Align` supports zipping together structures with different shapes, |
| 9 | + * holding the results from either or both structures in an `Ior`. |
| 10 | + * |
| 11 | + * Must obey the laws in cats.laws.AlignLaws |
| 12 | + */ |
| 13 | +@typeclass trait Align[F[_]] { |
| 14 | + |
| 15 | + def functor: Functor[F] |
| 16 | + |
| 17 | + /** |
| 18 | + * Pairs elements of two structures along the union of their shapes, using `Ior` to hold the results. |
| 19 | + * |
| 20 | + * Example: |
| 21 | + * {{{ |
| 22 | + * scala> import cats.implicits._ |
| 23 | + * scala> import cats.data.Ior |
| 24 | + * scala> Align[List].align(List(1, 2), List(10, 11, 12)) |
| 25 | + * res0: List[Ior[Int, Int]] = List(Both(1,10), Both(2,11), Right(12)) |
| 26 | + * }}} |
| 27 | + */ |
| 28 | + def align[A, B](fa: F[A], fb: F[B]): F[Ior[A, B]] |
| 29 | + |
| 30 | + /** |
| 31 | + * Combines elements similarly to `align`, using the provided function to compute the results. |
| 32 | + * |
| 33 | + * Example: |
| 34 | + * {{{ |
| 35 | + * scala> import cats.implicits._ |
| 36 | + * scala> Align[List].alignWith(List(1, 2), List(10, 11, 12))(_.mergeLeft) |
| 37 | + * res0: List[Int] = List(1, 2, 12) |
| 38 | + * }}} |
| 39 | + */ |
| 40 | + def alignWith[A, B, C](fa: F[A], fb: F[B])(f: Ior[A, B] => C): F[C] = |
| 41 | + functor.map(align(fa, fb))(f) |
| 42 | + |
| 43 | + /** |
| 44 | + * Align two structures with the same element, combining results according to their semigroup instances. |
| 45 | + * |
| 46 | + * Example: |
| 47 | + * {{{ |
| 48 | + * scala> import cats.implicits._ |
| 49 | + * scala> Align[List].alignCombine(List(1, 2), List(10, 11, 12)) |
| 50 | + * res0: List[Int] = List(11, 13, 12) |
| 51 | + * }}} |
| 52 | + */ |
| 53 | + def alignCombine[A: Semigroup](fa1: F[A], fa2: F[A]): F[A] = |
| 54 | + alignWith(fa1, fa2)(_.merge) |
| 55 | + |
| 56 | + /** |
| 57 | + * Same as `align`, but forgets from the type that one of the two elements must be present. |
| 58 | + * |
| 59 | + * Example: |
| 60 | + * {{{ |
| 61 | + * scala> import cats.implicits._ |
| 62 | + * scala> Align[List].padZip(List(1, 2), List(10)) |
| 63 | + * res0: List[(Option[Int], Option[Int])] = List((Some(1),Some(10)), (Some(2),None)) |
| 64 | + * }}} |
| 65 | + */ |
| 66 | + def padZip[A, B](fa: F[A], fb: F[B]): F[(Option[A], Option[B])] = |
| 67 | + alignWith(fa, fb)(_.pad) |
| 68 | + |
| 69 | + /** |
| 70 | + * Same as `alignWith`, but forgets from the type that one of the two elements must be present. |
| 71 | + * |
| 72 | + * Example: |
| 73 | + * {{{ |
| 74 | + * scala> import cats.implicits._ |
| 75 | + * scala> Align[List].padZipWith(List(1, 2), List(10, 11, 12))(_ |+| _) |
| 76 | + * res0: List[Option[Int]] = List(Some(11), Some(13), Some(12)) |
| 77 | + * }}} |
| 78 | + */ |
| 79 | + def padZipWith[A, B, C](fa: F[A], fb: F[B])(f: (Option[A], Option[B]) => C): F[C] = |
| 80 | + alignWith(fa, fb) { ior => |
| 81 | + val (oa, ob) = ior.pad |
| 82 | + f(oa, ob) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +object Align { |
| 87 | + def semigroup[F[_], A](implicit F: Align[F], A: Semigroup[A]): Semigroup[F[A]] = new Semigroup[F[A]] { |
| 88 | + def combine(x: F[A], y: F[A]): F[A] = Align[F].alignCombine(x, y) |
| 89 | + } |
| 90 | +} |
0 commit comments