diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/clj/clojure/core.clj | 2 | ||||
-rw-r--r-- | src/jvm/clojure/lang/ASeq.java | 4 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Cons.java | 6 | ||||
-rw-r--r-- | src/jvm/clojure/lang/LazySeq.java | 406 | ||||
-rw-r--r-- | src/jvm/clojure/lang/RT.java | 10 |
5 files changed, 221 insertions, 207 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index ecd417d5..b13bc52b 100644 --- a/src/clj/clojure/core.clj +++ b/src/clj/clojure/core.clj @@ -425,7 +425,7 @@ seq calls. Any closed over locals will be cleared prior to the tail call of body." [& body] - (list* '#^{:once true :super-name "clojure/lang/LazySeq"} fn* [] body)) + (list 'new 'clojure.lang.LazySeq (list* '#^{:once true} fn* [] body))) (defn concat "Returns a lazy seq representing the concatenation of the elements in the supplied colls." diff --git a/src/jvm/clojure/lang/ASeq.java b/src/jvm/clojure/lang/ASeq.java index 1dfbabe8..938c55e7 100644 --- a/src/jvm/clojure/lang/ASeq.java +++ b/src/jvm/clojure/lang/ASeq.java @@ -103,7 +103,7 @@ public int count(){ return i;
}
-public ISeq seq(){
+final public ISeq seq(){
return this;
}
@@ -114,7 +114,7 @@ public ISeq cons(Object o){ public ISeq more(){
ISeq s = next();
if(s == null)
- return LazySeq.EMPTY;
+ return PersistentList.EMPTY;
return s;
}
diff --git a/src/jvm/clojure/lang/Cons.java b/src/jvm/clojure/lang/Cons.java index 8425865c..ecb82a9d 100644 --- a/src/jvm/clojure/lang/Cons.java +++ b/src/jvm/clojure/lang/Cons.java @@ -39,7 +39,7 @@ public ISeq next(){ public ISeq more(){ if(_more == null) - return LazySeq.EMPTY; + return PersistentList.EMPTY; return _more; } @@ -47,10 +47,6 @@ public int count(){ return 1 + RT.count(_more); } -public ISeq seq(){ - return this; -} - public Cons withMeta(IPersistentMap meta){ return new Cons(meta, _first, _more); } diff --git a/src/jvm/clojure/lang/LazySeq.java b/src/jvm/clojure/lang/LazySeq.java index a268599b..4eea89d8 100644 --- a/src/jvm/clojure/lang/LazySeq.java +++ b/src/jvm/clojure/lang/LazySeq.java @@ -14,204 +14,220 @@ package clojure.lang; import java.util.*; -public class LazySeq extends AFn implements ISeq, List { - static final ISeq DUMMY = new Cons(null, null); - static final LazySeq EMPTY = new LazySeq(null); - - private ISeq s; - - public LazySeq() { - this(DUMMY); - } - - LazySeq(ISeq s) { - this.s = s; - } - - final synchronized public ISeq seq() { - if(s == DUMMY) - { - try - { - s = RT.seq(invoke()); - } - catch (Exception e) - { - throw new RuntimeException(e); - } - } - return s; - } - - public int count() { - int c = 0; - for (ISeq s = seq(); s != null; s = s.next()) - ++c; - return c; - } - - public Object first() { - return RT.first(seq()); - } - - public ISeq next() { - return RT.next(seq()); - } - - public ISeq more() { - return RT.more(seq()); - } - - public ISeq cons(Object o) { - return RT.cons(o, seq()); - } - - public IPersistentCollection empty() { - return null; - } - - public boolean equiv(Object o) { - return equals(o); - } - - public int hashCode() { - return Util.hash(seq()); - } - - public boolean equals(Object o) { - ISeq s = seq(); - if( s != null ) - return s.equiv(o); - else - return (o instanceof Sequential || o instanceof List) && RT.seq(o) == null; - } +public final class LazySeq extends Obj implements ISeq, List{ + +private IFn fn; +private ISeq s; + +public LazySeq(IFn fn){ + this.fn = fn; +} + +private LazySeq(IPersistentMap meta, ISeq s){ + super(meta); + this.fn = null; + this.s = s; +} + +public Obj withMeta(IPersistentMap meta){ + return new LazySeq(meta, seq()); +} + +final synchronized public ISeq seq(){ + if(fn != null) + { + try + { + s = RT.seq(fn.invoke()); + fn = null; + } + catch(Exception e) + { + throw new RuntimeException(e); + } + } + return s; +} + +public int count(){ + int c = 0; + for(ISeq s = seq(); s != null; s = s.next()) + ++c; + return c; +} + +public Object first(){ + seq(); + if(s == null) + return null; + return s.first(); +} + +public ISeq next(){ + seq(); + if(s == null) + return null; + return s.next(); +} + +public ISeq more(){ + seq(); + if(s == null) + return PersistentList.EMPTY; + return s.more(); +} + +public ISeq cons(Object o){ + return RT.cons(o, seq()); +} + +public IPersistentCollection empty(){ + return null; +} + +public boolean equiv(Object o){ + return equals(o); +} + +public int hashCode(){ + return Util.hash(seq()); +} + +public boolean equals(Object o){ + ISeq s = seq(); + if(s != null) + return s.equiv(o); + else + return (o instanceof Sequential || o instanceof List) && RT.seq(o) == null; +} // java.util.Collection implementation - public Object[] toArray() { - return RT.seqToArray(seq()); - } - - public boolean add(Object o) { - throw new UnsupportedOperationException(); - } - - public boolean remove(Object o) { - throw new UnsupportedOperationException(); - } - - public boolean addAll(Collection c) { - throw new UnsupportedOperationException(); - } - - public void clear() { - throw new UnsupportedOperationException(); - } - - public boolean retainAll(Collection c) { - throw new UnsupportedOperationException(); - } - - public boolean removeAll(Collection c) { - throw new UnsupportedOperationException(); - } - - public boolean containsAll(Collection c) { - for (Object o : c) - { - if (!contains(o)) - return false; - } - return true; - } - - public Object[] toArray(Object[] a) { - if (a.length >= count()) - { - ISeq s = seq(); - for (int i = 0; s != null; ++i, s = s.next()) - { - a[i] = s.first(); - } - if (a.length > count()) - a[count()] = null; - return a; - } - else - return toArray(); - } - - public int size() { - return count(); - } - - public boolean isEmpty() { - return seq() == null; - } - - public boolean contains(Object o) { - for (ISeq s = seq(); s != null; s = s.next()) - { - if (Util.equiv(s.first(), o)) - return true; - } - return false; - } - - public Iterator iterator() { - return new SeqIterator(seq()); - } - - //////////// List stuff ///////////////// - private List reify() { - return new ArrayList(this); - } - - public List subList(int fromIndex, int toIndex) { - return reify().subList(fromIndex, toIndex); - } - - public Object set(int index, Object element) { - throw new UnsupportedOperationException(); - } - - public Object remove(int index) { - throw new UnsupportedOperationException(); - } - - public int indexOf(Object o) { - ISeq s = seq(); - for (int i = 0; s != null; s = s.next(), i++) - { - if (Util.equiv(s.first(), o)) - return i; - } - return -1; - } - - public int lastIndexOf(Object o) { - return reify().lastIndexOf(o); - } - - public ListIterator listIterator() { - return reify().listIterator(); - } - - public ListIterator listIterator(int index) { - return reify().listIterator(index); - } - - public Object get(int index) { - return RT.nth(this, index); - } - - public void add(int index, Object element) { - throw new UnsupportedOperationException(); - } - - public boolean addAll(int index, Collection c) { - throw new UnsupportedOperationException(); - } +public Object[] toArray(){ + return RT.seqToArray(seq()); +} + +public boolean add(Object o){ + throw new UnsupportedOperationException(); +} + +public boolean remove(Object o){ + throw new UnsupportedOperationException(); +} + +public boolean addAll(Collection c){ + throw new UnsupportedOperationException(); +} + +public void clear(){ + throw new UnsupportedOperationException(); +} + +public boolean retainAll(Collection c){ + throw new UnsupportedOperationException(); +} + +public boolean removeAll(Collection c){ + throw new UnsupportedOperationException(); +} + +public boolean containsAll(Collection c){ + for(Object o : c) + { + if(!contains(o)) + return false; + } + return true; +} + +public Object[] toArray(Object[] a){ + if(a.length >= count()) + { + ISeq s = seq(); + for(int i = 0; s != null; ++i, s = s.next()) + { + a[i] = s.first(); + } + if(a.length > count()) + a[count()] = null; + return a; + } + else + return toArray(); +} + +public int size(){ + return count(); +} + +public boolean isEmpty(){ + return seq() == null; +} + +public boolean contains(Object o){ + for(ISeq s = seq(); s != null; s = s.next()) + { + if(Util.equiv(s.first(), o)) + return true; + } + return false; +} + +public Iterator iterator(){ + return new SeqIterator(seq()); +} + +//////////// List stuff ///////////////// +private List reify(){ + return new ArrayList(this); +} + +public List subList(int fromIndex, int toIndex){ + return reify().subList(fromIndex, toIndex); +} + +public Object set(int index, Object element){ + throw new UnsupportedOperationException(); +} + +public Object remove(int index){ + throw new UnsupportedOperationException(); +} + +public int indexOf(Object o){ + ISeq s = seq(); + for(int i = 0; s != null; s = s.next(), i++) + { + if(Util.equiv(s.first(), o)) + return i; + } + return -1; +} + +public int lastIndexOf(Object o){ + return reify().lastIndexOf(o); +} + +public ListIterator listIterator(){ + return reify().listIterator(); +} + +public ListIterator listIterator(int index){ + return reify().listIterator(index); +} + +public Object get(int index){ + return RT.nth(this, index); +} + +public void add(int index, Object element){ + throw new UnsupportedOperationException(); +} + +public boolean addAll(int index, Collection c){ + throw new UnsupportedOperationException(); +} + } diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java index 32294f4b..954f1b26 100644 --- a/src/jvm/clojure/lang/RT.java +++ b/src/jvm/clojure/lang/RT.java @@ -443,10 +443,10 @@ static public int nextID(){ ////////////// Collections support ///////////////////////////////// static public ISeq seq(Object coll){ - if(coll == null) - return null; - else if(coll instanceof ASeq) + if(coll instanceof ASeq) return (ASeq) coll; + else if(coll instanceof LazySeq) + return ((LazySeq) coll).seq(); else return seqFrom(coll); } @@ -479,6 +479,8 @@ static public IStream stream(final Object coll) throws Exception{ static ISeq seqFrom(Object coll){ if(coll instanceof Seqable) return ((Seqable) coll).seq(); + else if(coll == null) + return null; else if(coll instanceof Iterable) return IteratorSeq.create(((Iterable) coll).iterator()); else if(coll.getClass().isArray()) @@ -590,7 +592,7 @@ static public ISeq more(Object x){ return ((ISeq) x).more(); ISeq seq = seq(x); if(seq == null) - return LazySeq.EMPTY; + return PersistentList.EMPTY; return seq.more(); } |