Fork me on GitHub
#meander
<
2022-08-22
>
nonrecursive13:08:59

hello! I am drinking the meander koolaid and wondering how to do this transform?

;; input
{:a {:m {}
     :n {}}
 :b {:m {}
     :o {}}}

;; output
[[:a :m]
 [:a :n]
 [:a :m :n]
 [:b :m]
 [:b :o]
 [:b :m :o]]

jgdavey14:08:38

Does this need to be arbitrarily deep? Or for only the first level of keys?

nonrecursive14:08:25

only for the first level of keys

nonrecursive14:08:56

I found the cookbook and it looks like {& (m/seqable [!ks !vs] …)} might help here

jgdavey14:08:44

Something like this is almost right:

(m/rewrite {:a {:m {}
                :n {}}
            :b {:m {}
                :o {}}}
  [?k (m/and (m/seqable !v ...) ?v2)]
  [[?k !v] ... [?k & ?v2]]

  (m/and {} (m/gather [!k (m/app keys !v)]))
  [(m/cata [!k !v]) ...])

jgdavey14:08:30

The trick, I think, is using m/cata to “recurse” with k and its set of keys, then doing 2 different things with those keys in the other patter

jgdavey14:08:34

It’s possible it could all be done in the one top-level map pattern, but my brain started to get cloudy.

jgdavey14:08:05

This isn’t quite right, though, as its got an extra layer of vector nesting per top-level key, and you didn’t want that

nonrecursive14:08:58

oh wow this is so close though, I really appreciate it

jgdavey14:08:57

Another caveat is that m/gather acts like filter, so may or may not be what you want

nonrecursive14:08:04

oh interesting, I think that’s OK here

nonrecursive15:08:06

I was able to flatten it with m/app into:

(m/rewrite {:a {:m {}
                :n {}}
            :b {:m {}
                :o {}}}

  [?k (m/and (m/seqable !v ...) ?v2)]
  [[?k !v] ... [?k & ?v2]]

  (m/and {} (m/gather [!k (m/app keys !v)]))
  (m/app into (m/cata [!k !v]) ...))

Richie16:08:47

I don’t understand the ask. How do I know that it should be :a :m :n and not :a :n :m?

Richie17:08:22

I mean since maps are unordered.

nonrecursive17:08:42

good catch, the order is not essential to the solution here

Richie17:08:19

What would it look like if there are multiple entries in the inner map?

nonrecursive17:08:11

which inner map?

Richie17:08:40

Does {:a {:b {} :c {} :d {} :e {}} produce [:a :b :c] as one of the results or just [[:a :b] [:a :c] [:a d] [:a :e] [:a :b :c :d :e]] ?

Richie17:08:10

I don't understand the transformation from just the one example.