From 0b7d04bad801f49db9a7c3bf3004184c07fe6843 Mon Sep 17 00:00:00 2001 From: David Nolen Date: Sat, 21 Apr 2012 12:28:37 -0400 Subject: [PATCH] * devnotes/corelib.org: CLJS-16: missing array ops --- devnotes/corelib.org | 12 ++++++------ src/cljs/cljs/core.cljs | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/devnotes/corelib.org b/devnotes/corelib.org index 39da753080..96dd4201fe 100644 --- a/devnotes/corelib.org +++ b/devnotes/corelib.org @@ -198,7 +198,7 @@ For macros only, uses clojure.core version * DONE dotimes * DONE doto * TODO double -* TODO double-array +* DONE double-array * TODO doubles * DONE drop * DONE drop-last @@ -284,7 +284,7 @@ does what? * intern * DONE interpose * DONE into -* TODO into-array +* DONE into-array * ints * io! * DONE isa? @@ -315,12 +315,12 @@ does what? * loaded-libs * locking * DONE long -* TODO long-array +* DONE long-array * TODO longs * DONE loop * macroexpand * macroexpand-1 -* TODO make-array +* DONE make-array * DONE make-hierarchy * DONE map * DONE map-indexed @@ -369,7 +369,7 @@ does what? * TODO num * DONE number? * TODO numerator -* TODO object-array +* DONE object-array * DONE odd? * DONE or * DONE parents @@ -519,7 +519,7 @@ as macro * thread-bound? * DONE time * DONE to-array -* TODO to-array-2d +* DONE to-array-2d * DONE trampoline * transient * DONE tree-seq diff --git a/src/cljs/cljs/core.cljs b/src/cljs/cljs/core.cljs index 39ee5c3a3e..8ce345a1f1 100644 --- a/src/cljs/cljs/core.cljs +++ b/src/cljs/cljs/core.cljs @@ -99,6 +99,20 @@ [array] (.-length array)) +(declare reduce) + +(defn into-array [coll] + (reduce (fn [a x] (.push a x) a) (array) coll)) + +(defn long-array [coll] + (array)) + +(defn double-array [coll] + (array)) + +(defn object-array [coll] + (array)) + ;;;;;;;;;;;;;;;;;;;;;;;;;;; core protocols ;;;;;;;;;;;;; (defprotocol IFn @@ -1417,6 +1431,17 @@ reduces them without incurring seq initialization" (recur (next s))) ary)))) +(defn to-array-2d + "Returns a (potentially-ragged) 2-dimensional array + containing the contents of coll." + [coll] + (let [ret (make-array (count coll))] + (loop [i 0 xs (seq coll)] + (when xs + (aset ret i (to-array (first xs))) + (recur (inc i) (next xs)))) + ret)) + (defn- bounded-count [s n] (loop [s s i n sum 0] (if (and (pos? i)