This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-07-08
Channels
- # admin-announcements (1)
- # arachne (3)
- # beginners (17)
- # boot (36)
- # bristol-clojurians (1)
- # cider (4)
- # clara (10)
- # cljsjs (11)
- # cljsrn (20)
- # clojure (134)
- # clojure-austin (2)
- # clojure-boston (1)
- # clojure-czech (1)
- # clojure-greece (128)
- # clojure-norway (1)
- # clojure-romania (1)
- # clojure-russia (17)
- # clojure-spec (106)
- # clojure-sweden (1)
- # clojure-taiwan (1)
- # clojure-uk (41)
- # clojurescript (122)
- # component (4)
- # cursive (1)
- # datomic (34)
- # editors (57)
- # emacs (12)
- # events (5)
- # hoplon (18)
- # instaparse (1)
- # jobs (9)
- # keechma (9)
- # lein-figwheel (3)
- # luminus (1)
- # om (47)
- # onyx (31)
- # proton (2)
- # re-frame (7)
- # reagent (3)
- # rethinkdb (1)
- # specter (25)
- # sql (2)
- # untangled (21)
- # yada (1)
@arijun: How would you convert an arbitrary collection into a map?
If you have a sequence of pairs, you can use (into {} seq-of-pairs)
Ah, then (apply hash-map [k1 v1 k2 v2])
will work.
You can convert an arbitrary collection into a set, but only certain shapes of collection can make a valid map.
If you have an even number of items in your collection, apply hash-map
will work. If you have an odd number of items, you’ll get an error. If you have a collection of pairs, into {}
will work.
(into {} [[k1 v1] [k2 v2]])
it seems weird that there is no symmetric rountrip capability (unlike, say (set (vec s))
) . Worrying about errors doesn't seem sensible since the other methods will also give you an error with malformed input. I feel like it's probably just because the name map
was already taken.
No, it wouldn’t make sense: as shown above, there are two representations that can be used to construct hash maps.
It’s also reflected in reduce
and reduce-kv
— hash maps are "special" collections.
(the latter works on associative collections so it works on vectors too — but you need to understand the abstractions behind the collection types)
Also consider that you can’t round-trip through a set: (vec (set v))
will not necessarily give you the original back.