This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-03-17
Channels
- # bangalore-clj (1)
- # beginners (23)
- # boot (141)
- # cider (68)
- # cljs-dev (29)
- # cljsjs (1)
- # cljsrn (11)
- # clojure (150)
- # clojure-austin (3)
- # clojure-berlin (1)
- # clojure-france (2)
- # clojure-greece (13)
- # clojure-italy (5)
- # clojure-russia (49)
- # clojure-spec (15)
- # clojure-uk (45)
- # clojurescript (152)
- # code-art (1)
- # core-async (75)
- # cursive (12)
- # datascript (2)
- # datomic (90)
- # dirac (5)
- # emacs (10)
- # garden (1)
- # hoplon (52)
- # instaparse (4)
- # juxt (2)
- # lein-figwheel (2)
- # lumo (47)
- # mount (94)
- # off-topic (20)
- # om (21)
- # onyx (14)
- # parinfer (19)
- # pedestal (3)
- # protorepl (13)
- # re-frame (5)
- # reagent (20)
- # slack-help (10)
- # spacemacs (8)
- # specter (57)
- # unrepl (11)
- # untangled (3)
- # vim (1)
- # yada (1)
What is this
in (defnav a [x] (select* [this structure next-fn] ...))
I tried to use it but "Unable to resolve symbol this".
@oskarkv that's currently ignored
just there for consistency with how method definition works in clojure
if you use defrichnav
, then this
refers to the navigator object
hi would transformed
be able to receive a value from above its scope? Like:
(transform
[VAL
(transformed
[some-path]
(fn [value-from-VAL path-from-transformed]
(do-something)))]
(fn [value-from-VAL value-from-transformed]
(do-another-thing)))
@levitanong no, I opted to design it so the values for transformed
are reset
@nathanmarz So if within a transform
I use a transformed
followed by some navigator, the “cursor” that navigator will operate on will be the original shape of the data passed to transform
?
i.e. as if i had used a collector
without any collecting going on
no, it just ignores collected values
(transform [ALL (transformed STAY inc)] inc [1 2 3 4])
will emit [3 4 5 6]
hmmm.
I think i have to parse this. lol
thanks for the replies, @nathanmarz
(transform [(transformed [VAL :key ALL] (fn [a b] (inc b)) VAL even?]
(fn [a b] (println a))
{:key [1 2 3 4]})
In this above example, I expect a to be [2 3 4 5]
, but I seem to be getting {:key [2 3 4 5]}
At least, this is a simplified version of what I’m trying to do. Is this expected behavior?@levitanong that code produces an error
since it's trying to incrementing a vector in the transformed
clause
@nathanmarz woops, fixed
@levitanong still erroring
now because it's calling even?
on the map
but anyway, the navigator after the transformed
clause will get {:key [2 3 4 5]}
as input
@nathanmarz so it doesn’t actually navigate to the transformed values
correct
it works just like view
oh, must’ve misread the docs on transformed
i was almost certain it said “navigates to the transformed values” or somesuch.
Anyway, thanks! I am no longer confused 😄
I've been trying to use Specter to return paths of keys from embedded maps. e.g. amalloy's answer at http://stackoverflow.com/questions/21768802/how-can-i-get-the-nested-keys-of-a-map-in-clojure :
(defn keys-in [m]
(if (or (not (map? m))
(empty? m))
'(())
(for [[k v] m
subkey (keys-in v)]
(cons k subkey))))
I have been trying everything, turning myself inside-out. Probably used combinations of half of the items on the navigators page.
Is this possible in Specter? Undoubtedly yes. Is it possible more simply than without it, though? I suspect so, but I don't yet have the insight to see how.
@mars0i you can do that with value collection
one sec
I have experimented with VAL
, collect
, collect-one
, but haven't figured out the magic configuration.
(defn keys-in [m]
(let [p (recursive-path [] p
(if-path map?
[ALL (collect-one FIRST) LAST p]
STAY
))]
(map butlast (select p m))
))
that's one way
here's another way:
(defn keys-in [m]
(let [p (recursive-path [] p
(if-path map?
[ALL
(if-path [LAST map?]
[(collect-one FIRST) LAST p]
FIRST
)]))]
(select p m)
))
more efficient
I'd like to have this added to that StackOverflow question. I'll do it and credit you, if you want, but if you play SO and have time, you could get the points.
go for it
OK, thanks much. I think there ought to be a Specter solution there. I'll add both. I saw a discussion elsewhere someone trying to do this with Specter and gave up.
once your brain adapts to thinking in terms of navigation this is easy stuff
i.e. the discussion elsewhere someone else gave up. So it's not just me who thought of using Specter for this kind of thing.
@mars0i also worth noting that the specter solution should be significantly more efficient than all the solutions on that page
except for the amalloy one which is probably comparable
I was wondering about that. I'll run the specter defs through Criterium later. I'm using very small structures, so differences might not show up.
At some point I might randomly generate some large embedded maps just to see how the algorithms perform, but I don't have a need for large ones.
@mars0i you should see a substantial difference