Skip to content

Commit d24dadf

Browse files
committed
PrependAll in preparation for intersperse
1 parent 5c2ef5b commit d24dadf

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.lambda.functions.Fn2;
5+
6+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Head.head;
7+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Tail.tail;
8+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Cons.cons;
9+
import static java.util.Collections.emptyList;
10+
11+
public final class PrependAll<A> implements Fn2<A, Iterable<A>, Iterable<A>> {
12+
13+
private static final PrependAll INSTANCE = new PrependAll();
14+
15+
private PrependAll() {
16+
}
17+
18+
@Override
19+
public Iterable<A> apply(A a, Iterable<A> as) {
20+
return () -> head(as).map(head -> cons(a, cons(head, prependAll(a, tail(as))))).orElse(emptyList()).iterator();
21+
}
22+
23+
@SuppressWarnings("unchecked")
24+
public static <A> PrependAll<A> prependAll() {
25+
return INSTANCE;
26+
}
27+
28+
public static <A> Fn1<Iterable<A>, Iterable<A>> prependAll(A a) {
29+
return PrependAll.<A>prependAll().apply(a);
30+
}
31+
32+
public static <A> Iterable<A> prependAll(A a, Iterable<A> as) {
33+
return prependAll(a).apply(as);
34+
}
35+
}
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.fn2;
2+
3+
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.traitor.annotations.TestTraits;
5+
import com.jnape.palatable.traitor.runners.Traits;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import testsupport.traits.EmptyIterableSupport;
9+
import testsupport.traits.FiniteIteration;
10+
import testsupport.traits.ImmutableIteration;
11+
import testsupport.traits.Laziness;
12+
13+
import static com.jnape.palatable.lambda.functions.builtin.fn2.PrependAll.prependAll;
14+
import static java.util.Arrays.asList;
15+
import static java.util.Collections.emptyList;
16+
import static org.junit.Assert.assertThat;
17+
import static testsupport.matchers.IterableMatcher.isEmpty;
18+
import static testsupport.matchers.IterableMatcher.iterates;
19+
20+
@RunWith(Traits.class)
21+
public class PrependAllTest {
22+
23+
@TestTraits({Laziness.class, FiniteIteration.class, EmptyIterableSupport.class, ImmutableIteration.class})
24+
public Fn1<Iterable<Object>, Iterable<Object>> testSubject() {
25+
return prependAll(0);
26+
}
27+
28+
@Test
29+
public void prependsValueToAllElementsInIterable() {
30+
Iterable<Integer> ints = asList(1, 2, 3);
31+
assertThat(prependAll(0, ints), iterates(0, 1, 0, 2, 0, 3));
32+
}
33+
34+
@Test
35+
public void prependingToEmptyIterableIsStillEmpty() {
36+
assertThat(prependAll(0, emptyList()), isEmpty());
37+
}
38+
}

0 commit comments

Comments
 (0)