-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 Cocartesian #1362
Comments
The concept definitely makes sense to me. I wonder if @travisbrown, @mpilquist, @johnynek, or others in the "codec space" have run into situations where they would have appreciated having something like this available? |
While this In my opinion, we should have several cases where we want to write general functions over the abstraction (Monoid, Monad, Applicative, Traverse...). Maybe this has that property, but I don't see it yet. Can we see several functions we want to write over several different |
I've occasionally needed this operation, but it can be written so straightforwardly in terms of |
Drawing from an example from the shapeless wiki: type ISB = Int `Xor` String `Xor` Boolean
trait Size[T] { def sz(v: T): Int }
val caseInt: Size[Int] =
new Size[Int] { def sz(v: Int) = v }
val caseString: Size[String] =
new Size[String] { def sz(v: String) = v.length }
val caseBoolean: Size[Boolean] =
new Size[Boolean] { def sz(v: Boolean) = 1 } It would be helpful for there to be a CoCartsian such that val caseIsb: Size[ISB] = CoCartesian.xxx(caseInt, caseString, caseBoolean) Which can be used for: val isb: ISB = Xor.right(true)
caseIsb.size(isb) The difference with the shapeless use case is that the definition for |
Closing this as the discussion has sort of moved to #2620 |
Cocartesian is the dual of Cartesian.
It defines:
def sum(fa: F[A], fb: F[B]): F[Xor[A, B]]
A sample use case is for combining json codecs:
A
Codec[Int]
andCodec[String]
can be combined to form aCodec[Xor[Int, String]]
. The resulting codec encodes anXor[Int, String]
. For decoding, it attempts first decoding anInt
then aString
.The text was updated successfully, but these errors were encountered: