diff options
author | Rich Hickey <richhickey@gmail.com> | 2008-12-11 20:20:10 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2008-12-11 20:20:10 +0000 |
commit | 8aaeff138f515fff4291fe61330b5f69cb7e3ce9 (patch) | |
tree | 10a4d16e7557830e49a966f27da557f657e2bffc /src | |
parent | c5e8c6f06292fab26d29cb951d8400394e0f8258 (diff) |
release coll on nth of seq
Diffstat (limited to 'src')
-rw-r--r-- | src/jvm/clojure/lang/RT.java | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java index 11f9d0c7..ae18bfce 100644 --- a/src/jvm/clojure/lang/RT.java +++ b/src/jvm/clojure/lang/RT.java @@ -453,6 +453,31 @@ static public ISeq seq(Object coll){ return seqFrom(coll); } +static public IStream stream(final Object coll) throws Exception{ + if(coll == null) + return EMPTY_STREAM; + else if(coll instanceof IStream) + return (IStream) coll; + else if(coll instanceof Streamable) + return ((Streamable)coll).stream(); + else if(coll instanceof Fn) + { + return new IStream(){ + public Object next() throws Exception { + return ((IFn)coll).invoke(); + } + }; + } + else if(coll instanceof Iterable) + return new IteratorStream(((Iterable) coll).iterator()); + else if (coll.getClass().isArray()) + return ArrayStream.createFromObject(coll); + else if (coll instanceof String) + return ArrayStream.createFromObject(((String)coll).toCharArray()); + + throw new IllegalArgumentException("Don't know how to create IStream from: " + coll.getClass().getSimpleName()); +} + static ISeq seqFrom(Object coll){ if(coll instanceof Iterable) return IteratorSeq.create(((Iterable) coll).iterator()); @@ -697,7 +722,8 @@ static public Object nth(Object coll, int n){ else if(coll instanceof Sequential) { ISeq seq = ((IPersistentCollection) coll).seq(); - for(int i = 0; i <= n && seq != null; ++i, seq = seq.rest()) + coll = null; + for(int i = 0; i <= n && seq != null; ++i, seq = seq.rest()) { if(i == n) return seq.first(); @@ -759,7 +785,8 @@ static public Object nth(Object coll, int n, Object notFound){ else if(coll instanceof Sequential) { ISeq seq = ((IPersistentCollection) coll).seq(); - for(int i = 0; i <= n && seq != null; ++i, seq = seq.rest()) + coll = null; + for(int i = 0; i <= n && seq != null; ++i, seq = seq.rest()) { if(i == n) return seq.first(); @@ -1655,4 +1682,20 @@ static public int alength(Object xs){ return Array.getLength(xs); } +final static private Object EOS = new Object(); + +final static public Object eos() { + return EOS; + } + +static public boolean isEOS(Object o){ + return o == EOS; + } + +static final public IStream EMPTY_STREAM = new IStream(){ + + public Object next() throws Exception { + return eos(); + } +}; } |