 
 (extend-protocol mp/PIndexedAccess
   NDArray
-  (get-1d [m ^long row]
+  (get-1d [m row]
     (with-check-shape
       1 [m m] row
       (.get m row)))
-  (get-2d [m ^long row ^long col]
+  (get-2d [m row col]
     (with-check-shape
       2 [m m] row col
       (.get m row col)))

                    (dec ndims) ", but got " dimension-number "instead!"))
        (aget shape dimension-number)))))

(defn all? 
  ([coll]
   (every? true? coll))
  ([f coll]
   (every? true? (mapv f coll))))

(defn in-bound? [^long idx ^long len]
  (and (>= idx 0) (< idx len)))

(defmacro with-check-indices [ndims m & args]
   (let [row (gensym "row_")
         col (gensym "col_")
         indexes (gensym "indexes_")
         check-clause
         (cond (= ndims 1)
               `(and (>= ~row 0)
                     (< ~row (first (mp/get-shape ~m))))
               (= ndims 2)
               `(let [shape# (mp/get-shape ~m)]
                 (and (>= ~row 0)
                      (< ~row (first shape#))
                      (>= ~col 0)
                      (< ~col (second shape#))))
               :else
               `(let [shape# (mp/get-shape ~m)] 
                  (all? (map-indexed
                          (fn [dim# idx#]
                            (and (>= idx# 0)
                                 (< idx# (nth shape# dim#))))
                          ~indexes))))
         body
         (cond (= ndims 2)
               (next (next args)) 


(extend-protocol mp/PIndexedSettingMutable
  NDArray
  (set-1d! [m row ^double v]
    (with-check-shape
      1 [m m] row
      (.set m row v)))
  (set-2d! [m row col ^double v]
    (with-check-shape
      2 [m m] row col
      (.set m row col v)))
  (set-nd! [m indexes ^double v]
    (with-check-shape
      (count indexes) [m m] indexes
      (.set m (long-array indexes) v))))
