Fork me on GitHub
#specter
<
2017-08-29
>
urzds18:08:14

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)

nathanmarz18:08:23

@urzds what does "first" mean in this context given that your input is an unsorted map?

urzds18:08:49

any, but one is enough

urzds18:08:15

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".

urzds18:08:24

I don't like the regular-Clojure version I wrote above, because it is not immediately clear what the two firsts 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.

nathanmarz18:08:28

@urzds

(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)

nathanmarz18:08:55

if you want all of them just change select-first to select

urzds18:08:28

(selected? ...) is basically like (some? (select ...))?

nathanmarz18:08:50

it's like (not (empty? (select ...))) except it runs far more efficiently than that

nathanmarz18:08:22

no materialization of intermediate sequences and stops traversal as soon as it matches something

nathanmarz18:08:29

same as select-first

urzds18:08:01

Guess I should alias LAST to VALUE and FIRST to KEY. That might make it easier to read...

nathanmarz18:08:37

yea, that's easy enough

urzds19:08:32

If I wanted to select the IP address where :k is 1, I would run (select-first [ALL (selected? LAST :k #(= % 1)) FIRST] data) ?

nathanmarz19:08:36

can also say (pred= 1) instead of the anonymous function

urzds19:08:04

pred= is defined by Specter?

nathanmarz19:08:23

there's also pred>, pred<, pred<=, and pred>=

nathanmarz19:08:33

just convenience navigators

nathanmarz19:08:45

I find it slightly more readable