Fork me on GitHub
#meander
<
2022-10-02
>
prnc16:10:07

Hello, trying out meander for data exploration Looking at an example like this…

(m/search {:foo {:bar :target}}
    (m/$ :target)
    :found)
I was wondering if it’s possible to get the parent collection of :target without knowing the concrete type of that collection i.e. capture the immediately surrounding ‘context’ of the match? In this particular case that would mean getting out {:bar :target}

timothypratley21:10:59

Hi 👋 The https://github.com/noprompt/meander/blob/epsilon/doc/operator-overview.md#subtree-search- indicate that you can capture the context like this:

(m/search {:foo {:bar :target}}
          (m/$ ?context :target)
          (?context :target))
=> ({:foo {:bar :target-replacement}})
But this isn't quite what you want... because ?context is the entire surrounding context, not just the parent. I can't think of a way to request the direct parent; in such situations I'd write a pattern to match the parent:
(m/search {:foo {:bar :target}}
          (m/$ {?k :target :as ?m})
          ?m)
=> ({:bar :target})
You stated that you don't want to restrict the 'type' of the parent, but I think it's impossible to avoid to some extent... certainly you could search for map parents, and sequable parents, which in combination would cover all the possible parent types:
(m/search {:foo {:bar [:baz :booz :target]}}
          (m/$ (m/and ?parent (m/seqable & _ :target & _)))
          ?parent)
=> ([:baz :booz :target])
^^ HTH, sorry if I'm misunderstanding your goal.

prnc15:10:25

> But this isn’t quite what you want... because ?context is the entire surrounding context, not just the parent. yes! > You stated that you don’t want to restrict the ‘type’ of the parent Yes 🙂 I’m assuming no knowledge of the concrete type, abstractions are great 😜 — so sequable is great Thank you so much 🙏 This indeed helps a lot I might be on some older meander version or something cause I’m seeing

(m/search {:foo {:bar [:baz :booz :target]}}
    (m/$ (m/and ?parent (m/seqable & _ :target & _)))
    ?parent)
  ;; => ()
Will check! Thanks again :)

timothypratley16:10:05

For reference I was using

meander/epsilon            {:mvn/version "0.0.650"}