This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-01-10
Channels
- # beginners (97)
- # boot (77)
- # cider (7)
- # cljs-dev (47)
- # cljsrn (3)
- # clojure (125)
- # clojure-austin (5)
- # clojure-dusseldorf (1)
- # clojure-italy (4)
- # clojure-russia (91)
- # clojure-spec (80)
- # clojure-uk (54)
- # clojurescript (92)
- # core-async (6)
- # cursive (17)
- # datomic (56)
- # hoplon (7)
- # immutant (3)
- # liberator (3)
- # luminus (4)
- # off-topic (26)
- # om (41)
- # om-next (11)
- # pedestal (3)
- # perun (3)
- # protorepl (25)
- # re-frame (32)
- # reagent (33)
- # ring (46)
- # rum (3)
- # spacemacs (5)
- # specter (82)
- # test-check (16)
- # untangled (8)
- # yada (26)
maybe FAQ π is there a way in specter to stop to the first MAP-VALS
, without further going deep in a map for instance?
is there a way to avoid the all caps? On a kinesis advantage pro keyboard, typing all caps is quite annoying
@qqq you can do (def my-all ALL)
if you want
@richiardiandrea no, not at the moment
keywords extend ImplicitNav
in order to wrap themselves in keypath
https://github.com/nathanmarz/specter/blob/master/src/clj/com/rpl/specter.cljc#L896
@richiardiandrea fyi, there are some ideas about supporting that in the future though: https://github.com/nathanmarz/specter/issues/125
oh cool, for a moment I though I could use STOP
for it
btw I tried 0.13.3-SNAPSHOT
and the NONE
syntax works here, but because it navigates deeply in the data, it is still something different from what I was thinking
what do you mean?
example?
uhm...no ok I lost it, I tried another example but it works fine, give a couple of minutes π
uhm (2), I tried again what I had in mind and it works fine...
It was (sp/setval [sp/MAP-VALS #(map? %)] sp/NONE {:a 3 :b 5 :c {:d 5}})
very convenient btw π
@nathanmarz every time I turn to Specter for data manipulation, I surprise myself with the simplicity of the result, thanks:
(sp/transform [(sp/collect-one [:family (sp/submap [:name :description])])
:family
(sp/submap [:resource :relationship])
sp/MAP-VALS
sp/ALL]
(fn [family m]
(assoc m :family family))
(first families))
the collect feature is super neat
yea I use that feature quite a bit
I wanted basically to avoid using a submap
with keys there
so that I can assoc to all the key-values where the value is a map
oh ok wow done π
(defn propagate-family
[family]
(sp/transform [(sp/collect-one [:family (sp/submap [:name :description])])
:family
sp/MAP-VALS
#(vector? %)
sp/ALL]
(fn [family m]
(assoc m :family family))
family))
http://stackoverflow.com/questions/39123457/how-to-use-specter-in-clojurescript <-- is using specter in cljs in figwheel still a problem? (I'm having problems using specter in cljs, but not in clj, and I can't figure out why)
@qqq I've only used it in Node myself
what specific problem are you having?
@richiardiandrea fyi you can write that in a simpler way like this:
(defn propagate-family
[family]
(sp/transform [:family
(sp/collect-one (sp/submap [:name :description]))
sp/MAP-VALS
vector?
sp/ALL
:family]
(fn [family _] family)
family))
Thanks @nathanmarz, is transform
acting like update
then?
Did not know that, even better π
@nathanmarz: let me create a minimal failure care
@richiardiandrea yea it's like update
on steroids
@nathanmarz : from a newcomer perspective, it might help if at the very top of the README.md, there was a line to the effect of transform takes two functions, one to select items, one to update, and leaves everything else the samespe
Super!
yea the documentation could be a lot better
a good beginner tutorial would be great, but I don't have time for that now
would love documentation contributions
@nathanmarz is there a more elegant way to do this:
(defn normalize-family
[family]
(merge
(sp/setval [sp/MAP-VALS string?] sp/NONE (:family family))
(sp/transform [(sp/collect-one [:family (sp/submap [:name :description])])
:family]
(fn [family _] family)
family)))
the initial shape is {:family {:name "art" :desc "sda" :complex-map {....}}
and I basically want to transform it into {:family {:name "art" :desc "sda"} :complex-map {....}}
you just want to move :complex-map keypair into parent map?
ah yes that's another way to see it π
but all the :complex-map
s, so everything that has not a string?
value should go up
I think the way you're doing it is fine
@nathanmarz cool then thanks!
in specter, is it possible for a update function to depend not only on the value, but also on the "path" down to the node ?
@qqq I think there is a https://github.com/nathanmarz/specter/wiki/List-of-Navigators#if-path but I guess depends a lot on what you have in mind
@qqq also https://github.com/nathanmarz/specter/wiki/List-of-Macros#multi-transform
{:a {:b [:apple :orange :pear]
:c [:juice]}
:e [:cat :dog]}
to
{:a {:b [[[:a :b 0] :apple]
[[:a :b 1] :orange]
[[:a :b 2] :pear]]
:c [[[:a :c 0] :juice]]}
:e [[[:e 0] :cat]
[[:e 1] :dog]
]}
^^ can specter do the above transform ? I want to map over the leafs, and for each leaf, also store into it its 'path'that's an interesting thought. what would you want to receive if it went through a navigator like a predicate? #(.startsWith % ":or") or similar?
@qqq you can collect values as you go
nathanmarz: does this involve (1) making the 'update' function impure', or (2) does specter support the reader monad?
@qqq like this:
(defnav ALL-INDEXED []
(select* [this structure next-fn]
;; fill this in
)
(transform* [this structure next-fn]
(map-indexed
(fn [i v]
(second (next-fn [i v])))
structure
)))
(def MyPath
(recursive-path [] p
(cond-path map? [ALL (collect-one FIRST) LAST p]
vector? [ALL-INDEXED (collect-one FIRST) LAST p]
STAY STAY)))
(transform MyPath
(fn [& vals] vals)
{:a {:b [:apple :orange :pear]
:c [:juice]}
:e [:cat :dog]})
it's pure
more work is needed on ALL-INDEXED
to make it preserve type (ala ALL
), but that's the gist of it
(defn every-nth [n] [ALL-INDEXED (collect-one FIRST) (fn [[i _]] (= 0 (mod i n))) LAST])
just do this:
(defn every-nth [n] (path ALL-INDEXED (collect-one FIRST) (fn [[i _]] (= 0 (mod i n))) LAST))
path
implements all the inline caching stuff
also, the macros wiki page mentioned a must-cache-paths!
fn, but it seems like that function is gone?
yea that's gone as of 0.13.0
every path can be inline cached now
itβs really a whole new paradigm to use Specter, I feel kinda lost just like I did when I went from imperative to functional programming
you'll get there
eventually you'll wonder how you ever programmed without it