On 29 October 2014 11:01, Roelof Wobben <rwob...@hotmail.com> wrote:
>
> For a exercise I have to add something to the end of a existing map.
>
> So I thought this would work :
>
> (defn add-author [book new-author]
>   (assoc book (conj :authors new-author)))
>

Take a look at that conj expression on its own:

    (conj :authors new-author)

You're trying to conjoin "new-author" onto the keyword :authors, but
keywords aren't collections. That's what the error means. It's saying that
it expected a collection, but you've supplied a keyword instead.

What you want is:

    (conj (get book :authors) new-author)

Which can also be written:

    (conj (book :authors) new-author)

    (conj (:authors book) new-author)

These two expressions are shortcuts for the above "get" function. They work
because maps and keywords can act as functions.

- James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to