This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-06-03
Channels
- # admin-announcements (2)
- # alda (4)
- # beginners (15)
- # boot (89)
- # cljs-dev (88)
- # cljsrn (75)
- # clojure (149)
- # clojure-belgium (16)
- # clojure-france (2)
- # clojure-greece (6)
- # clojure-russia (108)
- # clojure-spec (39)
- # clojure-taiwan (3)
- # clojure-uk (7)
- # clojurescript (70)
- # css (3)
- # cursive (17)
- # data-science (2)
- # datascript (7)
- # datomic (41)
- # dirac (3)
- # hoplon (12)
- # instaparse (1)
- # juxt (3)
- # lambdaisland (9)
- # mount (4)
- # off-topic (6)
- # om (71)
- # om-next (4)
- # onyx (22)
- # other-languages (56)
- # perun (15)
- # proton (6)
- # re-frame (32)
- # reagent (42)
- # specter (34)
- # spirituality-ethics (7)
- # tmp-json-parsing (5)
- # untangled (13)
- # vim (4)
- # yada (6)
@dmbennett: this helped me: https://en.wikibooks.org/w/index.php?title=Clojure_Programming/Examples/API_Examples/Function_Tools&action=edit&section=4
Hello everyone. How would you filter a vector, removing those elements that did not satisfy specific predicates? Example: given vector v,
(def v [:odd :even :1 :2 :3])
, I need to define function f such that: (f v 1) -> [:odd :1]
, (f v 2) -> [:even :2]
, (f v 99) -> [:odd]
, (f v 0) -> [:even]
, etc.So you have some function that transforms the elements in the vector into actual predicates, I assume?
I was thinking of, for each keyword in v, defining a matches-item predicate function indeed.
Let’s call that ->pred
, then f
would be (defn f [pred-vec value] (filterv #((->pred %) value) pred-vec))
<digesting this>
Are you thinking of something like this?
(defn ->pred [kw] (case kw :odd odd? :even even? :1 #(= 1 %) ...
Wow. It actually works. Thanks a lot for this!
I was thinking of ->pred
just being a hash map but, yeah.
Other question: given a map, what is the simplest way to reset all its values to, say, 0?
(defn cleared [m] (into {} (for ([k v] m] (assoc k 0))))
maybe?
(defn cleared [m] (zipmap (keys m) (repeat 0)))
seems to work.so simple seems sinful. 🙂