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

Create a copair typeclass #974

Closed
Jacoby6000 opened this issue Apr 11, 2016 · 2 comments
Closed

Create a copair typeclass #974

Jacoby6000 opened this issue Apr 11, 2016 · 2 comments

Comments

@Jacoby6000
Copy link
Contributor

I was speaking with @tpolecat in the gitter channel, and I was looking to generalize some things, where I'd like to accept Either, Xor and Validated. Rob mentioned that this would be called a Copair. I've done a very small (tiny) amount of work on this so far, but I know there's more. I'm working to fully implement this now. To provide an idea, my first pass implementation looked like this

  trait Copair[F[_,_]] {
    def left[A,B](a: A): F[A,B]
    def right[A,B](b: B): F[A,B]

    def fold[A,B,C](f: F[A,B])(fa: A => C, fb: B => C): C

    final def swap[A,B](f: F[A,B]): F[B,A] = fold(f)(right[B,A](_), left[B,A](_))
  }

  implicit class CopairOps[F[_,_]: Copair, A, B](pair: F[A,B]) {
    val ev = implicitly[Copair[F]]

    def fold[C](fa: A => C, fb: B => C): C = ev.fold(pair)(fa, fb)
    def swap: F[B,A] = ev.swap(pair)
  }

  implicit class CopairBuilderOps[A](a: A) {
    def left[F[_,_]: Copair, A, B](a: A): F[A,B] = implicitly[Copair[F]].left(a)
    def right[F[_,_]: Copair, A, B](b: B): F[A,B] = implicitly[Copair[F]].right(b)
  }

Based on the conversations I had with @tpolecat it sounds like you should be able to derive a Bifunctor and Bifoldable given a Copair. He also mentioned that there's probably a reason this thing doesn't exist, so this might be a dead end.

@etorreborre
Copy link
Contributor

I am wondering if Copair is not actually a Coproduct as described in Alissa Pajer's presentation: https://vimeo.com/channels/flatmap2016/165852490.

@Jacoby6000
Copy link
Contributor Author

Closing this issue in light of discussion on #976. Probably should've closed this ages ago 😅

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

2 participants