Skip to content

Commit ed1a4e3

Browse files
committed
interim checkin, incorporating new stream model
1 parent 703c847 commit ed1a4e3

10 files changed

Lines changed: 707 additions & 795 deletions

File tree

src/jvm/clojure/lang/APersistentVector.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -405,18 +405,25 @@ else if(count() > v.count())
405405
return 0;
406406
}
407407

408-
public IStream stream() throws Exception {
409-
final AtomicInteger ai = new AtomicInteger(0);
410-
return new IStream(){
411-
public Object next() throws Exception {
412-
int i = ai.getAndIncrement();
413-
if(i < count())
414-
return nth(i);
415-
return RT.eos();
416-
}
417-
};
408+
public Stream stream() throws Exception {
409+
return new Stream(new Src(this));
418410
}
419411

412+
static class Src extends AFn{
413+
final IPersistentVector v;
414+
int i = 0;
415+
416+
Src(IPersistentVector v) {
417+
this.v = v;
418+
}
419+
420+
public Object invoke() throws Exception {
421+
if (i < v.count())
422+
return v.nth(i++);
423+
return RT.EOS;
424+
}
425+
}
426+
420427
static class Seq extends ASeq implements IndexedSeq, IReduce{
421428
//todo - something more efficient
422429
final IPersistentVector v;

src/jvm/clojure/lang/ASeq.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,26 @@ public Iterator iterator(){
204204

205205

206206

207-
public IStream stream() throws Exception {
208-
return new Stream(this);
207+
public Stream stream() throws Exception {
208+
return new Stream(new Src(this));
209209
}
210210

211-
static class Stream implements IStream{
211+
static class Src extends AFn{
212212
ISeq s;
213213

214-
public Stream(ISeq s) {
214+
public Src(ISeq s) {
215215
this.s = s;
216216
}
217217

218-
synchronized public Object next() throws Exception {
219-
if(s != null)
218+
public Object invoke() throws Exception {
219+
ISeq sq = RT.seq(s);
220+
if(sq != null)
220221
{
221-
Object ret = s.first();
222-
s = s.next();
222+
Object ret = sq.first();
223+
s = sq.more();
223224
return ret;
224225
}
225-
return RT.eos();
226+
return RT.EOS;
226227
}
227228
}
228229

0 commit comments

Comments
 (0)