mediachain / L-SPACE

[DEPRECATED] Books = Knowledge = Power = (Mass x Distance^2) / Time^3
MIT License
9 stars 1 forks source link

Combining/flattening nested Xor/Seq #38

Closed parkan closed 8 years ago

parkan commented 8 years ago

We encounter a lot of situations where the types look like

Xor[List[Xor[Thing]]]

which is a real pain to unpack, especially since the toList trick, which sort of works with Option semantics, does not preserve lefts.

Xor provides a combine method:

def combine[AA >: A, BB >: B](that: AA Xor BB)(implicit AA: Semigroup[AA], BB: Semigroup[BB]): AA Xor BB

This allows Xors to be combined:

scala> import cats._
import cats._

scala> import cats.std.all._
import cats.std.all._

scala> foo.combine(bar)
res7: cats.data.Xor[Int,Int] = Left(789)

scala> val foo = Xor.left[String, Int]("Boom")
foo: cats.data.Xor[String,Int] = Left(Boom)

scala> val bar = Xor.right[String, Int](123)
bar: cats.data.Xor[String,Int] = Right(123)

scala> foo.combine(bar)
res8: cats.data.Xor[String,Int] = Left(Boom)

scala> val qux = Xor.left[String, Int]("Brap")
qux: cats.data.Xor[String,Int] = Left(Brap)

scala> foo.combine(qux)
res9: cats.data.Xor[String,Int] = Left(BoomBrap)

We would need to define our own Semigroup for the left/right types, and possibly pick a different underlying Xor (I believe there's at least 2 different implementations in cats) to get the combining behavior we want (left+right = left or right). What I really want is

Xor.right(123).combine(Xor.right(456))
Xor.Right(Seq(123,456))

Which is probably impossible with the above signature, but maybe we can add our own version?

Actually, what I really want is a non-exclusive Outcome that contains 0-n successes and 0-n failures and is right-biased but doesn't discard left so you can pipe around the success/failure state and recover/collect as needed, and also has .isAllRight type methods. Does this exist somewhere? @bigs

parkan commented 8 years ago

I think you built this/something similar to this @bigs?

bigs commented 8 years ago

yeah it lives on in a stash... will try to revive

parkan commented 8 years ago

No rush, just checking if it was all forgotten