Skip to content

Commit 02fe4b6

Browse files
committed
Добавлено комбинирование функий и flatmap
1 parent 47dfae5 commit 02fe4b6

File tree

5 files changed

+81
-42
lines changed

5 files changed

+81
-42
lines changed

program.own

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ squares = map(nums, def(x) = x * x)
126126
foreach(squares, ::echo)
127127
println "Sum: " + reduce(squares, 0, def(x, y) = x + y)
128128

129+
nums = [[1, 2], [3], [], [4, 5]]
130+
nums = flatmap(nums, IDENTITY)
131+
println "flatmap"
132+
foreach(nums, ::echo)
133+
129134
use "http"
130135

131136
/*http("http://jsonplaceholder.typicode.com/users", "POST", {"name": "OwnLang", "versionCode": 10}, def(v) {
@@ -164,4 +169,13 @@ def fact2(n) = match n {
164169
}
165170

166171
println fact1(6)
167-
println fact2(6)
172+
println fact2(6)
173+
174+
class = {
175+
"add": def(a, b) = a + b,
176+
"sub": def(a, b) = a - b,
177+
"mul": def(a, b) = a * b,
178+
"div": def(a, b) = a / b
179+
}
180+
181+
println class.add(2, class.mul(2, 2))

src/com/annimon/ownlang/lib/ArrayValue.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.annimon.ownlang.exceptions.TypeException;
44
import java.util.Arrays;
55
import java.util.Iterator;
6+
import java.util.List;
67

78
/**
89
*
@@ -39,6 +40,11 @@ public ArrayValue(Value[] elements) {
3940
System.arraycopy(elements, 0, this.elements, 0, elements.length);
4041
}
4142

43+
public ArrayValue(List<Value> values) {
44+
final int size = values.size();
45+
this.elements = values.toArray(new Value[size]);
46+
}
47+
4248
public ArrayValue(ArrayValue array) {
4349
this(array.elements);
4450
}

src/com/annimon/ownlang/lib/modules/functional.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ public final class functional implements Module {
1313
public void init() {
1414
Functions.set("foreach", new functional_foreach());
1515
Functions.set("map", new functional_map());
16+
Functions.set("flatmap", new functional_flatmap());
1617
Functions.set("reduce", new functional_reduce());
1718
Functions.set("filter", new functional_filter());
19+
20+
Functions.set("combine", new functional_combine());
21+
22+
Variables.set("IDENTITY", new FunctionValue(args -> args[0]));
1823
}
1924
}
Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,29 @@
11
package com.annimon.ownlang.lib.modules.functions;
22

3+
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
4+
import com.annimon.ownlang.exceptions.TypeException;
35
import com.annimon.ownlang.lib.*;
46

5-
import java.util.Map;
6-
7-
public final class functional_map implements Function {
7+
public final class functional_combine implements Function {
88

99
@Override
1010
public Value execute(Value... args) {
11-
if (args.length < 2) throw new RuntimeException("At least two args expected");
11+
if (args.length < 1) throw new ArgumentsMismatchException("At least one arg expected");
1212

13-
final Value container = args[0];
14-
if (container.type() == Types.ARRAY) {
15-
if (args[1].type() != Types.FUNCTION) {
16-
throw new RuntimeException("Function expected in second arg");
13+
Function result = null;
14+
for (Value arg : args) {
15+
if (arg.type() != Types.FUNCTION) {
16+
throw new TypeException(arg.toString() + " is not a function");
1717
}
18-
final Function mapper = ((FunctionValue) args[1]).getValue();
19-
return mapArray((ArrayValue) container, mapper);
18+
final Function current = result;
19+
final Function next = ((FunctionValue) arg).getValue();
20+
result = fArgs -> {
21+
if (current == null) return next.execute(fArgs);
22+
return next.execute(current.execute(fArgs));
23+
};
2024
}
2125

22-
if (container.type() == Types.MAP) {
23-
if (args[1].type() != Types.FUNCTION) {
24-
throw new RuntimeException("Function expected in second arg");
25-
}
26-
if (args[2].type() != Types.FUNCTION) {
27-
throw new RuntimeException("Function expected in third arg");
28-
}
29-
final Function keyMapper = ((FunctionValue) args[1]).getValue();
30-
final Function valueMapper = ((FunctionValue) args[2]).getValue();
31-
return mapMap((MapValue) container, keyMapper, valueMapper);
32-
}
33-
34-
throw new RuntimeException("Invalid first argument. Array or map exprected");
26+
return new FunctionValue(result);
3527
}
3628

37-
private Value mapArray(ArrayValue array, Function mapper) {
38-
final int size = array.size();
39-
final ArrayValue result = new ArrayValue(size);
40-
for (int i = 0; i < size; i++) {
41-
result.set(i, mapper.execute(array.get(i)));
42-
}
43-
return result;
44-
}
45-
46-
private Value mapMap(MapValue map, Function keyMapper, Function valueMapper) {
47-
final MapValue result = new MapValue(map.size());
48-
for (Map.Entry<Value, Value> element : map) {
49-
final Value newKey = keyMapper.execute(element.getKey());
50-
final Value newValue = valueMapper.execute(element.getValue());
51-
result.set(newKey, newValue);
52-
}
53-
return result;
54-
}
5529
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.annimon.ownlang.lib.modules.functions;
2+
3+
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
4+
import com.annimon.ownlang.exceptions.TypeException;
5+
import com.annimon.ownlang.lib.*;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public final class functional_flatmap implements Function {
10+
11+
@Override
12+
public Value execute(Value... args) {
13+
if (args.length < 2) throw new ArgumentsMismatchException("At least two arguments expected");
14+
15+
if (args[0].type() != Types.ARRAY) {
16+
throw new TypeException("Array expected in first argument");
17+
}
18+
if (args[1].type() != Types.FUNCTION) {
19+
throw new TypeException("Function expected in second argument");
20+
}
21+
22+
final Function mapper = ((FunctionValue) args[1]).getValue();
23+
return flatMapArray((ArrayValue) args[0], mapper);
24+
}
25+
26+
private Value flatMapArray(ArrayValue array, Function mapper) {
27+
final List<Value> values = new ArrayList<>();
28+
final int size = array.size();
29+
for (int i = 0; i < size; i++) {
30+
final Value inner = mapper.execute(array.get(i));
31+
if (inner.type() != Types.ARRAY) {
32+
throw new TypeException("Array expected " + inner);
33+
}
34+
for (Value value : (ArrayValue) inner) {
35+
values.add(value);
36+
}
37+
}
38+
return new ArrayValue(values);
39+
}
40+
}

0 commit comments

Comments
 (0)