This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-08-20
Channels
- # beginners (110)
- # boot (1)
- # cljs-experience (1)
- # clojure (58)
- # clojure-italy (4)
- # clojure-russia (1)
- # clojure-spec (4)
- # clojurescript (51)
- # cursive (2)
- # datomic (4)
- # devcards (4)
- # dirac (1)
- # figwheel (3)
- # fulcro (46)
- # hoplon (11)
- # om (4)
- # parinfer (7)
- # pedestal (1)
- # re-frame (11)
- # reagent (20)
- # specter (6)
- # unrepl (6)
There was recently a very interesting question on Stack Overflow - no one has written a Specter-based solution yet... I am trying to write one.
@michaelwfogleman I'll take a closer look later, but you basically want to use recursive-path
, collect-one
, and INDEXED-VALS
@michaelwfogleman looks like this:
(defn find-index-route [v data]
(let [p (recursive-path [] p
(if-path sequential?
[INDEXED-VALS
(if-path [LAST (pred= v)]
FIRST
[(collect-one FIRST) LAST p]
)]))]
(let [ret (select-first p data)]
(if (vector? ret) ret [ret])
)))
Mine, w/o specter but works on maps and find "all" routes
(defn find-index-route
"find all routes to some value"
([k x] (find-index-route k x []))
([k x p]
(cond
(= k x) [p]
(coll? x) (->> (if (map? x) x (vec x))
(reduce-kv (fn [acc i v]
(into (vec (find-index-route k v (conj p i))) acc)) []))
:else nil)))
@souenzzo a slight modification makes the specter version work for maps as well:
(defn find-index-route [v data]
(let [p (recursive-path [] p
[(if-path map? ALL INDEXED-VALS)
(if-path [LAST (pred= v)]
FIRST
[(collect-one FIRST) LAST coll? p]
)])]
(let [ret (select-first p data)]
(if (or (nil? ret) (vector? ret)) ret [ret])
)))
@souenzzo a slight modification makes the specter version work for maps as well:
(defn find-index-route [v data]
(let [p (recursive-path [] p
[(if-path map? ALL INDEXED-VALS)
(if-path [LAST (pred= v)]
FIRST
[(collect-one FIRST) LAST coll? p]
)])]
(let [ret (select-first p data)]
(if (or (nil? ret) (vector? ret)) ret [ret])
)))