Fork me on GitHub
#specter
<
2017-09-20
>
michaelwfogleman13:09:07

@nathanmarz Thanks for the example!

michaelwfogleman13:09:24

The overview is helpful too... I've not really used zippers before

michaelwfogleman13:09:49

For the part you don't think I should include - are you saying the example zipper?

michaelwfogleman13:09:52

"Let's also define a sample zipper that we can work with in the examples below.

(def data [1 [[2 3 4] 5 6] 7 [8 9]])
(def data-zipper (zip/vector-zip data))
"

michaelwfogleman13:09:06

I added the example of NODE-SEQ that you gave me, as well as a few others to compare to selecting and also using NODE instead

nathanmarz13:09:19

@michaelwfogleman yea, I wouldn't include data-zipper

nathanmarz13:09:34

that should all be encapsulated in the paths

michaelwfogleman13:09:08

OK, I think I've got what you are saying! 🙂

michaelwfogleman13:09:19

Cool, just pushed a new version - let me know if that's not what you had in mind, or you see anything else 🙂

nathanmarz13:09:16

I still see zipper data structures printed out as results to the examples

nathanmarz13:09:42

like for (S/select-any [SZ/VECTOR-ZIP SZ/DOWN] data)

michaelwfogleman13:09:57

Got it, I see what you're saying now, that does make it a lot neater. I pushed a new version and also added your comment about the three steps - I can remove that but I found what you said useful 🙂

pataprogramming20:09:28

I’m wrestling with figuring out recursive paths. Specifically, I’m processing and transforming some JSON-LD. I’d like to perform some transformations at any level of the tree, with arbitrary nesting. To simplify, consider a map where keys are primitive strings and values can be a primitive, a nested map, or a vector of nested maps. A standard transformation might be: if a map has a key jkl:mno equal to wibble, add a new key jkl:123 equal to whammy. Here’s a test map:

{"abc:def": [{"abc:ghi": {"jkl:mno": "womble",
                          "jkl:qrs": 12345}},
             {"abc:tuv": {"jkl:mno": "wibble",
                          "abc:def": {"abc:ghi": [{"jkl:mno": "wibble",
                                                   "jkl:qrs": 0 },
                                                  {"jkl:qrs": 20}]}}}],
 "abc:wxy": {"abc:def": {"jkl:mno": "wibble"}}}

pataprogramming20:09:47

walker terminates as soon as it matches, so it’s clear a recursive path is needed. Is recursive-path the right one to be using, or is this a job for a extend-protocolpath providing implementations for vectors and maps?

pataprogramming20:09:10

Some of the maps are sorted-maps, as well, to influence the serialization when the JSON is written out, so just providing an implementation for clojure.lang.PersistentHashMap won’t cut it in the latter case.

nathanmarz20:09:54

I'll show you how

nathanmarz21:09:21

(def data
  {"abc:def" [{"abc:ghi" {"jkl:mno" "womble",
                          "jkl:qrs" 12345}},
               {"abc:tuv" {"jkl:mno" "wibble",
                            "abc:def" {"abc:ghi" [{"jkl:mno" "wibble",
                                                     "jkl:qrs" 0 },
                                                    {"jkl:qrs" 20}]}}}],
   "abc:wxy" {"abc:def" {"jkl:mno" "wibble"}}})


(def MY-WALKER
  (recursive-path [] p
    (continue-then-stay
      (cond-path
        map? [MAP-VALS p]
        sequential? [ALL p]
        ))))

(setval [MY-WALKER map? (selected? (keypath "jkl:mno") (pred= "wibble")) (keypath "jkl:123")] "whammy" data)

pataprogramming21:09:41

@nathanmarz: Oh, great, thanks! cond-path is what I didn’t know I was groping for. And that example also helps make it clear how to use self-sym, which I wasn’t quite grokking.

pataprogramming21:09:40

Major kudos for specter, by the way. It’s a brilliant library.

nathanmarz21:09:13

happy to hear you're getting good use out of it