Why does `Either.bifoldMap` take a `Monoid` argume...
# arrow
j
Why does
Either.bifoldMap
take a
Monoid
argument, when the monoid basically does no work, since
bifoldMap
always combines the mapped values of the
Either
with
empty
, where the result of that combination is know ahead of time to always be the same as the mapped values. In other words, why jump through the hoop of combining a value with
empty
, when we know the result will always be the same original value?
The only reason I can think of is so that
bifoldMap
can build on top of
bifoldLeft
. Is that it?
s
I believe
bifoldMap
was previously part of a typeclass that has since been removed from Arrow, which needed the monoid instance to be general for other "bifoldable" types like tuples. Now that it is specialised to
Either
, I can't see any need for it.
j
Ah, that makes sense. So maybe there are future plans to remove that dependency on
Monad
and simplify `bifoldMap`s signature. Thanks @Scott Christopher.
r
Hi @julian @Scott Christopher, bifoldMap uses the Monoid.combine in addition to
empty
in bifoldmap. it’s kind of hidden because it’s achieved through
run
, but when below we use
combine
, that is syntax that is projected by the monoid receiver. I order to replace this monoid here we would have to provide both an empty operation and associate combine operation. https://github.com/arrow-kt/arrow/blob/b4714662adcd321cc8a03d01ff86da8fb187f406/ar[…]libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt I think it’s correct as it’s stated now
j
Thanks @raulraja. But why use
combine
at all? Since we know ahead of time that for any type
C
,
MN.empty().combine(f(a))
==
f(a)
.
r
yes makes sense, it should be doable just with the empty identity, it was probably like that because of the type classes as @Scott Christopher noted
We should probably review all methods like that to see if there is others. I guess the combine here would only matter if the monoid is unlawful and empty is truly not an empty identity, otherwise as you are saying is essentially a no op in both branches
j
Cool, thanks @raulraja!
r
Thank you for finding this!
j
😄 Sure thing!