This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-06-22
Channels
- # beginners (8)
- # boot (41)
- # cider (1)
- # cljsrn (2)
- # clojure (91)
- # clojure-dev (34)
- # clojure-gamedev (3)
- # clojure-germany (6)
- # clojure-greece (324)
- # clojure-japan (2)
- # clojure-miami (4)
- # clojure-nl (6)
- # clojure-quebec (3)
- # clojure-russia (26)
- # clojure-spec (50)
- # clojure-uk (19)
- # clojurescript (147)
- # core-async (5)
- # css (2)
- # cursive (15)
- # datascript (7)
- # datomic (6)
- # hoplon (1)
- # jobs (4)
- # lein-figwheel (17)
- # off-topic (4)
- # om (52)
- # om-next (10)
- # onyx (1)
- # planck (19)
- # proton (1)
- # re-frame (81)
- # reagent (61)
- # spacemacs (1)
- # specter (46)
- # spirituality-ethics (7)
- # untangled (7)
- # yada (17)
@lellis: Your compare function compares with the values inside demo-map
so you're comparing [nil :c] vs. ["aab" :b]
the maps themselves have the correct comparison function
user=> (identical? (.comparator result) (.comparator demo-map-sorted-by-value))
true
@aengelberg: which priority map?
maps other than clojure's persistent types will run the Object
case of all-transform
protocol (which is the former implementation of ALL
)
@josh.freckleton: Nothing besides what I showed in that talk: https://github.com/nathanmarz/specter-clojure-west/blob/master/src/specter_demo/examples.clj#L466
I work with DAGs so my operators are specific to those
External library. clojure.data.priority-map
PARENTS
, CHILDREN
, TOPSORT
, NODE
, and then a bunch of different ways to navigate to "subgraphs"
subgraph navigators are similar in concept to subset
and submap
, and metadata on the transformed subgraph indicates how to re-attach the edges that were formerly to the prior subgraph
@aengelberg: if it correctly implements empty
I imagine it should work
and IKVReduce
wouldn't be as fast as the other ones though since the default handler does a bunch of other checks beforehand
Is is possible to remove nodes from a tree structure with specter? I can only manage to replace nodes with nil
@odinodin: how are you representing your tree?
(def all-pages
{:pages {:page-id "home",
:options '({:page-id "other_inq"}
{:page-id "other_inq",
:access #{"employee"}})}})
(defn only-authorized [pages access]
(->> pages
(transform*
[(walker #(and (map? %) (contains? % :access)))]
(fn [page] (when (-> page :access (some access)) page)))))
only-authorized returns
{:pages {:page-id "home", :options ({:page-id "other_inq"} nil)}}
my current fix for removing the nil is :
(defn only-authorized [pages access]
(->> pages
(transform*
[(walker #(and (map? %) (contains? % :access)))]
(fn [page] (when (-> page :access (some access)) page)))
(transform*
[(walker #(and (map? %) (contains? % :options)))]
(fn [x] (update x :options (partial remove nil?))))))
well you definitely can't do it with walker
if you make your own recursive path there's a variety of ways of doing it
I saw the next version of specter has a NONE value, what if I could return NONE instead of nil in the transform-function?
@odinodin: I'll show you how to do it
@odinodin: not yet decided whether that feature idea will become reality
your data structure also isn't recursive
this example isn't recursive so it's hard to show you what to do
i think if you change :options to :pages its recursive
(def all-pages
{:pages {:page-id "home",
:options '({:page-id "a" :options ({:page-id "c", :access #{"external"}})}
{:page-id "b", :access #{"employee"}})}})
(def all-pages
{:page-id "root"
:pages [{:page-id "other-page",
:pages '({:page-id "other_inq"}
{:page-id "other_inq",
:access #{"employee"}})}]
})
(declarepath NODES)
(providepath NODES
(stay-then-continue
(must :pages) ALL NODES
))
(setval
[NODES
(must :pages)
(continuous-subseqs :access)
]
[]
all-pages
)
that's one way to do it
modified the example data to be recursive in structure
enjoy 🙂
(transform [(collect MAP-VALS) MAP-VALS] (fn [vs n] (/ n (apply + vs))) (frequencies [1 1 2 2 2 3 4 4 4]))
is there any way to avoid calculating the sum of the values for every individual value as I'm doing there?
@luxbock: like this?
(transform [(collect-one (view count)) (view frequencies) MAP-VALS]
(fn [s n] (/ n s))
[1 1 2 2 2 3 4 4 4]
)
(transform [ALL (view :foo) #(> % 2)]
str
[{:foo 1} {:foo 2} {:foo 3} {:foo 4}])
;; => [1 2 "3" "4"]
@luxbock: (select [ALL :foo #(> % 2) (view str)] [{:foo 1} {:foo 2} {:foo 3} {:foo 4}])
@nathanmarz: in my case :foo
is actually a function that retrieves the value from deeper in the map, which is why I used view
with it