lambda icon indicating copy to clipboard operation
lambda copied to clipboard

Non-commutative operations in `foldRight`

Open t-rutten opened this issue 2 years ago • 0 comments

The behavior of Semigroup.foldRight and Monoid.foldRight is different for non-commutative operations. For monoids, we have:

Lazy<String> resultMonoid = Monoid.monoid(String::concat, "").foldRight("", Arrays.asList("1", "2", "3", "4"));
resultMonoid.value()
// "1234"

For semigroups:

Lazy<String> resultSemigroup = ((Semigroup<String> String::concat)).foldRight("", Arrays.asList("1", "2", "3", "4"));
resultSemigroup.value()
// "4321"

It appears the order of arguments is reversed inside fmap of the semigroup implementation (source). Is there motivation for this difference between foldRight in the two algebras?

t-rutten avatar Apr 20 '23 23:04 t-rutten