typelevel / cats

Lightweight, modular, and extensible library for functional programming.
https://typelevel.org/cats/
Other
5.25k stars 1.21k forks source link

Add cartesian operations on Nested #1343

Open mkotsbak opened 8 years ago

mkotsbak commented 8 years ago

Nested[_, _, _] |@| Nested[_, _, _] should probably result in a nested cartesian builder, so that it is easier to handle Nested[_, Validated, _]

peterneyens commented 7 years ago

With -Ypartial-unification this works:

import cats.data.{Nested, Validated}
import cats.implicits._

val a = Nested(1.validNel[String].some)
val b = Nested(2.validNel[String].some)
val c = Nested("boom1".invalidNel[Int].some)
val d = Nested("boom2".invalidNel[Int].some)

(a |@| b).map(_ + _)
// Nested[Option,[+A]ValidatedNel[String,A],Int] = 
//   Nested(Some(Valid(3)))

(a |@| c).map(_ +  _)
// Nested[Option,[+A]ValidatedNel[String,A],Int] =
//   Nested(Some(Invalid(NonEmptyList(boom1))))

(c |@| d).map(_ + _)
// Nested[Option,[+A]ValidatedNel[String,A],Int] = 
//   Nested(Some(Invalid(NonEmptyList(boom1, boom2))))

(c, d).map2(_ + _) // mapN after PR #1487
// Nested[Option,[+A]ValidatedNel[String,A],Int] = 
//   Nested(Some(Invalid(NonEmptyList(boom1, boom2))))

Note that the cartesian builder syntax will (probably) be replaced by enriched tuple methods.