Fork me on GitHub
#specter
<
2017-12-15
>
jrheard00:12:40

hi, specter rookie here, i’m sure there’s a simple way to do this:

jrheard00:12:53

i have a grid, which is a vector of vectors of values; those values are either numbers or nil

jrheard00:12:17

i’d like to write a specter query that gets me the indexes of all non-nil elements in grid

jrheard00:12:56

i can get the non-nil elements themselves by doing

(select [ALL ALL (comp not nil?)]
                  (@app-state :grid))
which gives me eg (8 2 3 5 7)

jrheard00:12:15

but i’d like instead to be given eg ([5 3] [2 2] [3 1] [6 1] [4 2]) or whatever

jrheard00:12:38

i.e. a list of the locations of those non-nil values in the grid, encoded as x-y pairs

jrheard00:12:46

the vanilla clojure code i have to do this is

(apply concat
         (for [x (range (count grid))]
           (for [y (range (count (grid x)))
                 :when (not (nil? (get-in grid [x y])))]
             [x y]))))
, but i have a comment above it saying “; xxx is there a way to do this in specter”, so here i am 🙂 thanks!

nathanmarz02:12:47

@jrheard like this: (select [INDEXED-VALS (collect-one FIRST) LAST INDEXED-VALS (selected? LAST some?) FIRST] grid)

nathanmarz02:12:19

will be a lot faster than that clojure code too

mbjarland13:12:41

@nathanmarz The dependency chain we discussed yesterday is a step closer to completion, the pull request on suchwow was accepted, I've created a ticket on midje and we're waiting for build to get deployed to clojars for suchwow (https://github.com/marick/Midje/issues/427)

nathanmarz18:12:16

btw, have you tried just overriding the dependency with the latest version? should make the warning go away

mbjarland18:12:31

@nathanmarz no haven’t done that yet and really, it’s just a warning during a test run so not that big a deal to begin with. I’m happy to wait for the transitive chain to percolate up to midje

jrheard22:12:35

thanks! that does give the same results, excellent

jrheard22:12:14

i don’t yet understand what the collecting-values is system is / how it works, will read the docs more closely!

jrheard22:12:00

the README makes it look pretty clear, i think i’ve just gotta play around with specter some more to build up some muscle memory and get to a point where this feels more natural 🙂

nathanmarz22:12:35

@jrheard collected values are a vector that is passed along through navigation

jrheard22:12:08

and the result of the selection is basically (concat collected-values value-you-navigated-to) ?

nathanmarz22:12:52

conj, not concat

jrheard22:12:10

right, i follow

jrheard22:12:13

thanks! 🙂

jrheard22:12:44

thanks again for the help with my grid example, i wouldn’t have come up with that specter code but it makes a lot of sense to me now!