我只是想在Clojure中重命名地图功能,以便更好地接触任意输入。我写的函数有传递的args数量的问题。即,
(defn new-map [f [& x]]
(map f x))
此外,最终目标是编写一个可以处理嵌套输入的映射函数:
输入1:[inc [[1 2 3] [4 5]] [2 1]]
输出1:[[1 2 3] [5 5]]其中inc是特定函数f,[[1 2 3] [4 5]]是一个数组,[2 1]首先选出第二行数组的元素。
输入2:[inc [[1 2 3] [4 5] [6] [7]] [[1 1] [2 2] [3]]]
输出2:[[2 2 3] [4 6] [7] [7]]
答案 0 :(得分:1)
听起来你希望reduce
与update-in
即
(defn new-map [f nested-vecs indices]
(reduce (fn [nv idxs] (update-in nv idxs f))
nested-vecs
indices))
;; I changed your inputs. Remember that indices start from 0
(new-map inc [[1 2 3] [4 5] [6] [7]] [[0 0] [1 1] [2 0]])
; => [[2 2 3] [4 6] [7] [7]]