diff options
-rw-r--r-- | clojure.markdown | 24 | ||||
-rw-r--r-- | src/boot.clj | 3 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Compiler.java | 2 | ||||
-rw-r--r-- | src/jvm/clojure/lang/PersistentVector.java | 4 | ||||
-rw-r--r-- | src/jvm/clojure/lang/RT.java | 2 |
5 files changed, 31 insertions, 4 deletions
diff --git a/clojure.markdown b/clojure.markdown index 2d77b20d..713f02e7 100644 --- a/clojure.markdown +++ b/clojure.markdown @@ -474,6 +474,30 @@ Same as `first`. Returns the first item in the list. If the list is empty, retur --- ### (*pop* list) Returns a new list without the first item. If the list is empty, throws an exception. Note - *not* the same as `rest`. + +### _Vectors (IPersistentVector)_ +Vectors are collections. They are sequences of values indexed by contiguous integers. Vectors support O(log32N) access to items by index. `count` is O(1). `conj` puts the item at the end of the vector. In addition, vectors support the functions: + +--- +### (*vector* & items) +Creates a new vector containing the items. + +--- +### (*assoc* vector index val) +Assoc[iate]. Returns a new vector that contains val at index. Note - index must be <= (count vector). + +--- +### (*get* map index) +### (*nth* map index) +Returns the value at the index. `get` returns `nil` if index out of bounds, `nth` throws an exception. + +--- +### (*peek* vector) +Returns the last item in the vector. If the vector is empty, returns `nil`. + +--- +### (*pop* vector) +Returns a new vector without the last item. If the vector is empty, throws an exception. ### _Maps_ (IPersistentMap) diff --git a/src/boot.clj b/src/boot.clj index 3e82cea8..c711e27a 100644 --- a/src/boot.clj +++ b/src/boot.clj @@ -209,6 +209,9 @@ (defn pop [list] (. RT (pop list))) +(defn nth [coll index] + (. RT (nth coll index))) + ;;map stuff (defn contains [coll key] diff --git a/src/jvm/clojure/lang/Compiler.java b/src/jvm/clojure/lang/Compiler.java index bebdede8..a9552adc 100644 --- a/src/jvm/clojure/lang/Compiler.java +++ b/src/jvm/clojure/lang/Compiler.java @@ -515,7 +515,7 @@ static abstract class HostExpr implements Expr{ m = byteValueMethod; else if(paramType == short.class) m = shortValueMethod; - gen.invokeStatic(NUMBER_TYPE, m); + gen.invokeVirtual(NUMBER_TYPE, m); } } else diff --git a/src/jvm/clojure/lang/PersistentVector.java b/src/jvm/clojure/lang/PersistentVector.java index 0f1c9d0e..d45ef2dc 100644 --- a/src/jvm/clojure/lang/PersistentVector.java +++ b/src/jvm/clojure/lang/PersistentVector.java @@ -80,9 +80,9 @@ public Object nth(int i){ public PersistentVector assocN(int i, Object val){ if(i >= 0 && i < cnt) - { return new PersistentVector(meta(), cnt, shift, doAssoc(shift, root, i, val)); - } + if(i == cnt) + return cons(val); throw new IndexOutOfBoundsException(); } diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java index 6186e973..ae46b0fb 100644 --- a/src/jvm/clojure/lang/RT.java +++ b/src/jvm/clojure/lang/RT.java @@ -320,7 +320,7 @@ static public Object dissoc(Object key, Object coll){ return ((IPersistentMap) coll).without(key); } -static public Object nth(int n, Object coll){ +static public Object nth(Object coll, int n){ if(coll == null) return null; else if(coll instanceof IPersistentVector) |