Question

I am simply trying to rename the map function in Clojure to gain better exposure to arbitrary inputs. The function I wrote has problems with the number of args being passed. Namely,

(defn new-map [f [& x]]
      (map f x))

Moreover, the end goal is to write a mapping function that can handle nested inputs:

Input 1: [inc [[1 2 3] [4 5]] [2 1]]

Output1: [[1 2 3] [5 5]] whereby inc is a specific function f, [[1 2 3] [4 5]] is an array, and [2 1] picks out the second row first element of the array.

Input 2: [inc [[1 2 3] [4 5] [6] [7]] [[1 1] [2 2] [3]]]

Output 2: [[2 2 3] [4 6] [7] [7]]

Was it helpful?

Solution

it sounds like you want reduce with update-in

i.e.

(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]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top