-
Notifications
You must be signed in to change notification settings - Fork 0
/
LazyList.groovy
71 lines (54 loc) · 1.66 KB
/
LazyList.groovy
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
/**
* Copied from http://groovy.codehaus.org/Functional+Programming+with+Groovy
*/
class LazyList {
def head
private Closure tail
LazyList(def head, Closure tail) { this.head=head; this.tail=tail }
def LazyList getTail() { tail==null ? nil() : tail.call() }
static LazyList nil() {
new LazyList( null,{-> null} )
}
def LazyList filter(Closure matchExpr) {
if(head==null)
return nil();
if (matchExpr(head))
return new LazyList(head, { getTail().filter(matchExpr) })
else{
return getTail().filter(matchExpr)
}
}
def each(Closure proc){
if(head != null){
proc(head)
getTail().each(proc);
}
}
def fold(accumulator, Closure proc){
if(head==null)
return accumulator
return tail().fold(proc.call(accumulator, head), proc);
}
def LazyList map(Closure proc){
if(head==null)
return nil();
return new LazyList(proc.call(head), {getTail().map(proc)})
}
def List getHead(n) {
def valuesFromHead = [];
def current = this
for(i in 0..n){
if(current.head==null)
return valuesFromHead;
valuesFromHead << current.head
current = current.tail
}
valuesFromHead
}
def get(i){
return getHead(i+1)[i]
}
static LazyList range (a,b){ if (a>b) LazyList.nil() else new LazyList(a, {range(a+1, b)})}
static LazyList reverseRange (a,b){ if(b>a) LazyList.nil() else new LazyList(a, {reverseRange(a-1, b)})}
static LazyList integersStartingFrom (n){return new LazyList(n, {integersStartingFrom(n+1)})}
}