diff --git a/src/main/java/one/util/streamex/StreamEx.java b/src/main/java/one/util/streamex/StreamEx.java index 2d9f4193..0f265cf5 100644 --- a/src/main/java/one/util/streamex/StreamEx.java +++ b/src/main/java/one/util/streamex/StreamEx.java @@ -1376,9 +1376,9 @@ public EntryStream 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. * *

* This is an quasi-intermediate @@ -1393,8 +1393,8 @@ public EntryStream withFirst() { *

{@code
      * // Returns lazily-generated stream which performs scanLeft operation on the input
      * static  StreamEx scanLeft(StreamEx input, BinaryOperator 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));
      * }}
* *

diff --git a/src/test/java/one/util/streamex/StreamExTest.java b/src/test/java/one/util/streamex/StreamExTest.java index 1e9583ef..7fe68dc4 100644 --- a/src/test/java/one/util/streamex/StreamExTest.java +++ b/src/test/java/one/util/streamex/StreamExTest.java @@ -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 }; @@ -1595,6 +1598,11 @@ private static StreamEx scanLeft(StreamEx input, BinaryOperator ope return input.headTail((head, stream) -> scanLeft(stream.mapFirst(cur -> operator.apply(head, cur)), operator) .prepend(head)); } + + // takeWhile intermediate op implementation + private static StreamEx takeWhile(StreamEx input, Predicate predicate) { + return input.headTail((head, stream) -> predicate.test(head) ? takeWhile(stream, predicate).prepend(head) : null ); + } @Test public void testHeadTailClose() {