Skip to content

Commit

Permalink
[#54] takeWhile-implementation test; javaDoc update
Browse files Browse the repository at this point in the history
  • Loading branch information
amaembo committed Jan 14, 2016
1 parent ae6a1f3 commit 3e910c3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/main/java/one/util/streamex/StreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -1376,9 +1376,9 @@ public EntryStream<T, T> withFirst() {

/**
* Creates a new Stream which is the result of applying of the supplied
* mapper {@code BiFunction} to the first element of the current stream and
* the stream containing the rest elements. (If a mapped stream is
* {@code null} an empty stream is used, instead.)
* mapper {@code BiFunction} to the first element of the current stream
* (head) and the stream containing the rest elements (tail). If a mapped
* stream is {@code null} an empty stream is used, instead.
*
* <p>
* This is an <a href="package-summary.html#StreamOps">quasi-intermediate
Expand All @@ -1393,8 +1393,8 @@ public EntryStream<T, T> withFirst() {
* <pre>{@code
* // Returns lazily-generated stream which performs scanLeft operation on the input
* static <T> StreamEx<T> scanLeft(StreamEx<T> input, BinaryOperator<T> operator) {
* return input.withFirst((head, stream) -> scanLeft(stream.mapFirst(cur -> operator.apply(head, cur)), operator)
* .prepend(head));
* return input.headTail((head, tail) ->
* scanLeft(tail.mapFirst(cur -> operator.apply(head, cur)), operator).prepend(head));
* }}</pre>
*
* <p>
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/one/util/streamex/StreamExTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,9 @@ public void testHeadTail() {

streamEx(() -> StreamEx.iterate(1, x -> x + 1), s -> assertEquals(asList(1, 3, 6, 10, 15, 21, 28, 36, 45, 55,
66, 78, 91), scanLeft(s.get(), Integer::sum).takeWhile(x -> x < 100).toList()));

streamEx(() -> StreamEx.iterate(1, x -> x + 1), s -> assertEquals(IntStreamEx.range(1, 100).boxed().toList(),
takeWhile(s.get(), x -> x < 100).toList()));

// http://stackoverflow.com/q/34395943/4856258
int[] input = { 1, 2, 3, -1, 3, -10, 9, 100, 1, 100, 0 };
Expand All @@ -1595,6 +1598,11 @@ private static <T> StreamEx<T> scanLeft(StreamEx<T> input, BinaryOperator<T> ope
return input.headTail((head, stream) -> scanLeft(stream.mapFirst(cur -> operator.apply(head, cur)), operator)
.prepend(head));
}

// takeWhile intermediate op implementation
private static <T> StreamEx<T> takeWhile(StreamEx<T> input, Predicate<T> predicate) {
return input.headTail((head, stream) -> predicate.test(head) ? takeWhile(stream, predicate).prepend(head) : null );
}

@Test
public void testHeadTailClose() {
Expand Down

0 comments on commit 3e910c3

Please sign in to comment.