Fork me on GitHub
#specter
<
2016-06-22
>
nathanmarz01:06:49

@lellis: Your compare function compares with the values inside demo-map

nathanmarz01:06:04

so you're comparing [nil :c] vs. ["aab" :b]

nathanmarz01:06:13

the maps themselves have the correct comparison function

nathanmarz01:06:32

user=> (identical? (.comparator result) (.comparator demo-map-sorted-by-value))
true

nathanmarz01:06:36

@aengelberg: which priority map?

nathanmarz01:06:41

maps other than clojure's persistent types will run the Object case of all-transform protocol (which is the former implementation of ALL)

nathanmarz01:06:24

I work with DAGs so my operators are specific to those

aengelberg01:06:55

External library. clojure.data.priority-map

nathanmarz01:06:18

PARENTS, CHILDREN, TOPSORT, NODE, and then a bunch of different ways to navigate to "subgraphs"

nathanmarz01:06:16

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

nathanmarz01:06:30

@aengelberg: if it correctly implements empty I imagine it should work

nathanmarz01:06:21

and IKVReduce

nathanmarz01:06:40

wouldn't be as fast as the other ones though since the default handler does a bunch of other checks beforehand

odinodin10:06:00

Is is possible to remove nodes from a tree structure with specter? I can only manage to replace nodes with nil

nathanmarz11:06:42

@odinodin: how are you representing your tree?

odinodin11:06:01

(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)))))

odinodin11:06:26

only-authorized returns

{:pages {:page-id "home", :options ({:page-id "other_inq"} nil)}} 

odinodin11:06:07

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?))))))

nathanmarz11:06:29

well you definitely can't do it with walker

nathanmarz11:06:20

if you make your own recursive path there's a variety of ways of doing it

odinodin11:06:09

I saw the next version of specter has a NONE value, what if I could return NONE instead of nil in the transform-function?

nathanmarz11:06:59

@odinodin: I'll show you how to do it

nathanmarz11:06:10

@odinodin: not yet decided whether that feature idea will become reality

nathanmarz11:06:25

your data structure also isn't recursive

odinodin11:06:42

it is, just simplified the example

nathanmarz11:06:56

this example isn't recursive so it's hard to show you what to do

nathanmarz11:06:43

i think if you change :options to :pages its recursive

odinodin11:06:41

(def all-pages
  {:pages {:page-id "home",
           :options '({:page-id "a" :options ({:page-id "c", :access #{"external"}})}
                       {:page-id "b", :access #{"employee"}})}})

nathanmarz11:06:04

@odinodin:

(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
  )

nathanmarz11:06:09

that's one way to do it

odinodin11:06:30

sweet, thanks 🙂

nathanmarz11:06:42

modified the example data to be recursive in structure

odinodin11:06:29

specter is really awesome, thanks for making it 🙂

nathanmarz11:06:51

enjoy 🙂

luxbock16:06:58

(transform [(collect MAP-VALS) MAP-VALS] (fn [vs n] (/ n (apply + vs))) (frequencies [1 1 2 2 2 3 4 4 4]))

luxbock16:06:50

is there any way to avoid calculating the sum of the values for every individual value as I'm doing there?

luxbock16:06:23

a specter way of doing that I mean

nathanmarz16:06:15

@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]
  )

luxbock16:06:49

ah very nice

luxbock16:06:38

(transform [ALL (view :foo) #(> % 2)]
  str
  [{:foo 1} {:foo 2} {:foo 3} {:foo 4}])
;; => [1 2 "3" "4"]

luxbock16:06:57

how would I do this but only get ["3" "4"] back?

nathanmarz16:06:45

@luxbock: (select [ALL :foo #(> % 2) (view str)] [{:foo 1} {:foo 2} {:foo 3} {:foo 4}])

luxbock17:06:18

@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

luxbock17:06:32

ah, but it looks like it actually works if I just wrap it in view and then use select