Добавлена функция peek к StreamValue

This commit is contained in:
Victor 2016-11-28 15:44:17 +02:00
parent b8a2242272
commit 78c601cb6d
3 changed files with 17 additions and 3 deletions

View File

@ -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<Value, Value> element : map) {
consumer.execute(element.getKey(), element.getValue());
}
return NumberValue.ZERO;
return map;
}
throw new TypeException("Invalid first argument. Array or map expected");
}

View File

@ -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);

View File

@ -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)