-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flatmap.java
95 lines (83 loc) · 2.53 KB
/
Flatmap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import rx.functions.Func1;
import rx.functions.Func2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Flatmap {
public static List<Integer> repeat(int n) {
List<Integer> xs = new ArrayList<Integer>();
for (int i = 0; i < n; i++)
xs.add(n);
return xs;
}
public static <T,S> List<S> flatMap(List<T> xs, Func1<T, List<S>> f) {
List<S> res = new ArrayList<S>();
for (T x : xs) {
List<S> ys = f.call(x);
for (S y : ys) {
res.add(y);
}
}
return res;
}
public static <T,S> List<S> map(List<T> xs, Func1<T, S> f) {
return flatMap(xs, new Func1<T, List<S>>() {
@Override
public List<S> call(T x) {
return Collections.singletonList(f.call(x));
}
});
}
public static <T> List<T> filter(List<T> xs, Func1<T, Boolean> f) {
return flatMap(xs, new Func1<T, List<T>>() {
@Override
public List<T> call(T x) {
return f.call(x) ? Collections.singletonList(x)
: Collections.emptyList();
}
});
}
public static <T,S> List<Pair<T,S>> crossProduct(List<T> xs, List<S> ys) {
return flatMap(xs, new Func1<T, List<Pair<T,S>>> () {
@Override
public List<Pair<T, S>> call(T x) {
return map(ys, new Func1<S, Pair<T, S>>() {
@Override
public Pair<T, S> call(S y) {
return new Pair<T, S>(x, y);
}
});
}
});
}
public static <T,S,U> List<U> crossProductWith(List<T> xs, List<S> ys, Func2<T, S, U> f) {
return flatMap(xs, new Func1<T, List<U>> () {
@Override
public List<U> call(T x) {
return map(ys, new Func1<S, U>() {
@Override
public U call(S y) {
return f.call(x, y);
}
});
}
});
}
public static List<Integer> IncSum(List<Integer> src) {
List<Integer> res = new ArrayList<Integer>();
int accumulator = 0;
for (Integer n : src) {
accumulator += n;
res.add(accumulator);
}
return res;
}
}
class Pair<T,S> {
public T x;
public S y;
public Pair(T x, S y) {
this.x = x;
this.y = y;
}
}