diff options
| author | Rich Hickey <richhickey@gmail.com> | 2006-08-06 18:17:13 +0000 |
|---|---|---|
| committer | Rich Hickey <richhickey@gmail.com> | 2006-08-06 18:17:13 +0000 |
| commit | 0f1d6411d324b512aa8658d769f274b27b20bce0 (patch) | |
| tree | a01eb472abc53b975f0b7b731a83ab97fd697690 /src/cli/runtime | |
| parent | 3d28909a40372830824c98434fab45198248e75e (diff) | |
added static collection funcs
Diffstat (limited to 'src/cli/runtime')
| -rw-r--r-- | src/cli/runtime/RT.cs | 168 |
1 files changed, 134 insertions, 34 deletions
diff --git a/src/cli/runtime/RT.cs b/src/cli/runtime/RT.cs index eab52dfa..7b814154 100644 --- a/src/cli/runtime/RT.cs +++ b/src/cli/runtime/RT.cs @@ -81,17 +81,145 @@ static public int hashCombine(int seed, int hash){ return seed;
}
-static public ISeq seq(Object coll) {
- if(coll == null || coll is ISeq)
- return (ISeq) coll;
+static public ISeq seq(Object coll) {
+ if(coll == null)
+ return null;
else if(coll is IPersistentCollection)
return ((IPersistentCollection) coll).seq();
- else if(coll is IEnumerable)
- return EnumeratorSeq.create(((IEnumerable) coll).GetEnumerator());
+ else if (coll is IEnumerable)
+ return EnumeratorSeq.create(((IEnumerable)coll).GetEnumerator());
else if(coll is Object[])
return ArraySeq.create((Object[]) coll);
else
- throw new ArgumentException("Don't know how to create ISeq from arg");
+ throw new ArgumentException("Don't know how to create ISeq from arg");
+}
+
+static public Object meta(Object x)
+ {
+ if (x == null)
+ return null;
+ return ((Obj)x).meta();
+ }
+
+public static int count(Object o)
+ {
+ if (o == null)
+ return 0;
+ return ((IPersistentCollection)o).count();
+ }
+
+static public IPersistentCollection cons(Object x, IPersistentCollection y)
+ {
+ if (y == null)
+ return new PersistentList(x);
+ return y.cons(x);
+ }
+
+static public ISeq cons(Object x, ISeq y)
+ {
+ if (y == null)
+ return new PersistentList(x);
+ return (ISeq)y.cons(x);
+ }
+
+static public Object first(Object x)
+ {
+ if (x == null)
+ return null;
+ return seq(x).first();
+ }
+
+static public ISeq rest(Object x)
+ {
+ if (x == null)
+ return null;
+ return seq(x).rest();
+ }
+
+static public Object peek(Object x)
+ {
+ if (x == null)
+ return null;
+ return ((IPersistentList)x).peek();
+ }
+
+static public Object pop(Object x)
+ {
+ if (x == null)
+ return null;
+ return ((IPersistentList)x).pop();
+ }
+
+static public Object get(Object key, Object coll)
+ {
+ if (coll == null)
+ return null;
+ return ((Associative)coll).get(key);
+ }
+
+static public Object assoc(Object key, Object val, Object coll)
+ {
+ if (coll == null)
+ return new MapEntry(key, val);
+ return ((Associative)coll).assoc(key, val);
+ }
+
+static public Object contains(Object key, Object coll)
+ {
+ if (coll == null)
+ return false;
+ return ((Associative)coll).contains(key);
+ }
+
+static public Object find(Object key, Object coll)
+ {
+ if (coll == null)
+ return null;
+ return ((Associative)coll).find(key);
+ }
+
+static public Object without(Object key, Object coll)
+ {
+ if (coll == null)
+ return null;
+ return ((IPersistentMap)coll).without(key);
+ }
+
+static public Object nth(int n, Object coll) {
+ if(coll == null)
+ return null;
+ else if(coll is IPersistentArray)
+ return ((IPersistentArray)coll).nth(n);
+ else if(coll is Object[])
+ return ((Object[])coll)[n];
+ else if(coll is Sequential)
+ {
+ ISeq seq = ((IPersistentCollection) coll).seq();
+ for(int i=0;i<=n && seq != null;++i, seq = seq.rest())
+ {
+ if(i == n)
+ return seq.first();
+ }
+ return null;
+ }
+ else
+ return null;
+}
+
+static public Object assocN(int n, Object val, Object coll) {
+ if(coll == null)
+ return null;
+ else if(coll is IPersistentArray)
+ return ((IPersistentArray)coll).assocN(n,val);
+ else if(coll is Object[])
+ {
+ //hmm... this is not persistent
+ Object[] array = ((Object[])coll);
+ array[n] = val;
+ return array;
+ }
+ else
+ return null;
}
static public Iter iter(Object coll)
@@ -193,29 +321,6 @@ static public ISeq seq(Object coll) { return Convert.ToDouble(x);
}
-static public IPersistentCollection cons(Object x, IPersistentCollection y)
- {
- if (y == null)
- return new PersistentList(x);
- return y.cons(x);
- }
-
-static public ISeq cons(Object x, ISeq y)
- {
- if (y == null)
- return new PersistentList(x);
- return (ISeq)y.cons(x);
- }
-
-
-static public Object first(Object x) {
- return seq(x).first();
-}
-
-static public ISeq rest(Object x) {
- return seq(x).rest();
-}
-
static public ISeq list()
{
return null;
@@ -407,10 +512,5 @@ static public Object setValues(params Object[] vals) return null;
}
-public static int count(Object o) {
- if(o == null)
- return 0;
- return ((IPersistentCollection)o).count();
-}
}
}
\ No newline at end of file |
