Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FoldRight fixes for Monoid/Semigroup #128

Merged
merged 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}