-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathIterate.java
More file actions
110 lines (90 loc) · 2.74 KB
/
Iterate.java
File metadata and controls
110 lines (90 loc) · 2.74 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
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
package clojure.lang;
/* Alex Miller, Dec 5, 2014 */
import java.io.IOException;
public class Iterate extends ASeq implements IReduce, IPending {
private static final long serialVersionUID = -78221705247226450L;
private static final Object UNREALIZED_SEED = new Object();
private final IFn f; // never null
private final Object prevSeed;
private volatile Object _seed; // lazily realized
private volatile ISeq _next; // cached
private Iterate(IFn f, Object prevSeed, Object seed){
this.f = f;
this.prevSeed = prevSeed;
this._seed = seed;
}
private Iterate(IPersistentMap meta, IFn f, Object prevSeed, Object seed, ISeq next){
super(meta);
this.f = f;
this.prevSeed = prevSeed;
this._seed = seed;
this._next = next;
}
public static ISeq create(IFn f, Object seed){
return new Iterate(f, null, seed);
}
public boolean isRealized() {
return _seed != UNREALIZED_SEED;
}
public Object first(){
if(_seed == UNREALIZED_SEED) {
_seed = f.invoke(prevSeed);
}
return _seed;
}
public ISeq next(){
if(_next == null) {
_next = new Iterate(f, first(), UNREALIZED_SEED);
}
return _next;
}
public Iterate withMeta(IPersistentMap meta){
if(meta() == meta)
return this;
return new Iterate(meta, f, prevSeed, _seed, _next);
}
public Object reduce(IFn rf){
Object first = first();
Object ret = first;
Object v = f.invoke(first);
while(true){
ret = rf.invoke(ret, v);
if(RT.isReduced(ret))
return ((IDeref)ret).deref();
v = f.invoke(v);
}
}
public Object reduce(IFn rf, Object start){
Object ret = start;
Object v = first();
while(true){
ret = rf.invoke(ret, v);
if(RT.isReduced(ret))
return ((IDeref)ret).deref();
v = f.invoke(v);
}
}
public int hashCode(){
throw new UnsupportedOperationException();
}
public int hasheq(){
throw new UnsupportedOperationException();
}
// serialization not supported
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
throw new UnsupportedOperationException();
}
// deserialization not supported
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
throw new UnsupportedOperationException();
}
}