On 29 October 2014 11:20, Roelof Wobben <[email protected]> wrote:
> Thanks James,
>
> But how do I use assoc with it
>
> I tried this :
>
> (defn add-author [book new-author]
> (assoc book (conj (book :authors) new-author)))
>
> but then I see this message :
>
>
> ArityException Wrong number of args (2) passed to: core$assoc
> clojure.lang.AFn.throwArity (AFn.java:437)
>
Right, so take a look at that error message. It says you've got the wrong
number of arguments for assoc.
Your code has:
(assoc book (conj (book :authors) new-author))
So your code is passing two arguments to assoc:
(let [new-authors-list (conj (book :authors) new-author)]
(assoc book new-authors-list))
Clojure says that's wrong, because assoc takes at least three arguments.
Take a look at: http://clojuredocs.org/clojure.core/assoc
So the way it's supposed to look is:
(assoc map key value)
You have your map (book), and you have your value (new-authors-list), but
you haven't specified a key. Your code should look like:
(assoc book :authors (conj (book :authors) new-author))
Incidentally, there's also a shortcut for forms like this:
(update-in book [:authors] conj new-author)
- James
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.