Skip to content

Commit 17c052e

Browse files
committed
Adding tails
1 parent 0ed79ba commit 17c052e

3 files changed

Lines changed: 93 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
2020
- `Maybe`
2121
- `SingletonHList`
2222
- `Inits`, for iterating all the initial element subsequences of an `Iterable`
23+
- `Tails`, for iterating all the tail element subsequences of an `Iterable`
2324
- `Force`, for forcing iteration of an `Iterable` to perform any side-effects
2425
- `Snoc`, for lazily appending an element to the end of an `Iterable`
2526
- `Coalesce`, for folding an `Iterable<Either<L, R>>` into an `Either<Iterable<L>, Iterable<R>>`
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn1;
2+
3+
import com.jnape.palatable.lambda.functions.Fn1;
4+
5+
import java.util.Collections;
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.adt.hlist.HList.tuple;
10+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Empty.empty;
11+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Tail.tail;
12+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Snoc.snoc;
13+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Unfoldr.unfoldr;
14+
15+
/**
16+
* Given an <code>{@link Iterable}&lt;A&gt;</code>, produce an
17+
* <code>{@link Iterable}&lt;{@link Iterable}&lt;A&gt;&gt;</code>, representing all of the subsequences of tail
18+
* elements, ordered by size, starting with the full {@link Iterable}.
19+
* <p>
20+
* For example, <code>tails(asList(1,2,3))</code> would iterate <code>[1,2,3]</code>, <code>[2,3]</code>,
21+
* <code>[3]</code>, and <code>[]</code>.
22+
*
23+
* @param <A> the Iterable element type
24+
*/
25+
public final class Tails<A> implements Fn1<Iterable<A>, Iterable<Iterable<A>>> {
26+
27+
private static final Tails INSTANCE = new Tails();
28+
29+
private Tails() {
30+
}
31+
32+
@Override
33+
public Iterable<Iterable<A>> apply(Iterable<A> as) {
34+
return snoc(Collections::emptyIterator,
35+
unfoldr(next -> empty(next)
36+
? nothing()
37+
: just(tuple(next, tail(next))), as));
38+
}
39+
40+
@SuppressWarnings("unchecked")
41+
public static <A> Tails<A> tails() {
42+
return INSTANCE;
43+
}
44+
45+
public static <A> Iterable<Iterable<A>> tails(Iterable<A> as) {
46+
return Tails.<A>tails().apply(as);
47+
}
48+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn1;
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.InfiniteIterableSupport;
12+
import testsupport.traits.Laziness;
13+
14+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Tails.tails;
15+
import static java.util.Arrays.asList;
16+
import static java.util.Collections.emptyList;
17+
import static java.util.Collections.singletonList;
18+
import static org.junit.Assert.assertThat;
19+
import static testsupport.matchers.IterableMatcher.iterates;
20+
21+
@RunWith(Traits.class)
22+
public class TailsTest {
23+
24+
@TestTraits({EmptyIterableSupport.class, InfiniteIterableSupport.class, FiniteIteration.class, ImmutableIteration.class, Laziness.class})
25+
public Fn1<? extends Iterable, ? extends Iterable> testSubject() {
26+
return tails();
27+
}
28+
29+
@Test
30+
public void empty() {
31+
assertThat(tails(emptyList()), iterates(emptyList()));
32+
}
33+
34+
@Test
35+
public void nonEmpty() {
36+
assertThat(tails(singletonList(1)), iterates(singletonList(1), emptyList()));
37+
assertThat(tails(asList(1, 2, 3, 4, 5)), iterates(asList(1, 2, 3, 4, 5),
38+
asList(2, 3, 4, 5),
39+
asList(3, 4, 5),
40+
asList(4, 5),
41+
singletonList(5),
42+
emptyList()));
43+
}
44+
}

0 commit comments

Comments
 (0)