mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Добавлена функция peek к StreamValue
This commit is contained in:
parent
b8a2242272
commit
78c601cb6d
@ -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");
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user