SultanLegend
SultanLegend

Reputation: 555

Why does clojure give this arity error

I have the function definition below for map-edit

(def map-edit
  (fn [m lst k f]
    (if (car lst)
      (assoc m
             (car lst)
             (map-edit (get m (car lst) {}) k f))
      (assoc m k (f (get m k))))))

When I try to call this function in my repl

(map-edit {} (list "oeu") "oeuoeu" (fn [q] "oeu"))

I get an error for Arity

ArityException Wrong number of args (3) passed to: core/map-edit  clojure.lang.AFn.throwArity (AFn.java:429)

Why does it think I am only passing 3 arguments?

; CIDER 0.8.2 (Java 1.8.0_121, Clojure 1.8.0, nREPL 0.2.12)

Upvotes: 2

Views: 1057

Answers (2)

SultanLegend
SultanLegend

Reputation: 555

Assuming that you have these definitions

(def car first)
(def cdr rest)

The recursive call to map-edit only uses 3 arguments

The line probably should be

(map-edit (get m (car lst) {}) (cdr lst) k f))

Upvotes: 6

Thumbnail
Thumbnail

Reputation: 13483

Note

Assuming that @SultanLegend's answer is correct (not accepted as I write), the core update-in does what you require. You could define ...

(defn map-edit [m lst k f]
  (update-in m (conj (vec lst) k) f))

(map-edit {} (list "oeu") "oeuoeu" (fn [q] "oeu"))
=> {"oeu" {"oeuoeu" "oeu"}}

Upvotes: 0

Related Questions