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 Cocartesian #1362

Closed
jvliwanag opened this issue Sep 7, 2016 · 5 comments
Closed

Add Cocartesian #1362

jvliwanag opened this issue Sep 7, 2016 · 5 comments

Comments

@jvliwanag
Copy link

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] and Codec[String] can be combined to form a Codec[Xor[Int, String]]. The resulting codec encodes an Xor[Int, String]. For decoding, it attempts first decoding an Int then a String.

@non
Copy link
Contributor

non commented Sep 7, 2016

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?

@johnynek
Copy link
Contributor

johnynek commented Sep 7, 2016

While this sum combination certainly is useful for codecs, I'm not sure one is likely to abstract over it.

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 F[_]?

@travisbrown
Copy link
Contributor

I've occasionally needed this operation, but it can be written so straightforwardly in terms of map and combineK (or a more concrete version of combineK like circe's or) that I've never particularly wanted it as a primitive.

@jvliwanag
Copy link
Author

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 caseIsb
is explicitly defined rather than automatically derived.

@LukaJCB
Copy link
Member

LukaJCB commented Jul 3, 2019

Closing this as the discussion has sort of moved to #2620

@LukaJCB LukaJCB closed this as completed Jul 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants