clojure - Does (into) do any higher-level inference to stay idiomatic on output type? -
say have key-value pair i've agnostically defined key-value map:
(def foo {:bar "baz" :bat "squanch"})
it occurs me @ later time set operations on it, i'll need convert relation, clojure cheatsheet says type of set, go ahead , grab (into)
, go it:
(set/project (into #{} foo) [:bar]) #{{}}
huh?
(into #{} foo) #{[:bar "baz"] [:bat "squanch"]}
that's not (project)
expects @ all. according the docs:
;; `project` strips out unwanted key/value pairs set of maps. ;; suppose have these descriptions of cows: user=> (def cows #{ {:name "betsy" :id 33} {:name "panda" :id 34} }) #'user/cows ;; care names. can them this: user=> (project cows [:name]) #{{:name "panda"} {:name "betsy"}}
so close.
should expecting (into)
know mean when convert 1 of these types, or there way? if it's going this, might roll own thing few map/flatten calls, that's i'm trying avoid phrasing things in more elegant language of sets.
for clarity, here example of thought best done (set/project)
seems not possible above expectations:
(defn exclude-keys "filters out argument-provided keys key-value map." [input-map excluded-keys] (-> input-map (select-keys (set/difference (into #{} (keys input-map)) excluded-keys))))
i guess i'm surprised takes syntax accomplish in clojure.
into
conj
s elements of sequence given collection e.g.
(into #{} [:a :b :b :c :a]) => #{:c :b :a}
since maps sequences of pairs end set of pairs in input map.
if want remove collection of keys map can use dissoc
:
(defn exclude-keys "filters out argument-provided keys key-value map." [input-map excluded-keys] (apply dissoc input-map excluded-keys))
Comments
Post a Comment