On Wed, Jan 12, 2011 at 8:15 AM, WoodHacker <[email protected]> wrote:
> This is a much better solution. It's shorter and is easier to read.
> Thanks for tip!
>
> On Jan 10, 11:49 pm, Sunil S Nandihalli <[email protected]>
> wrote:
>> (into-array (map float-array [[1.0 1.0 2.0 2.0] [3.0 2.2 4.0 0.0]]))
Why not generalize further?
(defn arrayify [array-fn coll]
(if (some #(and
(not (instance? String %))
(try (seq %) (catch Exception _))) coll)
(into-array (map #(arrayify array-fn %) coll))
(array-fn coll)))
user=> (arrayify float-array [1.2 0.3 4.0])
[1.2, 0.3, 4.0]
user=> (.getClass (arrayify float-array [1.2 0.3 4.0]))
[F
(My repl prints arrays nicely, like vectors)
user=> (arrayify float-array [[1.2 0.3][-2.1 4.0]])
[[1.2, 0.3],
[-2.1, 4.0]]
user=> (.getClass (arrayify float-array [[1.2 0.3][-2.1 4.0]]))
[[F
user=> (arrayify int-array [[[1 0][2 4]][[3 7][9 -1]]])
[[[1, 0], [2, 4]],
[[3, 7], [9, -1]]]
user=> (.getClass (arrayify int-array [[[1 0][2 4]][[3 7][9 -1]]]))
[[[I
user=> (arrayify (partial into-array String) ["foo" "bar"])
["foo", "bar"]
user=> (.getClass (arrayify (partial into-array String) ["foo" "bar"]))
[Ljava.lang.String;
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en