Should the Cheat Sheet mention assoc-in for vectors?

79 views
Skip to first unread message

Carl Smotricz

unread,
Aug 6, 2015, 8:29:04 AM8/6/15
to Clojure
Dunno if this is the correct place and way to suggest a correction:

Someone just mentioned to me that (assoc-in) seems to only work for maps, as the Cheat Sheet section on vectors doesn't mention it. I was able to show him otherwise, but maybe this is something the CS should pick up?

Francis Avila

unread,
Aug 6, 2015, 11:17:40 AM8/6/15
to Clojure
assoc-in "works" for vectors in the sense that both vectors and maps implement ILookup (get) and IAssoc (assoc). assoc-in can navigate any collection that understands "get" (including sets!), and add to any collection that understands "assoc".

However, assoc-in will always create maps when it encounters missing keys. This is unlikely to be what you want most of the time when working with a vector structure. Examples:

(assoc-in [:a [:b [:c :d] :e]] [1 1 1] :f)
;=> [:a [:b [:c :f] :e]]
(assoc-in [] [0] :a)
;=> [:a]
(assoc-in [] [1] :a)
;IndexOutOfBoundsException   clojure.lang.PersistentVector.assocN (PersistentVector.java:183)
(assoc-in [] [0 0] :a)
;=> [{0 :a}]
(assoc-in [] [0 0 0] :a)
;=> [{0 {0 :a}}]
(assoc-in [] [0 0 1] :a)
;=> [{0 {1 :a}}]


Andy Fingerhut

unread,
Aug 6, 2015, 1:54:54 PM8/6/15
to clo...@googlegroups.com
I have added assoc-in to the Vectors/'Change' section of the cheat sheet that is available here: http://jafingerhut.github.io

That is where the latest version is published.  The version at clojure.org/cheatsheet gets updated less frequently -- typically a couple of times a year, when there are more significant updates (e.g. a new Clojure version release).

I would not recommend using the cheat sheet to tell you everything that is or is not possible in Clojure.  By design, it is limited in the amount of information it can give you.  The doc strings, and/or trying it out or reading the source code, are better indications of what is possible.

Andy

On Thu, Aug 6, 2015 at 5:29 AM, Carl Smotricz <carl.s...@gmail.com> wrote:
Dunno if this is the correct place and way to suggest a correction:

Someone just mentioned to me that (assoc-in) seems to only work for maps, as the Cheat Sheet section on vectors doesn't mention it. I was able to show him otherwise, but maybe this is something the CS should pick up?

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Carl Smotricz

unread,
Aug 6, 2015, 1:57:44 PM8/6/15
to clo...@googlegroups.com
Thank you both, Francis and Andy! :)

You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/ScfNTwBtS9A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages