X Tutup
Skip to content

Commit 44db57e

Browse files
committed
adding CatMaybes to unwrap an Iterable<Maybe<A>> to Iterable<A>
1 parent 10f1b0d commit 44db57e

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
3131
- `Tails`, for iterating all the tail element subsequences of an `Iterable`
3232
- `Inits`, for iterating all the initial element subsequences of an `Iterable`
3333
- `Init`, for iterating all but the last element of an `Iterable`
34+
- `CatMaybes`, for unwrapping the present values in an `Iterable<Maybe<A>>` to produce an `Iterable<A>`
3435

3536
### Removed
3637
- `Fn1#then(Function<? super B, ? extends C>)`, deprecated in previous release
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn1;
2+
3+
import com.jnape.palatable.lambda.adt.Maybe;
4+
import com.jnape.palatable.lambda.functions.Fn1;
5+
6+
import java.util.Collections;
7+
8+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Flatten.flatten;
9+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Map.map;
10+
11+
/**
12+
* Given an <code>{@link Iterable}&lt;{@link Maybe}&lt;A&gt;&gt;</code>, return an
13+
* <code>{@link Iterable}&lt;A&gt;</code> of only the present values.
14+
*
15+
* @param <A> the {@link Maybe} element type, as well as the resulting {@link Iterable} element type
16+
*/
17+
public final class CatMaybes<A> implements Fn1<Iterable<Maybe<A>>, Iterable<A>> {
18+
private static final CatMaybes INSTANCE = new CatMaybes();
19+
20+
private CatMaybes() {
21+
}
22+
23+
@Override
24+
public Iterable<A> apply(Iterable<Maybe<A>> maybes) {
25+
return flatten(map(m -> m.<Iterable<A>>fmap(Collections::singletonList)
26+
.orElse(Collections::emptyIterator), maybes));
27+
}
28+
29+
@SuppressWarnings("unchecked")
30+
public static <A> CatMaybes<A> catMaybes() {
31+
return INSTANCE;
32+
}
33+
34+
public static <A> Iterable<A> catMaybes(Iterable<Maybe<A>> as) {
35+
return CatMaybes.<A>catMaybes().apply(as);
36+
}
37+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn1;
2+
3+
import com.jnape.palatable.traitor.runners.Traits;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
7+
import static com.jnape.palatable.lambda.adt.Maybe.just;
8+
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
9+
import static com.jnape.palatable.lambda.functions.builtin.fn1.CatMaybes.catMaybes;
10+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Repeat.repeat;
11+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Take.take;
12+
import static java.util.Arrays.asList;
13+
import static java.util.Collections.emptyList;
14+
import static org.junit.Assert.assertThat;
15+
import static testsupport.matchers.IterableMatcher.isEmpty;
16+
import static testsupport.matchers.IterableMatcher.iterates;
17+
18+
@RunWith(Traits.class)
19+
public class CatMaybesTest {
20+
21+
@Test
22+
public void empty() {
23+
assertThat(catMaybes(emptyList()), isEmpty());
24+
}
25+
26+
@Test
27+
public void onlyNothingsIsEquivalentToEmpty() {
28+
assertThat(catMaybes(asList(nothing(), nothing(), nothing())), isEmpty());
29+
}
30+
31+
@Test
32+
public void nonEmpty() {
33+
assertThat(catMaybes(asList(nothing(), just(1), just(2), nothing(), just(3))), iterates(1, 2, 3));
34+
}
35+
36+
@Test
37+
public void infiniteIterableSupport() {
38+
assertThat(take(3, catMaybes(repeat(just(1)))), iterates(1, 1, 1));
39+
}
40+
}

0 commit comments

Comments
 (0)
X Tutup