-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUser.java
60 lines (47 loc) · 1.36 KB
/
User.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
/** Functions to sum and increment the elements of a WeirdList.
* @author Kiet Lam
*/
class User {
/** Returns the result of adding N to each element of L. */
static WeirdList add(WeirdList L, int n) {
AddFunction func = new AddFunction(n);
return L.map(func);
}
/** Returns the sum of the elements in L.*/
static int sum(WeirdList L) {
WhatAHackFunction hack = new WhatAHackFunction();
L.map(hack);
return hack.getCounter();
}
/** An add function.*/
private static class AddFunction implements IntUnaryFunction {
/** The number to add.*/
private int _n;
/** Constructs an add function with N.*/
public AddFunction(int n) {
_n = n;
}
/** Returns the X + _n.*/
public int apply(int x) {
return _n + x;
}
/** Returns the N.*/
int getN() {
return _n;
}
}
/** Super hackish function.*/
private static class WhatAHackFunction implements IntUnaryFunction {
/** The counter.*/
private int _counter = 0;
/** Returns _counter + X.*/
public int apply(int x) {
_counter += x;
return _counter;
}
/** Returns the current counter.*/
int getCounter() {
return _counter;
}
}
}