This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-03-05
Channels
- # bangalore-clj (4)
- # beginners (16)
- # boot (4)
- # cljs-dev (1)
- # cljsrn (2)
- # clojure (177)
- # clojure-italy (2)
- # clojure-nl (1)
- # clojure-russia (41)
- # clojure-spec (3)
- # clojure-uk (21)
- # clojurescript (46)
- # code-art (1)
- # datomic (10)
- # hoplon (125)
- # leiningen (1)
- # luminus (2)
- # lumo (1)
- # off-topic (10)
- # onyx (69)
- # re-frame (22)
- # reagent (4)
- # ring (32)
- # rum (6)
- # specter (2)
- # untangled (5)
Hi there! Anyone care to explain, the differences between list, vector, and map? And when to use them? Thank you so much! :)
@zea Read this and see if it helps: http://clojure-doc.org/articles/language/collections_and_sequences.html
And if you want to understand more under the hood http://insideclojure.org/2016/03/16/collections/
And this might also be good reading https://clojure.org/reference/data_structures#Collections
@zea list and vector are for storing individual things in order. Map is for storing key-value pairs. Maps are not ordered, unless you specifically use an ordered map (usually no need for this). For example, if you want to represent a person, you'd use a map {:name "zea" :id 1}
@kauko so what is the difference between list and vector then? I get the idea of map thou, thanks! @seancorfield thanks for the reference sir!
Mmmh, I guess for now you only need to know that lists and vectors can behave differently when for example you conj
stuff into them.
cljs.user=> (conj `(1 2 3) 4)
(4 1 2 3)
cljs.user=> (conj [1 2 3] 4)
[1 2 3 4]
cljs.user=>
`There may be situations when you should use one over the other, but usually those are perfomance related, which means you don't really need to worry about it you're just learning IMO 🙂
@zea Have you looked at Clojure for the Brave and True? It's a pretty great introduction to the language!
hey I seem to be having an arity problem with multimethods...
(defmulti cast-spell (fn [mage] (:mage-type mage)))
(defmethod cast-spell :summoner
[mage victim]
(str "Summoner mage " (:name mage) " casts a spell on " (:name victim)))
(cast-spell {:mage-type :summoner
:name "Archdeacon Gotham"}
{:name "Poor Victim"})
Results in:
ArityException Wrong number of args (2) passed to: user/eval10235/fn--10236 clojure.lang.AFn.throwArity (AFn.java:429)
if I take out the victim
references in the defmethod (and the 2nd map in the call to cast-spell
it works fine ...
I found some suggestions that this might be due to redefinition of the multimethod but it's the same when I do it in a clean REPL
ok I guess it just takes posting the question to finally figure it out... I changed the arity of the defmulti
to match and it works now. I thought for sure I had tried that before but maybe not in a clean REPL
@zea vector is indexed — so random access is O(1) — list is sequential so accessing the nth element is O(n) /cc @kauko