-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctional.java
More file actions
187 lines (146 loc) · 4.35 KB
/
Functional.java
File metadata and controls
187 lines (146 loc) · 4.35 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package btp.oneP;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
public class Functional {
public static void main(String[] args) {
List<Integer> li = Arrays.asList(1,2,3,4,5,6,7);
Integer result = reduce(li,new IntegerAdder());
System.out.println(result);
result = reduce(li,new IntegerSubtracter());
System.out.println(result);
System.out.println(fillter(li,new GreaterThan<Integer>(4)));
System.out.println(forEach(li,new MultiplyingIntegerCollector()).result());
System.out.println(forEach(fillter(li,new GreaterThan<Integer>(4)),
new MultiplyingIntegerCollector()).result());
MathContext mc = new MathContext(7);
List<BigDecimal> lbd = Arrays.asList(new BigDecimal(1.1,mc),new BigDecimal(2.2,mc)
,new BigDecimal(3.3,mc),new BigDecimal(4.4,mc));
BigDecimal rbd = reduce(lbd,new BigDecimalAdder());
System.out.println(rbd);
System.out.println(fillter(lbd,new GreaterThan<BigDecimal>(new BigDecimal(3))));
List<BigInteger> lbi = new ArrayList<BigInteger>();
BigInteger bi = BigInteger.valueOf(11);
for(int i=0;i<11;i++){
lbi.add(bi);
bi = bi.nextProbablePrime();
}
System.out.println(lbi);
BigInteger rbi = reduce(lbi,new BigIntegerAdder());
System.out.println(rbi);
System.out.println(rbi.isProbablePrime(5));
List<AtomicLong> lal = Arrays.asList(new AtomicLong(11),new AtomicLong(47),
new AtomicLong(74),new AtomicLong(133));
AtomicLong ral = reduce(lal,new AtomicLongAdder());
System.out.println(ral);
System.out.println(transform(lbd,new BigDecimalUlp()));
}
public static <T> T reduce(Iterable<T> seq,Combiner<T> combiner){
Iterator<T> it = seq.iterator();
if(it.hasNext()){
T result = it.next();
while(it.hasNext()){
result = combiner.combine(result, it.next());
}
return result;
}
return null;
}
public static <T> Collector<T> forEach(Iterable<T> seq,Collector<T> func){
for(T t:seq){
func.function(t);
}
return func;
}
public static <R,T> List<R> transform(Iterable<T> seq,UnaryFunction<R,T> func){
List<R> result = new ArrayList<R>();
for(T t:seq){
result.add(func.function(t));
}
return result;
}
public static <T> List<T> fillter(Iterable<T> seq,UnaryPredicated<T> pred){
List<T> result = new ArrayList<T>();
for(T t:seq){
if(pred.test(t)){
result.add(t);
}
}
return result;
}
static class IntegerAdder implements Combiner<Integer>{
@Override
public Integer combine(Integer x, Integer y) {
return x+y;
}
}
static class IntegerSubtracter implements Combiner<Integer>{
@Override
public Integer combine(Integer x, Integer y) {
return x-y;
}
}
static class BigDecimalAdder implements Combiner<BigDecimal>{
@Override
public BigDecimal combine(BigDecimal x, BigDecimal y) {
return x.add(y);
}
}
static class BigIntegerAdder implements Combiner<BigInteger>{
@Override
public BigInteger combine(BigInteger x, BigInteger y) {
return x.add(y);
}
}
static class AtomicLongAdder implements Combiner<AtomicLong>{
@Override
public AtomicLong combine(AtomicLong x, AtomicLong y) {
return new AtomicLong(x.addAndGet(y.get()));
}
}
static class BigDecimalUlp implements UnaryFunction<BigDecimal,BigDecimal>{
@Override
public BigDecimal function(BigDecimal x) {
return x.ulp();
}
}
static class GreaterThan<T extends Comparable<T>> implements UnaryPredicated<T>{
private T bound;
public GreaterThan(T bound){
this.bound = bound;
}
@Override
public boolean test(T x) {
return x.compareTo(bound)>0;
}
}
static class MultiplyingIntegerCollector implements Collector<Integer>{
private Integer val = 1;
@Override
public Integer function(Integer x) {
val *= x;
return val;
}
@Override
public Integer result() {
return val;
}
}
}
interface Combiner<T> {
T combine(T x,T y);
}
interface UnaryFunction<R,T>{
R function(T x);
}
interface Collector<T> extends UnaryFunction<T,T>{
T result();
}
interface UnaryPredicated<T>{
boolean test(T x);
}