Skip to content

Commit

Permalink
Merge pull request #128 from 7h3kk1d/foldright-semigroup-fix
Browse files Browse the repository at this point in the history
FoldRight fixes for Monoid/Semigroup
  • Loading branch information
7h3kk1d committed May 1, 2023
2 parents 725d626 + c1d2193 commit d360ae8
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ default A foldLeft(A a, Iterable<A> as) {
*/
@Override
default Lazy<A> foldRight(A a, Iterable<A> as) {
return lazy(() -> flip().foldMap(id(), reverse(cons(a, as))));
return lazy(() -> flip().foldMap(id(), cons(a, reverse(as))));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ default A foldLeft(A a, Iterable<A> as) {
* @see FoldRight
*/
default Lazy<A> foldRight(A a, Iterable<A> as) {
return FoldRight.foldRight((y, lazyX) -> lazyX.fmap(x -> apply(x, y)), lazy(a), as);
return FoldRight.foldRight((y, lazyX) -> lazyX.fmap(x -> apply(y, x)), lazy(a), as);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Fn1<Iterable<Object>, Iterable<Object>> createTestSubject() {

@Test
public void foldRightAccumulatesRightToLeft() {
assertThat(foldRight((a, lazyB) -> lazyB.fmap(b -> explainFold().apply(a, b)),
assertThat(foldRight((a, lazyAcc) -> lazyAcc.fmap(acc -> explainFold().apply(a, acc)),
lazy("5"),
asList("1", "2", "3", "4"))
.value(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jnape.palatable.lambda.monoid;

import com.jnape.palatable.lambda.adt.Maybe;
import com.jnape.palatable.lambda.functor.builtin.Lazy;
import org.junit.Test;

import java.util.List;
Expand All @@ -10,6 +11,7 @@
import static com.jnape.palatable.lambda.monoid.Monoid.monoid;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static testsupport.functions.ExplainFold.explainFold;

public class MonoidTest {

Expand All @@ -25,6 +27,13 @@ public void reduceRight() {
assertEquals((Integer) 6, sum.reduceRight(asList(1, 2, 3)));
}

@Test
public void foldRight() {
Lazy<String> lazyString = monoid(explainFold()::apply, "0")
.foldRight("4", asList("1", "2", "3"));
assertEquals("(1 + (2 + (3 + (4 + 0))))", lazyString.value());
}

@Test
public void foldMap() {
Monoid<Integer> sum = monoid(Integer::sum, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static testsupport.functions.ExplainFold.explainFold;

public class SemigroupTest {

@Test
public void foldLeft() {
Semigroup<Integer> sum = (x, y) -> x + y;
assertEquals((Integer) 6, sum.foldLeft(0, asList(1, 2, 3)));
Semigroup<String> foldFn = explainFold()::apply;
assertEquals("(((0 + 1) + 2) + 3)", foldFn.foldLeft("0", asList("1", "2", "3")));
}

@Test
public void foldRight() {
Semigroup<Integer> sum = (x, y) -> x + y;
assertEquals((Integer) 6, sum.foldRight(0, asList(1, 2, 3)).value());
Semigroup<String> foldFn = explainFold()::apply;
assertEquals("(1 + (2 + (3 + 0)))", foldFn.foldRight("0", asList("1", "2", "3")).value());
}
}
2 changes: 1 addition & 1 deletion src/test/java/testsupport/functions/ExplainFold.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
public class ExplainFold {

public static Fn2<String, String, String> explainFold() {
return (acc, x) -> format("(%s + %s)", acc, x);
return (x, y) -> format("(%s + %s)", x, y);
}
}

0 comments on commit d360ae8

Please sign in to comment.