-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIFaces.java
More file actions
63 lines (54 loc) · 1.82 KB
/
IFaces.java
File metadata and controls
63 lines (54 loc) · 1.82 KB
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
// ELZ-IFac/IFaces.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
interface Operat<T> {
T oper(T lhs, T rhs);
}
class Tools {
public static <T> T foldl(
Operat<T> op, List<T> list, T id) {
T acc = id;
for (T e : list)
acc = op.oper(acc,e);
return acc;
}
public static <T> List<T> combine(
Operat<T> op, List<T> l1, List<T> l2) {
List<T> res = new ArrayList<T>();
int size = Math.min(l1.size(), l2.size());
for (int i = 0; i < size; ++i)
res.add(op.oper(l1.get(i), l2.get(i)));
return res;
}
}
public class IFaces {
public static void main (String[] args) {
Operat<Integer> operInt = new Operat<Integer>() {
@Override
public Integer oper(Integer lhs, Integer rhs) {
return lhs + rhs;
}
};
Operat<String> operStr = new Operat<String>() {
@Override
public String oper(String lhs, String rhs) {
return lhs + rhs;
}
};
List<Integer> listInt1 = Arrays.asList(1,2,3,4),
listInt2 = Arrays.asList(5,6,7,8);
List<Integer> intRes =
Tools.combine(operInt, listInt1, listInt2);
System.out.println("intRes = " + intRes);
List<String> listStr1 = Arrays.asList("a","b","c"),
listStr2 = Arrays.asList("1","2","3");
List<String> strRes =
Tools.combine(operStr, listStr1, listStr2);
System.out.println("strRes = " + strRes);
int intFold = Tools.foldl(operInt, intRes, 0);
System.out.println("intFold = " + intFold);
String strFold = Tools.foldl(operStr, strRes, "");
System.out.println("strFold = " + strFold);
}
}