Skip to content

Commit 5f8a73b

Browse files
committed
Coalesce to fold through eithers and collect all pertinent results
1 parent 4844025 commit 5f8a73b

10 files changed

Lines changed: 229 additions & 17 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
77
### Added
88
- `Force`, for forcing iteration of an `Iterable` to perform any side-effects
99
- `Snoc`, for lazily appending an element to the end of an `Iterable`
10+
- `Coalesce`, for folding an `Iterable<Either<L, R>>` into an `Either<Iterable<L>, Iterable<R>>`
11+
- `And`, `Or`, and `Xor` all gain `BiPredicate<Boolean, Boolean>` properties
1012

1113
### Removed
1214
- `Fn1#then(Function<? super B, ? extends C>)`, deprecated in previous release
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn1;
2+
3+
import com.jnape.palatable.lambda.adt.Either;
4+
import com.jnape.palatable.lambda.functions.Fn1;
5+
import com.jnape.palatable.lambda.functions.builtin.fn2.Snoc;
6+
import com.jnape.palatable.lambda.monoid.builtin.Merge;
7+
8+
import java.util.Collections;
9+
10+
import static com.jnape.palatable.lambda.adt.Either.right;
11+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
12+
import static com.jnape.palatable.lambda.functions.builtin.fn3.FoldLeft.foldLeft;
13+
14+
/**
15+
* Fold an <code>{@link Iterable}&lt;{@link Either}&lt;L, R&gt;&gt;</code> into an <code>{@link Either}&lt;{@link
16+
* Iterable}&lt;L&gt;, {@link Iterable}&lt;R&gt;&gt;</code>, preserving all results of the side that's returned. That
17+
* is, if the result is a <code>left</code>, it will contain all left values; if it is a <code>right</code>, it will
18+
* contain all right values.
19+
* <p>
20+
* It may be useful to think of this as a more efficient version of <code>{@link Merge}&lt;{@link Iterable}&lt;L&gt;,
21+
* {@link Iterable}&lt;R&gt;&gt;</code>.
22+
*
23+
* @param <L> the left parameter type
24+
* @param <R> the right parameter type
25+
*/
26+
public final class Coalesce<L, R> implements Fn1<Iterable<Either<L, R>>, Either<Iterable<L>, Iterable<R>>> {
27+
28+
private static final Coalesce INSTANCE = new Coalesce();
29+
30+
private Coalesce() {
31+
}
32+
33+
@Override
34+
public Either<Iterable<L>, Iterable<R>> apply(Iterable<Either<L, R>> eithers) {
35+
return foldLeft((acc, e) -> acc
36+
.biMapL(ls -> e.match(Snoc.<L>snoc().flip().apply(ls), constantly(ls)))
37+
.flatMap(rs -> e.biMap(Collections::singletonList,
38+
Snoc.<R>snoc().flip().apply(rs))),
39+
right(Collections::emptyIterator),
40+
eithers);
41+
}
42+
43+
@SuppressWarnings("unchecked")
44+
public static <A, B> Coalesce<A, B> coalesce() {
45+
return INSTANCE;
46+
}
47+
48+
public static <A, B> Either<Iterable<A>, Iterable<B>> coalesce(Iterable<Either<A, B>> eithers) {
49+
return Coalesce.<A, B>coalesce().apply(eithers);
50+
}
51+
}

‎src/main/java/com/jnape/palatable/lambda/monoid/builtin/And.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.monoid.builtin;
22

33
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
45
import com.jnape.palatable.lambda.monoid.Monoid;
56

