How do I create a primitive two-dimensional (2d) array of doubles in Clojure?

后端 未结 2 1459
Happy的楠姐
Happy的楠姐 2020-12-09 09:28

A Java API I am Clojure interoping with requires that I pass it a 2d array of doubles; double[][]. How do I create a primitive 2d array of doubles in Clojur

相关标签:
2条回答
  • 2020-12-09 10:22

    Try this:

    (into-array (map double-array [[1 2] [3 4]]))
    
    0 讨论(0)
  • 2020-12-09 10:27

    Try this:

    (defn double-array-2d [coll]
      (let [w (count coll)
            h (apply max (map count coll))
            arr (make-array Double/TYPE w h)]
        (doseq [x (range w)
                y (range h)]
          (aset arr x y (double (get-in coll [x y]))))
        arr))
    
    0 讨论(0)
提交回复
热议问题