# Why here a arity errror and how to solve it

**URL:** https://clojureverse.org/t/why-here-a-arity-errror-and-how-to-solve-it/7010
**Category:** Beginners
**Created:** [January 9, 2021, 12:08pm UTC](https://clojureverse.org/t/why-here-a-arity-errror-and-how-to-solve-it/7010 "2021-01-09T12:08:25Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![RoelofWobben](https://clojureverse.org/user_avatar/clojureverse.org/roelofwobben/32/3397_2.png) [@RoelofWobben](https://clojureverse.org/u/RoelofWobben)
#### Post date: [January 9, 2021, 12:08pm UTC](https://clojureverse.org/t/why-here-a-arity-errror-and-how-to-solve-it/7010/1 "2021-01-09T12:08:25Z")

</div>

I try to implement my own update-in as challenge from the brave book

so far I have this :

```auto

(def m {:1 {:value 0, :active false}, :2 {:value 0, :active false}})

(defn my-update-in [m keyword number function]
  (function (get-in m [(number keyword) keyword]) ))

(update-in m [:1] assoc :value 1 :active true)

(defn add-ten [number]
    (+ number 10))

(my-update-in p :age add-ten)

```

but now I see a arity error on the last one.  
So how I can I make the code work in both cases

---

<div class="post-metadata">

### Author: ![Phill](https://clojureverse.org/letter_avatar_proxy/v4/letter/p/779978/32.png) [@Phill](https://clojureverse.org/u/Phill)
#### Post date: [January 9, 2021, 12:38pm UTC](https://clojureverse.org/t/why-here-a-arity-errror-and-how-to-solve-it/7010/2 "2021-01-09T12:38:15Z")

</div>

How many parameters does my-up-date-in require? How many parameters did the invocation provide? That’s the arity error.

Also, keep in mind an interesting subtlety of the real update-in, which is, that its return value is **not** the return value of the function that the caller provides!

---

<div class="post-metadata">

### Author: ![RoelofWobben](https://clojureverse.org/user_avatar/clojureverse.org/roelofwobben/32/3397_2.png) [@RoelofWobben](https://clojureverse.org/u/RoelofWobben)
#### Post date: [January 9, 2021, 12:41pm UTC](https://clojureverse.org/t/why-here-a-arity-errror-and-how-to-solve-it/7010/3 "2021-01-09T12:41:46Z")

</div>

I thought that already  
so I cannot make one function which takes both cases

---

<div class="post-metadata">

### Author: ![mvarela](https://clojureverse.org/user_avatar/clojureverse.org/mvarela/32/1478_2.png) [@mvarela](https://clojureverse.org/u/mvarela)
#### Post date: [January 9, 2021, 4:14pm UTC](https://clojureverse.org/t/why-here-a-arity-errror-and-how-to-solve-it/7010/4 "2021-01-09T16:14:08Z")

</div>

Have a look at the variadic functions section here: [https://clojure.org/guides/learn/functions](https://clojure.org/guides/learn/functions)  
Also, as @Phill pointed out, you should return the updated map, not the updated value itself

---

<div class="post-metadata">

### Author: ![RoelofWobben](https://clojureverse.org/user_avatar/clojureverse.org/roelofwobben/32/3397_2.png) [@RoelofWobben](https://clojureverse.org/u/RoelofWobben)
#### Post date: [January 9, 2021, 8:22pm UTC](https://clojureverse.org/t/why-here-a-arity-errror-and-how-to-solve-it/7010/5 "2021-01-09T20:22:40Z")

</div>

Get some help and I think I solved it

```auto
(defn my-update-in [m ks f]
  (let [up (fn up [m ks f]
             (let [k (first ks)
                   r (rest ks)]
               (if (empty? r)
                 (assoc m k (f (get m k )))
                 (assoc m k (up (get m k) r f )))))]
    (up m ks f)))

```

satisfied ??

---

<div class="post-metadata">

### Author: ![mvarela](https://clojureverse.org/user_avatar/clojureverse.org/mvarela/32/1478_2.png) [@mvarela](https://clojureverse.org/u/mvarela)
#### Post date: [January 10, 2021, 8:03am UTC](https://clojureverse.org/t/why-here-a-arity-errror-and-how-to-solve-it/7010/6 "2021-01-10T08:03:02Z")

</div>

It’s almost there:

```
(my-update-in {:a {:b 1}} [:a :b] inc)
;; => {:a {:b 2}}

(my-update-in {:a {:b 1}} [:a :b] + 3)
;; => throws arity exception

(update-in {:a {:b 1}} [:a :b] + 3)
;; => {:a {:b 4}}
```

---

<div class="post-metadata">

### Author: ![RoelofWobben](https://clojureverse.org/user_avatar/clojureverse.org/roelofwobben/32/3397_2.png) [@RoelofWobben](https://clojureverse.org/u/RoelofWobben)
#### Post date: [January 10, 2021, 10:15am UTC](https://clojureverse.org/t/why-here-a-arity-errror-and-how-to-solve-it/7010/7 "2021-01-10T10:15:49Z")

</div>

This better

```auto
  (let [up (fn up [m ks f args]
             (let [k (first ks)
                   r (rest ks)]
               (if (empty? r)
                 (assoc m k (apply f (get m k) args))
                 (assoc m k (up (get m k) r f args)))))]
    (up m ks f args)))

```

---

<div class="post-metadata">

### Author: ![mvarela](https://clojureverse.org/user_avatar/clojureverse.org/mvarela/32/1478_2.png) [@mvarela](https://clojureverse.org/u/mvarela)
#### Post date: [January 11, 2021, 7:22am UTC](https://clojureverse.org/t/why-here-a-arity-errror-and-how-to-solve-it/7010/8 "2021-01-11T07:22:56Z")

</div>

Have you had a look at the varargs section in the link I sent you? `update-in` should take the map, path to update, a function, and any number of arguments to that function.  
What you have there is almost verbatim the `clojure.core/update-in` body, but missing the varargs part.

---

<div class="post-metadata">

### Author: ![RoelofWobben](https://clojureverse.org/user_avatar/clojureverse.org/roelofwobben/32/3397_2.png) [@RoelofWobben](https://clojureverse.org/u/RoelofWobben)
#### Post date: [January 11, 2021, 7:50am UTC](https://clojureverse.org/t/why-here-a-arity-errror-and-how-to-solve-it/7010/9 "2021-01-11T07:50:57Z")

</div>

yes, I have

```auto
(defn infix2 [[first-number operator second-number & r]]
  (if (empty? r)
    (list operator
          (if (list? first-number)
            (infix2 first-number)
            first-number)
          (if (list? second-number)
            (infix2 second-number)
            second-number))
    (if (< (get priorities operator) (get priorities (first r)))
      (list operator first-number (infix2 (conj r second-number)))
      (infix2 (conj r (list operator first-number second-number))))))

```

this one works also on nested ones like `( ((4 - 2) +2) * 3)`

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [July 12, 2021, 7:51pm UTC](https://clojureverse.org/t/why-here-a-arity-errror-and-how-to-solve-it/7010/10 "2021-07-12T19:51:00Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