67
/**
@@ -9,7 +10,7 @@
910
* @see Or
1011
* @see Monoid
1112
*/
12-
public class And implements Monoid<Boolean> {
13+
public class And implements Monoid<Boolean>, BiPredicate<Boolean, Boolean> {
1314

1415
private static final And INSTANCE = new And();
1516

@@ -26,6 +27,16 @@ public Boolean apply(Boolean x, Boolean y) {
2627
return x && y;
2728
}
2829

30+
@Override
31+
public boolean test(Boolean x, Boolean y) {
32+
return apply(x, y);
33+
}
34+
35+
@Override
36+
public And flip() {
37+
return this;
38+
}
39+
2940
public static And and() {
3041
return INSTANCE;
3142
}

‎src/main/java/com/jnape/palatable/lambda/monoid/builtin/Or.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.monoid.builtin;
22

33
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
45
import com.jnape.palatable.lambda.monoid.Monoid;
56

67
/**
@@ -9,7 +10,7 @@
910
* @see And
1011
* @see Monoid
1112
*/
12-
public class Or implements Monoid<Boolean> {
13+
public class Or implements Monoid<Boolean>, BiPredicate<Boolean, Boolean> {
1314

1415
private static final Or INSTANCE = new Or();
1516

@@ -26,6 +27,16 @@ public Boolean apply(Boolean x, Boolean y) {
2627
return x || y;
2728
}
2829

30+
@Override
31+
public boolean test(Boolean x, Boolean y) {
32+
return apply(x, y);
33+
}
34+
35+
@Override
36+
public Or flip() {
37+
return this;
38+
}
39+
2940
public static Or or() {
3041
return INSTANCE;
3142
}

‎src/main/java/com/jnape/palatable/lambda/monoid/builtin/Xor.java‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.jnape.palatable.lambda.monoid.builtin;
22

33
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
45
import com.jnape.palatable.lambda.monoid.Monoid;
56

67
/**
7-
* Logical exclusive-or. Note that this implementation behaves as a cascade of binary exclusive-or operations, as is the
8-
* only possible monoidal behavior when applied to an unknown number of inputs.
8+
* Logical exclusive-or. Equivalent to logical <code>^</code>.
9+
* <p>
10+
* Note that this implementation behaves as a cascade of binary exclusive-or operations, as is the only possible
11+
* monoidal behavior when applied to an unknown number of inputs.
912
*
1013
* @see Or
1114
* @see And
1215
*/
13-
public final class Xor implements Monoid<Boolean> {
16+
public class Xor implements Monoid<Boolean>, BiPredicate<Boolean, Boolean> {
1417

1518
private static final Xor INSTANCE = new Xor();
1619

@@ -24,7 +27,17 @@ public Boolean identity() {
2427

2528
@Override
2629
public Boolean apply(Boolean x, Boolean y) {
27-
return x ? !y : y;
30+
return x ^ y;
31+
}
32+
33+
@Override
34+
public boolean test(Boolean x, Boolean y) {
35+
return apply(x, y);
36+
}
37+
38+
@Override
39+
public Xor flip() {
40+
return this;
2841
}
2942

3043
public static Xor xor() {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn1;
2+
3+
import org.junit.Test;
4+
5+
import static com.jnape.palatable.lambda.adt.Either.left;
6+
import static com.jnape.palatable.lambda.adt.Either.right;
7+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Coalesce.coalesce;
8+
import static java.util.Arrays.asList;
9+
import static java.util.Collections.emptyList;
10+
import static org.junit.Assert.assertThat;
11+
import static testsupport.matchers.IterableMatcher.isEmpty;
12+
import static testsupport.matchers.IterableMatcher.iterates;
13+
import static testsupport.matchers.LeftMatcher.isLeftThat;
14+
import static testsupport.matchers.RightMatcher.isRightThat;
15+
16+
public class CoalesceTest {
17+
18+
@Test
19+
public void empty() {
20+
assertThat(coalesce(emptyList()), isRightThat(isEmpty()));
21+
}
22+
23+
@Test
24+
public void allRights() {
25+
assertThat(coalesce(asList(right(1), right(2), right(3))), isRightThat(iterates(1, 2, 3)));
26+
}
27+
28+
@Test
29+
public void allLefts() {
30+
assertThat(coalesce(asList(left("foo"), left("bar"), left("baz"))), isLeftThat(iterates("foo", "bar", "baz")));
31+
}
32+
33+
@Test
34+
public void someRightsAndLefts() {
35+
assertThat(coalesce(asList(right(1), left("foo"), right(2), left("bar"), right(3), left("baz"))),
36+
isLeftThat(iterates("foo", "bar", "baz")));
37+
}
38+
}

‎src/test/java/com/jnape/palatable/lambda/iterators/GroupingIteratorTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class GroupingIteratorTest {
1818

1919
@Mock private Iterator<Object> as;
2020

21-
private GroupingIterator groupingIterator;
21+
private GroupingIterator<Object> groupingIterator;
2222

2323
@Before
2424
public void setUp() {

‎src/test/java/testsupport/matchers/IterableMatcher.java‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
import static java.util.Arrays.asList;
1010
import static org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals;
1111

12-
public class IterableMatcher extends BaseMatcher<Iterable> {
12+
public class IterableMatcher<E> extends BaseMatcher<Iterable<E>> {
1313

14-
private final Iterable expected;
14+
private final Iterable<E> expected;
1515

16-
public IterableMatcher(Iterable expected) {
16+
private IterableMatcher(Iterable<E> expected) {
1717
this.expected = expected;
1818
}
1919

@@ -29,9 +29,11 @@ public void describeTo(Description description) {
2929

3030
@Override
3131
public void describeMismatch(Object item, Description description) {
32-
if (item instanceof Iterable)
33-
description.appendText("was <").appendText(stringify((Iterable) item)).appendText(">");
34-
else
32+
if (item instanceof Iterable) {
33+
if (description.toString().endsWith("but: "))
34+
description.appendText("was ");
35+
description.appendText("<").appendText(stringify((Iterable) item)).appendText(">");
36+
} else
3537
super.describeMismatch(item, description);
3638
}
3739

@@ -69,11 +71,11 @@ private String stringify(Iterable iterable) {
6971
}
7072

7173
@SafeVarargs
72-
public static <Element> IterableMatcher iterates(Element... elements) {
73-
return new IterableMatcher(asList(elements));
74+
public static <E> IterableMatcher<E> iterates(E... es) {
75+
return new IterableMatcher<>(asList(es));
7476
}
7577

76-
public static IterableMatcher isEmpty() {
77-
return new IterableMatcher(new ArrayList());
78+
public static <E> IterableMatcher<E> isEmpty() {
79+
return new IterableMatcher<>(new ArrayList<>());
7880
}
7981
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package testsupport.matchers;
2+
3+
import com.jnape.palatable.lambda.adt.Either;
4+
import org.hamcrest.Description;
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.TypeSafeMatcher;
7+
8+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
9+
10+
public final class LeftMatcher<L, R> extends TypeSafeMatcher<Either<L, R>> {
11+
12+
private final Matcher<L> lMatcher;
13+
14+
private LeftMatcher(Matcher<L> lMatcher) {
15+
this.lMatcher = lMatcher;
16+
}
17+
18+
@Override
19+
protected boolean matchesSafely(Either<L, R> actual) {
20+
return actual.match(lMatcher::matches, constantly(false));
21+
}
22+
23+
@Override
24+
public void describeTo(Description description) {
25+
description.appendText("Left value of ");
26+
lMatcher.describeTo(description);
27+
}
28+
29+
@Override
30+
protected void describeMismatchSafely(Either<L, R> item, Description mismatchDescription) {
31+
mismatchDescription.appendText("was ");
32+
item.peek(l -> {
33+
mismatchDescription.appendText("Left value of ");
34+
lMatcher.describeMismatch(l, mismatchDescription);
35+
},
36+
r -> mismatchDescription.appendValue(item));
37+
}
38+
39+
public static <L, R> LeftMatcher<L, R> isLeftThat(Matcher<L> lMatcher) {
40+
return new LeftMatcher<>(lMatcher);
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package testsupport.matchers;
2+
3+
import com.jnape.palatable.lambda.adt.Either;
4+
import org.hamcrest.Description;
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.TypeSafeMatcher;
7+
8+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
9+
10+
public final class RightMatcher<L, R> extends TypeSafeMatcher<Either<L, R>> {
11+
12+
private final Matcher<R> rMatcher;
13+
14+
private RightMatcher(Matcher<R> rMatcher) {
15+
this.rMatcher = rMatcher;
16+
}
17+
18+
@Override
19+
protected boolean matchesSafely(Either<L, R> actual) {
20+
return actual.match(constantly(false), rMatcher::matches);
21+
}
22+
23+
@Override
24+
public void describeTo(Description description) {
25+
description.appendText("Right value of ");
26+
rMatcher.describeTo(description);
27+
}
28+
29+
@Override
30+
protected void describeMismatchSafely(Either<L, R> item, Description mismatchDescription) {
31+
mismatchDescription.appendText("was ");
32+
item.peek(l -> mismatchDescription.appendValue(item),
33+
r -> {
34+
mismatchDescription.appendText("Right value of ");
35+
rMatcher.describeMismatch(r, mismatchDescription);
36+
});
37+
}
38+
39+
public static <L, R> RightMatcher<L, R> isRightThat(Matcher<R> rMatcher) {
40+
return new RightMatcher<>(rMatcher);
41+
}
42+
}

0 commit comments

Comments
 (0)