From 78c601cb6d274ec5a524b50613b9365c21fd4709 Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 28 Nov 2016 15:44:17 +0200 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20pee?= =?UTF-8?q?k=20=D0=BA=20StreamValue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/functional/functional_foreach.java | 4 ++-- .../modules/functional/functional_stream.java | 3 ++- src/test/resources/modules/functional/stream.own | 13 +++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/annimon/ownlang/modules/functional/functional_foreach.java b/src/main/java/com/annimon/ownlang/modules/functional/functional_foreach.java index 4e0ceed..239e3f3 100644 --- a/src/main/java/com/annimon/ownlang/modules/functional/functional_foreach.java +++ b/src/main/java/com/annimon/ownlang/modules/functional/functional_foreach.java @@ -20,14 +20,14 @@ public final class functional_foreach implements Function { for (Value element : array) { consumer.execute(element); } - return NumberValue.ZERO; + return array; } if (container.type() == Types.MAP) { final MapValue map = (MapValue) container; for (Map.Entry element : map) { consumer.execute(element.getKey(), element.getValue()); } - return NumberValue.ZERO; + return map; } throw new TypeException("Invalid first argument. Array or map expected"); } diff --git a/src/main/java/com/annimon/ownlang/modules/functional/functional_stream.java b/src/main/java/com/annimon/ownlang/modules/functional/functional_stream.java index 82e590a..544fe49 100644 --- a/src/main/java/com/annimon/ownlang/modules/functional/functional_stream.java +++ b/src/main/java/com/annimon/ownlang/modules/functional/functional_stream.java @@ -29,7 +29,7 @@ public final class functional_stream implements Function { private final ArrayValue container; public StreamValue(ArrayValue container) { - super(13); + super(15); this.container = container; init(); } @@ -41,6 +41,7 @@ public final class functional_stream implements Function { set("sortBy", wrapIntermediate(new functional_sortby())); set("takeWhile", wrapIntermediate(new functional_filter(true))); set("dropWhile", wrapIntermediate(new functional_dropwhile())); + set("peek", wrapIntermediate(new functional_foreach())); set("skip", this::skip); set("limit", this::limit); set("custom", this::custom); diff --git a/src/test/resources/modules/functional/stream.own b/src/test/resources/modules/functional/stream.own index 19fa66d..5d60078 100644 --- a/src/test/resources/modules/functional/stream.own +++ b/src/test/resources/modules/functional/stream.own @@ -44,6 +44,19 @@ def testCustom() { assertEquals([5,6,4,2], stream(data).custom(::reverse).toArray()) } +def testPeek() { + data = [2,3,4,5,6,7] + expected = [2,4,6] + index = 0 + + actual = stream(data) + .filter(def(x) = x % 2 == 0) + .peek(def(x) = assertEquals(expected[index++], x)) + .toArray() + assertEquals(expected, actual) + assertEquals(length(expected), index) +} + def reverse(container) { size = length(container) result = newarray(size)