You're viewing version 1.2.0 of to-array-2d. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

to-array-2d

clojure.core

  • (to-array-2d coll)
Returns a (potentially-ragged) 2-dimensional array of Objects
containing the contents of coll, which can be any Collection of any
Collection.

2 Examples top

  • user> (def a (to-array-2d [[1 2 3][4 5 6]]))
    #'user/a
    user> (alength a)
    2
    user> (alength (aget a 0))
    3
    user> (aget a 0 0)
    1
    user> (aget a 0 1)
    2
    user> (aget a 0 2)
    3
    user> (aget a 1 0)
    4
    user> (aget a 2 0)
    → ERROR
    nil
    
    user> 
  • ;; quick example of a ragged array where the length of each element of the 
    ;; 2d array is unique
    
    user=> (def a (to-array-2d [[0][1 2][3 4 5][6 7 8 9]]))
    #'user/a
    user=> (map alength [(aget a 0)(aget a 1)(aget a 2)])
    (1 2 3)
    user=>
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:3077 top

(defn to-array-2d
  "Returns a (potentially-ragged) 2-dimensional array of Objects
  containing the contents of coll, which can be any Collection of any
  Collection."
  {:tag "[[Ljava.lang.Object;"
   :added "1.0"}
  [^java.util.Collection coll]
    (let [ret (make-array (. Class (forName "[Ljava.lang.Object;")) (. coll (size)))]
      (loop [i 0 xs (seq coll)]
        (when xs
          (aset ret i (to-array (first xs)))
          (recur (inc i) (next xs))))
      ret))
Vars in clojure.core/to-array-2d: aset defn first inc let loop next to-array when
Used in 0 other vars

Comments top

No comments for to-array-2d. Log in to add a comment.