Add Divisible and Decideable (contravariant Applicative and co.) #1935
Comments
Yes please! Very useful for encoders |
Can you expand a bit on the example use case? I'm not really seeing its usefulness right now |
Let's say that we have an encoder trait: trait Encoder[T] {
def encode(t: T): String
} Encoder has a contravariant functor as it is isomorphic to Given this definition of divisible:
I could derive an encoder given two other encoders: case class Record(i: Int, s: String)
val recEnc: Encoder[Record] = Divisible[Record].contramap2(Encoder[Int], Encoder[String])((rec: Record) => (rec.i, rec.s)) |
Thanks @iravid, that indeed seems like a useful addition. However, I'm not sure that we'd have a lot of instances. The only one I can come up with right now would be Maybe we could add them in some other module? WDYT? |
I wouldn't mind that, as long as they're available :-) |
Looking at https://hackage.haskell.org/package/contravariant-1.4/docs/Data-Functor-Contravariant-Divisible.html we can define instances for |
Would anyone object if I were to try my hand at this? I'd be happy to give it a shot. I can always open it against |
This one is more likely to goto cats.more. |
This can be closed now after #2034 |
Actually I closed to early as we do have an analogue to |
To be honest, it'd be pretty cool to see something like this in Cats: @typeclass trait Decidable[F[_]] extends ContravariantMonoidal[F] with MonoidK[F] {
def decide[A, B, C](fa: F[A], fb: F[B])(f: C => Either[A, B]): F[C]
override def combineK[A](x: F[A], y: F[A]): F[A] =
decide[A, A, A](x, y)(Right(_))
} |
I am willing to add. I kind of meant to but forgot tbh
…On Tue, Oct 30, 2018, 03:55 Luka Jacobowitz ***@***.***> wrote:
To be honest, it'd be pretty cool to see something like this in Cats:
@typeclass trait Decidable[F[_]] extends ContravariantMonoidal[F] with MonoidK[F] {
def decide[A, B, C](fa: F[A], fb: F[B])(f: C => Either[A, B]): F[C]
override def combineK[A](x: F[A], y: F[A]): F[A] =
decide[A, A, A](x, y)(Right(_))
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1935 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AHdT-bMLA0rJ3M6_ChYQVg4v2Lw8dRQUks5uqDANgaJpZM4PlNfF>
.
|
Please feel free to go right ahead :) |
@LukaJCB If I wanted to add a dependency on |
I don't think that's in scope right now, there some talks on merging it into the cats repo or maybe even the cats-kernel module, so I propose adding Decidable now and then adding instances using |
Yeah, fair. Makes sense, I'll leave those instances out for now. I'll opt for more specific instances for now and leave a note, so we can at least have things like |
Is there an issue for merging algebra into cats? |
I'm happy to do that if that's something we want, after I finish adding this. |
@kailuowang the discussion's mostly been going on in #2041 and typelevel/algebra#218 :) |
|
Hmmm, right. I think for now I might drop the |
Speaking of use cases, here is contravariant hierarchy used for logging: https://kowainik.github.io/posts/2018-09-25-co-log (haskell) |
https://hackage.haskell.org/package/contravariant-1.4/docs/Data-Functor-Contravariant-Divisible.html
Example use case:
divide
can be seen as a way of deriving behavior by splitting a "larger" type in twoThe text was updated successfully, but these errors were encountered: