summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2006-08-02 00:28:32 +0000
committerRich Hickey <richhickey@gmail.com>2006-08-02 00:28:32 +0000
commit8c2c0ea39e31c05863a5f0d07fb3916a345c3da8 (patch)
treec8655378f8b511c2be8a40c22334177097af2c1b /src
parent275c906989e19fd8fa3afc152ce8e9153eccb595 (diff)
added Iterator/Enumerator seqs
Diffstat (limited to 'src')
-rw-r--r--src/cli/runtime/EnumeratorSeq.cs49
-rw-r--r--src/cli/runtime/RT.cs2
-rw-r--r--src/jvm/clojure/lang/IteratorSeq.java56
-rw-r--r--src/jvm/clojure/lang/RT.java2
4 files changed, 109 insertions, 0 deletions
diff --git a/src/cli/runtime/EnumeratorSeq.cs b/src/cli/runtime/EnumeratorSeq.cs
new file mode 100644
index 00000000..af2bfab0
--- /dev/null
+++ b/src/cli/runtime/EnumeratorSeq.cs
@@ -0,0 +1,49 @@
+/**
+ * Copyright (c) Rich Hickey. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
+ * which can be found in the file CPL.TXT 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.
+ **/
+
+using System;
+using System.Collections;
+
+namespace clojure.lang
+{
+public class EnumeratorSeq : ISeq{
+IEnumerator e;
+volatile ISeq _rest;
+
+public static EnumeratorSeq create(IEnumerator e){
+ if(e.MoveNext())
+ return new EnumeratorSeq(e);
+ return null;
+}
+
+EnumeratorSeq(IEnumerator e){
+ this.e = e;
+ this._rest = this;
+}
+
+public Object first() {
+ return e.Current;
+}
+
+public ISeq rest() {
+ if(_rest == this)
+ {
+ lock(this){
+ if(_rest == this)
+ {
+ _rest = create(e);
+ }
+ }
+ }
+ return _rest;
+}
+}
+
+}
diff --git a/src/cli/runtime/RT.cs b/src/cli/runtime/RT.cs
index e3129c23..08009170 100644
--- a/src/cli/runtime/RT.cs
+++ b/src/cli/runtime/RT.cs
@@ -70,6 +70,8 @@ static public ISeq seq(Object coll) {
return (ISeq) coll;
else if(coll is ISequential)
return ((ISequential) coll).seq();
+ else if(coll is IEnumerable)
+ return EnumeratorSeq.create(((IEnumerable) coll).GetEnumerator());
else if(coll is Object[])
return ArraySeq.create((Object[]) coll);
else
diff --git a/src/jvm/clojure/lang/IteratorSeq.java b/src/jvm/clojure/lang/IteratorSeq.java
new file mode 100644
index 00000000..e7147f03
--- /dev/null
+++ b/src/jvm/clojure/lang/IteratorSeq.java
@@ -0,0 +1,56 @@
+/**
+ * Copyright (c) Rich Hickey. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
+ * which can be found in the file CPL.TXT 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;
+
+import java.util.Iterator;
+
+public class IteratorSeq implements ISeq{
+Iterator iter;
+volatile Object val;
+volatile ISeq _rest;
+
+public static IteratorSeq create(Iterator iter){
+ if(iter.hasNext())
+ return new IteratorSeq(iter);
+ return null;
+}
+
+IteratorSeq(Iterator iter){
+ this.iter = iter;
+ this.val = this;
+ this._rest = this;
+}
+
+public Object first() {
+ if(val == this)
+ {
+ synchronized(this){
+ if(val == this)
+ val = iter.next();
+ }
+ }
+ return val;
+}
+
+public ISeq rest() {
+ if(_rest == this)
+ {
+ synchronized(this){
+ if(_rest == this)
+ {
+ first();
+ _rest = new IteratorSeq(iter);
+ }
+ }
+ }
+ return _rest;
+}
+}
diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java
index a9b074a7..8411508b 100644
--- a/src/jvm/clojure/lang/RT.java
+++ b/src/jvm/clojure/lang/RT.java
@@ -69,6 +69,8 @@ static public ISeq seq(Object coll) throws Exception {
return (ISeq) coll;
else if(coll instanceof ISequential)
return ((ISequential) coll).seq();
+ else if(coll instanceof Iterable)
+ return IteratorSeq.create(((Iterable) coll).iterator());
else if(coll instanceof Object[])
return ArraySeq.create((Object[]) coll);
else