Neither is a bifunctor if I understood that concep...
# arrow
j
Neither is a bifunctor if I understood that concept correctly. I think its best described by looking at the type signature of bimap (haskell and arrow):
(a -> b) -> (c -> d) -> p a c -> p b d
and arrow
fun <A, B, C, D> Kind2<F, A, B>.bimap(fl: (A) -> C, fr: (B) -> D): Kind2<F, C, D>
. So the intuition is its basically a functor applied to both type arguments. Fold looks more like this (for option):
(() -> b) -> (a -> B) -> Maybe a -> b
fun <A, B>Option<A>.fold(none: () -> B, some: (A) -> B): B
and either is similar. Fold destructures while bimap keeps structure. If you are looking for types that are bifunctors and their uses:
Either
has a bifunctor instance,
Ior
should have one and I think soon we'll have
IO
be parameterized to
IO<E, A>
and that should be a bifunctor as well
3
👍 1
k
ahh okay so the key is to keep the structure. A very simplified explanation would be that,
bimap
is similar to
map
with the capability to give a
Left
? Thank you for the arrow solution! haskell is way different and takes time to translate to kotlin, for me at lest 🙂
j
Yes structure is key here. It is a bit more general than just
Left
but for either and ior that is correct. For example `Tuple2`/`Pair` and
Const
have a
Bifunctor
instance as well. (Not sure if they do in arrow, but they should!). I kind of just think of it as a functor that applies to the second typeparameter, whereas the normal functor instance prefers one. The
Functor
bit is important here as similar laws apply for
Bifunctor
. I'd suggest looking at the laws to understand what qualifies as a `Bifunctor`/`Functor` instance.
👍 1
k
Yeah I actually reading the Category Theory for Programmers and thats where I found this question. Thanks for taking the time Jannis! You helped me a lot
j
I need to read that as well 🙈 There is currently a repo that is converting the snippets to kotlin/arrow code, it is very incomplete but could help 🙂 https://github.com/arrow-kt/Category-Theory-for-Programmers.kt
🤩 1
Welp I just noticed raul posted that below. So yeah 😄