Skip to content

Commit b15fdd0

Browse files
committed
Adding init
1 parent 17c052e commit b15fdd0

5 files changed

Lines changed: 144 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1919
- `LambdaIterable`
2020
- `Maybe`
2121
- `SingletonHList`
22-
- `Inits`, for iterating all the initial element subsequences of an `Iterable`
23-
- `Tails`, for iterating all the tail element subsequences of an `Iterable`
2422
- `Force`, for forcing iteration of an `Iterable` to perform any side-effects
2523
- `Snoc`, for lazily appending an element to the end of an `Iterable`
2624
- `Coalesce`, for folding an `Iterable<Either<L, R>>` into an `Either<Iterable<L>, Iterable<R>>`
@@ -29,6 +27,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
2927
- `Maybe`, lambda's analog of `java.util.Optional` conforming to all the lambda types
3028
- `Contravariant`, an interface representing functors that map contravariantly over their parameters
3129
- `Profunctor` extends `Contravariant`
30+
- `Tails`, for iterating all the tail element subsequences of an `Iterable`
31+
- `Inits`, for iterating all the initial element subsequences of an `Iterable`
32+
- `Init`, for iterating all but the last element of an `Iterable`
3233

3334
### Removed
3435
- `Fn1#then(Function<? super B, ? extends C>)`, deprecated in previous release
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn1;
2+
3+
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.lambda.iterators.InitIterator;
5+
6+
/**
7+
* Given an <code>{@link Iterable}&lt;A&gt;</code>, produce an
8+
* <code>{@link Iterable}&lt;A&gt;</code> of all elements but the last one.
9+
*
10+
* @param <A> the Iterable element type
11+
*/
12+
public final class Init<A> implements Fn1<Iterable<A>, Iterable<A>> {
13+
14+
private static final Init INSTANCE = new Init();
15+
16+
private Init() {
17+
}
18+
19+
@Override
20+
public Iterable<A> apply(Iterable<A> as) {
21+
return () -> new InitIterator<>(as);
22+
}
23+
24+
@SuppressWarnings("unchecked")
25+
public static <A> Init<A> init() {
26+
return INSTANCE;
27+
}
28+
29+
public static <A> Iterable<A> init(Iterable<A> as) {
30+
return Init.<A>init().apply(as);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.jnape.palatable.lambda.iterators;
2+
3+
import java.util.Iterator;
4+
import java.util.NoSuchElementException;
5+
6+
public class InitIterator<A> extends ImmutableIterator<A> {
7+
private final Iterator<A> asIterator;
8+
private A queued;
9+
10+
public InitIterator(Iterable<A> as) {
11+
asIterator = as.iterator();
12+
}
13+
14+
@Override
15+
public boolean hasNext() {
16+
if (queued == null)
17+
if (asIterator.hasNext())
18+
queued = asIterator.next();
19+
20+
return asIterator.hasNext();
21+
}
22+
23+
@Override
24+
public A next() {
25+
if (!hasNext())
26+
throw new NoSuchElementException();
27+
28+
A next = queued;
29+
queued = asIterator.next();
30+
return next;
31+
}
32+
}
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.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.Init.init;
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.isEmpty;
20+
import static testsupport.matchers.IterableMatcher.iterates;
21+
22+
@RunWith(Traits.class)
23+
public class InitTest {
24+
25+
@TestTraits({EmptyIterableSupport.class, InfiniteIterableSupport.class, Laziness.class, ImmutableIteration.class, FiniteIteration.class})
26+
public Fn1<? extends Iterable, ? extends Iterable> testSubject() {
27+
return init();
28+
}
29+
30+
@Test
31+
public void empty() {
32+
assertThat(init(emptyList()), isEmpty());
33+
}
34+
35+
@Test
36+
public void nonEmpty() {
37+
assertThat(init(singletonList(1)), isEmpty());
38+
assertThat(init(asList(1, 2, 3)), iterates(1, 2));
39+
}
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.jnape.palatable.lambda.iterators;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Collections;
6+
7+
import static java.util.Arrays.asList;
8+
import static java.util.Collections.singletonList;
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertFalse;
11+
import static org.junit.Assert.assertTrue;
12+
13+
public class InitIteratorTest {
14+
15+
@Test
16+
public void iteratesNothingForEmptyIterable() {
17+
assertFalse(new InitIterator<>(Collections::emptyIterator).hasNext());
18+
}
19+
20+
@Test
21+
public void iteratesNothingForSingleElementIterable() {
22+
assertFalse(new InitIterator<>(singletonList(1)).hasNext());
23+
}
24+
25+
@Test
26+
public void iteratesAllButLastElement() {
27+
InitIterator<Integer> iterator = new InitIterator<>(asList(1, 2, 3));
28+
29+
assertTrue(iterator.hasNext());
30+
assertEquals((Integer) 1, iterator.next());
31+
32+
assertTrue(iterator.hasNext());
33+
assertEquals((Integer) 2, iterator.next());
34+
35+
assertFalse(iterator.hasNext());
36+
}
37+
}

0 commit comments

Comments
 (0)