FoldRight fixes for Monoid/Semigroup · BackendJava/lambda@c1d2193

File tree

6 files changed

lines changed

    • main/java/com/jnape/palatable/lambda

      • com/jnape/palatable/lambda

6 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -82,7 +82,7 @@ default A foldLeft(A a, Iterable<A> as) {

8282

*/

8383

@Override

8484

default Lazy<A> foldRight(A a, Iterable<A> as) {

85-

return lazy(() -> flip().foldMap(id(), reverse(cons(a, as))));

85+

return lazy(() -> flip().foldMap(id(), cons(a, reverse(as))));

8686

}

8787
8888

/**

Original file line numberDiff line numberDiff line change

@@ -39,7 +39,7 @@ default A foldLeft(A a, Iterable<A> as) {

3939

* @see FoldRight

4040

*/

4141

default Lazy<A> foldRight(A a, Iterable<A> as) {

42-

return FoldRight.foldRight((y, lazyX) -> lazyX.fmap(x -> apply(x, y)), lazy(a), as);

42+

return FoldRight.foldRight((y, lazyX) -> lazyX.fmap(x -> apply(y, x)), lazy(a), as);

4343

}

4444
4545

/**

Original file line numberDiff line numberDiff line change

@@ -29,7 +29,7 @@ public Fn1<Iterable<Object>, Iterable<Object>> createTestSubject() {

2929
3030

@Test

3131

public void foldRightAccumulatesRightToLeft() {

32-

assertThat(foldRight((a, lazyB) -> lazyB.fmap(b -> explainFold().apply(a, b)),

32+

assertThat(foldRight((a, lazyAcc) -> lazyAcc.fmap(acc -> explainFold().apply(a, acc)),

3333

lazy("5"),

3434

asList("1", "2", "3", "4"))

3535

.value(),

Original file line numberDiff line numberDiff line change

@@ -1,6 +1,7 @@

11

package com.jnape.palatable.lambda.monoid;

22
33

import com.jnape.palatable.lambda.adt.Maybe;

4+

import com.jnape.palatable.lambda.functor.builtin.Lazy;

45

import org.junit.Test;

56
67

import java.util.List;

@@ -10,6 +11,7 @@

1011

import static com.jnape.palatable.lambda.monoid.Monoid.monoid;

1112

import static java.util.Arrays.asList;

1213

import static org.junit.Assert.assertEquals;

14+

import static testsupport.functions.ExplainFold.explainFold;

1315
1416

public class MonoidTest {

1517

@@ -25,6 +27,13 @@ public void reduceRight() {

2527

assertEquals((Integer) 6, sum.reduceRight(asList(1, 2, 3)));

2628

}

2729
30+

@Test

31+

public void foldRight() {

32+

Lazy<String> lazyString = monoid(explainFold()::apply, "0")

33+

.foldRight("4", asList("1", "2", "3"));

34+

assertEquals("(1 + (2 + (3 + (4 + 0))))", lazyString.value());

35+

}

36+
2837

@Test

2938

public void foldMap() {

3039

Monoid<Integer> sum = monoid(Integer::sum, 0);

Original file line numberDiff line numberDiff line change

@@ -4,18 +4,19 @@

44
55

import static java.util.Arrays.asList;

66

import static org.junit.Assert.assertEquals;

7+

import static testsupport.functions.ExplainFold.explainFold;

78
89

public class SemigroupTest {

910
1011

@Test

1112

public void foldLeft() {

12-

Semigroup<Integer> sum = (x, y) -> x + y;

13-

assertEquals((Integer) 6, sum.foldLeft(0, asList(1, 2, 3)));

13+

Semigroup<String> foldFn = explainFold()::apply;

14+

assertEquals("(((0 + 1) + 2) + 3)", foldFn.foldLeft("0", asList("1", "2", "3")));

1415

}

1516
1617

@Test

1718

public void foldRight() {

18-

Semigroup<Integer> sum = (x, y) -> x + y;

19-

assertEquals((Integer) 6, sum.foldRight(0, asList(1, 2, 3)).value());

19+

Semigroup<String> foldFn = explainFold()::apply;

20+

assertEquals("(1 + (2 + (3 + 0)))", foldFn.foldRight("0", asList("1", "2", "3")).value());

2021

}

2122

}

Original file line numberDiff line numberDiff line change

@@ -7,6 +7,6 @@

77

public class ExplainFold {

88
99

public static Fn2<String, String, String> explainFold() {

10-

return (acc, x) -> format("(%s + %s)", acc, x);

10+

return (x, y) -> format("(%s + %s)", x, y);

1111

}

1212

}