diff options
-rw-r--r-- | src/cli/runtime/PersistentArrayMap.cs | 35 | ||||
-rw-r--r-- | src/org/clojure/runtime/PersistentArrayMap.java | 36 |
2 files changed, 69 insertions, 2 deletions
diff --git a/src/cli/runtime/PersistentArrayMap.cs b/src/cli/runtime/PersistentArrayMap.cs index cffe1738..f6a30be7 100644 --- a/src/cli/runtime/PersistentArrayMap.cs +++ b/src/cli/runtime/PersistentArrayMap.cs @@ -25,7 +25,7 @@ namespace org.clojure.runtime * null keys and values are ok, but you won't be able to distinguish a null value via get - use contains/find
*/
-public class PersistentArrayMap : IPersistentMap {
+public class PersistentArrayMap : IPersistentMap, ISequential {
internal readonly Object[] array;
@@ -132,6 +132,39 @@ public IEnumerator GetEnumerator() { return new Iter(array);
}
+public ISeq seq() {
+ if(array.Length > 0)
+ return new Seq(array,0);
+ return null;
+}
+
+internal class Seq : ISeq, IMapEntry{
+ readonly Object[] array;
+ readonly int i;
+
+ internal Seq(Object[] array, int i){
+ this.array = array;
+ this.i = i;
+ }
+
+ public Object key() {
+ return array[i];
+ }
+
+ public Object val() {
+ return array[i+1];
+ }
+
+ public Object first() {
+ return this;
+ }
+
+ public ISeq rest() {
+ if(i+2 < array.Length)
+ return new Seq(array, i + 2);
+ return null;
+ }
+}
internal class Iter : IEnumerator,IMapEntry{
Object[] array;
int i;
diff --git a/src/org/clojure/runtime/PersistentArrayMap.java b/src/org/clojure/runtime/PersistentArrayMap.java index 5d7b8171..36cff950 100644 --- a/src/org/clojure/runtime/PersistentArrayMap.java +++ b/src/org/clojure/runtime/PersistentArrayMap.java @@ -23,7 +23,7 @@ import java.util.Iterator; * null keys and values are ok, but you won't be able to distinguish a null value via get - use contains/find
*/
-public class PersistentArrayMap implements IPersistentMap {
+public class PersistentArrayMap implements IPersistentMap, ISequential {
final Object[] array;
@@ -130,6 +130,40 @@ public Iterator iterator() { return new Iter(array);
}
+public ISeq seq() {
+ if(array.length > 0)
+ return new Seq(array,0);
+ return null;
+}
+
+static class Seq implements ISeq, IMapEntry{
+ final Object[] array;
+ final int i;
+
+ Seq(Object[] array, int i){
+ this.array = array;
+ this.i = i;
+ }
+
+ public Object key() {
+ return array[i];
+ }
+
+ public Object val() {
+ return array[i+1];
+ }
+
+ public Object first() {
+ return this;
+ }
+
+ public ISeq rest() {
+ if(i+2 < array.length)
+ return new Seq(array, i + 2);
+ return null;
+ }
+}
+
static class Iter implements Iterator,IMapEntry{
Object[] array;
int i;
|