This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-08-29
Channels
- # aws (1)
- # beginners (78)
- # boot (27)
- # cider (16)
- # clara (15)
- # cljs-dev (84)
- # cljsjs (13)
- # cljsrn (19)
- # clojure (65)
- # clojure-france (10)
- # clojure-italy (8)
- # clojure-russia (35)
- # clojure-spec (34)
- # clojure-uk (124)
- # clojurescript (50)
- # clojutre (3)
- # core-async (16)
- # data-science (18)
- # datascript (1)
- # datomic (9)
- # emacs (2)
- # flambo (3)
- # fulcro (55)
- # graphql (3)
- # hoplon (4)
- # jobs (2)
- # juxt (21)
- # keechma (6)
- # lumo (73)
- # off-topic (4)
- # om (10)
- # onyx (5)
- # parinfer (1)
- # pedestal (3)
- # re-frame (60)
- # reagent (19)
- # specter (24)
I have an ip-pool {"127.0.0.1" {:k v}}
and would like to get first IP where :k
is nil
. How would I do that? I would hope to get a more readable version of (first (first (filter #(nil? (:k (second %))) ip-pool)
@urzds what does "first" mean in this context given that your input is an unsorted map?
i.e. I don't care which one, but I want only one and not more. The result shall be the key, i.e. a string, e.g. "127.0.0.1".
I don't like the regular-Clojure version I wrote above, because it is not immediately clear what the two first
s mean. second
can be deduced, if one knows ip-pool
is a map and how filter
works, but I find the two first
to be not obvious enough.
(def data {"127.0.0.1" {:k 3} "127.0.0.2" {} "127.0.0.3" {:b 2}})
(select-first [ALL (selected? LAST :k nil?) FIRST] data)
if you want all of them just change select-first
to select
it's like (not (empty? (select ...)))
except it runs far more efficiently than that
no materialization of intermediate sequences and stops traversal as soon as it matches something
same as select-first
Guess I should alias LAST to VALUE and FIRST to KEY. That might make it easier to read...
yea, that's easy enough
If I wanted to select the IP address where :k
is 1, I would run (select-first [ALL (selected? LAST :k #(= % 1)) FIRST] data)
?
can also say (pred= 1)
instead of the anonymous function
there's also pred>
, pred<
, pred<=
, and pred>=
just convenience navigators
I find it slightly more readable
Thanks @nathanmarz !