Skip to content

Commit 909691d

Browse files
committed
Добавлена функция custom в StreamValue
1 parent de07d86 commit 909691d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/main/java/com/annimon/ownlang/lib/modules/functions/functional_stream.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private static class StreamValue extends MapValue {
2929
private final ArrayValue container;
3030

3131
public StreamValue(ArrayValue container) {
32-
super(12);
32+
super(13);
3333
this.container = container;
3434
init();
3535
}
@@ -43,6 +43,7 @@ private void init() {
4343
set("dropWhile", wrapIntermediate(new functional_dropwhile()));
4444
set("skip", this::skip);
4545
set("limit", this::limit);
46+
set("custom", this::custom);
4647

4748
set("reduce", wrapTerminal(new functional_reduce()));
4849
set("forEach", wrapTerminal(new functional_foreach()));
@@ -82,6 +83,19 @@ private Value limit(Value... args) {
8283
return new StreamValue(new ArrayValue(result));
8384
}
8485

86+
private Value custom(Value... args) {
87+
Arguments.check(1, args.length);
88+
if (args[0].type() != Types.FUNCTION) {
89+
throw new TypeException("Function expected in first argument");
90+
}
91+
final Function f = ((FunctionValue) args[0]).getValue();
92+
final Value result = f.execute(container);
93+
if (result.type() == Types.ARRAY) {
94+
return new StreamValue((ArrayValue) result);
95+
}
96+
return result;
97+
}
98+
8599
private FunctionValue wrapIntermediate(Function f) {
86100
return wrap(f, true);
87101
}

src/test/resources/modules/functional/stream.own

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use "std"
12
use "functional"
23

34
def testStream() {
@@ -37,3 +38,17 @@ def testDropWhile() {
3738
assertEquals([5, 6, 7, 8], stream(data).dropWhile(def(x) = x % 2 == 0).toArray())
3839
assertEquals(data, stream(data).dropWhile(def(x) = x % 2 != 0).toArray())
3940
}
41+
42+
def testCustom() {
43+
data = [2,4,6,5]
44+
assertEquals([5,6,4,2], stream(data).custom(::reverse).toArray())
45+
}
46+
47+
def reverse(container) {
48+
size = length(container)
49+
result = newarray(size)
50+
for i : range(size) {
51+
result[size - i - 1] = container[i]
52+
}
53+
return result
54+
}

0 commit comments

Comments
 (0)