This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-02-22
Channels
- # aws-lambda (2)
- # beginners (195)
- # boot (47)
- # capetown (14)
- # cljs-dev (7)
- # cljsjs (1)
- # cljsrn (1)
- # clojure (103)
- # clojure-berlin (28)
- # clojure-dev (92)
- # clojure-dusseldorf (3)
- # clojure-finland (2)
- # clojure-germany (3)
- # clojure-italy (4)
- # clojure-russia (37)
- # clojure-spec (104)
- # clojure-uk (52)
- # clojured (2)
- # clojurescript (124)
- # community-development (7)
- # core-async (6)
- # cursive (41)
- # datomic (53)
- # dirac (2)
- # emacs (16)
- # hoplon (5)
- # jobs (3)
- # juxt (12)
- # lein-figwheel (6)
- # leiningen (15)
- # luminus (3)
- # off-topic (49)
- # om (5)
- # onyx (13)
- # overtone (27)
- # re-frame (7)
- # reagent (46)
- # ring (3)
- # ring-swagger (11)
- # spacemacs (2)
- # specter (40)
- # sql (6)
- # untangled (149)
- # vim (14)
I am trying to tranform some "Hiccup-y" style data that will be compiled by Garden into CSS. Ex: [:h1 :h2 {:font-weight "none"}] [:body {:font-size "16px"}]
But it seem like the Specter functions expect maps. How can I get these vectors into something that Specter can see better?
ps. Thank you for Specter. I'm coming from front end and dont know much about typical database interactions, But Specter + nested EDN just seems to make sense 😅 I'm trying to use it in cljs client with persistance to Firebase JSON tree.
@chromalchemy specter works on all data structures
what are you trying to do?
It might be a lark, but since CSS uses a form of navigators, and Garden follows suit to compile Hiccup style clojure data into CSS, I could take a super-structure of ready-to-compile Garden data, and transform or filter it as needed with Specter for more specific application of the style data. Kind of like querying a style database dynamically, instead of declaring static css rules/paths directly.
Im not sure how to navigate the Garden vectors though. They arent exactly maps (though there are nested maps) I cant even get the first key-val out of a vector.
=> (def html [[:h1 :h2 {:font-weight "none"}] [:body {:font-size "16px"}]])
=> (transform [(walker :font-size) :font-size] #(s/replace % #"px" "em") html)
[[:h1 :h2 {:font-weight "none"}] [:body {:font-size "16em"}]]
your examples are just data structures: i.e. maps, vectors, sets, etc.. or not?would probably be better if you provided an example of a data structure (Garden, ..) + what you need to do with it
Yes, just data. Primarily a list of vectors, each vector is a css rule. I will try out walker. Not familiar with that one yet.
[:myclass {:color "red}] How can I get the map from the :myclass key?
you mean the element to the right of :myclass
in the vector?
you can do it with com.rpl.specter.zipper
navigate to the position of :myclass
, and then do z/RIGHT
I would really question why you store data like that instead of just using a map
Ok, How do I navigate to position of :myclass?
user=> (setval [z/VECTOR-ZIP (z/find-first #(= :myclass %)) z/RIGHT z/NODE :color] "blue" [:myclass {:color "red"}])
[:myclass {:color "blue"}]
https://github.com/nathanmarz/specter/blob/master/src/clj/com/rpl/specter/zipper.cljc#L120
Your probably right. The style data super-structure should probably just be maps. I was trying to keep it in the format that Garden wants so I dont have to do more syntax-oriented transformations to be able to compile the results to css
Thanks for that!
[[:class1 {foo}] [:class2 {bar}]] I assume I can use something similar to get to bar by filtering on :class2
find-first
will descend into nested vectors, so the code is pretty much exactly the same
or in [:class1 :class2 {foo}], getting from class1 to class2 to foo?
Ok cool. I came to Specter after hearing about the challenges of using zippers. But glad that it's all included :)
zippers are just an advanced form of navigation
this happens to be a case where it fits
Thanks for the help
On a side note, do you have any best practices on size of nested data in a cljs client. Like could i serialize a one-tree to-do datastructure, and transact on it in the client with atoms. Is this reasonable or am i asking for trouble? especially if there are thousands of items & notes & metadata (i have a lot to do lol)
no idea, I don't do much clojurescript
better to ask in #clojurescript channel
@chromalchemy i have a largish cljs (re-frame) app with a single atom for all client state - there are commonly many thousands of items in the tree and it's fine
Cool. good to know. Tree soup it is.
Hello folks! Can anyone help me implement a deep filter? I'm looking for the following transformation:
;; Drop all maps from the vector in :stuff where :c key is present
{:fluff 42, :stuff [{:a 1, :b 2} {:a 1, :c 3} {:b 2, :c 3} {:b 2, :d 4}]}
=>
{:fluff 42, :stuff [{:a 1, :b 2} {:b 2, :d 4}]}
I also discovered NONE
, but the example from Github doesn't work correctly for me (see the impl/NONE):
(setval [:a ALL nil?] NONE {:a [1 2 nil 3 nil]})
=>
{:a [1 2 :com.rpl.specter.impl/NONE 3 :com.rpl.specter.impl/NONE]}
Worked with 0.13.0-SNAPSHOT
:
(setval [:stuff ALL (pred :c)] NONE {:fluff 42, :stuff [{:a 1, :b 2} {:a 1, :c 3} {:b 2, :c 3} {:b 2, :d 4}]})
=>
{:fluff 42, :stuff [{:a 1, :b 2} {:b 2, :d 4}]}
Thanks for help! 😄@alexyakushev more precise to do (selected? (must :c))
rather than (pred :c)
Thanks! What is the case when they will work differently?
(pred :c)
will filter out maps with :c
being false or nil
Oh, I see. must
is like contains?
. I get it now, thanks!
must
is keypath
that navigates only if the key is present