@puzzler select-one
is the same as select-first
except it enforces that your path navigates to at most one value
corrected the wiki entry on select-first
, thanks for letting me know
you probably want select-one
for your use case
I recall a time when you sketched out a navigator for transforming a nested map in such a way that on the way out, it would recursively delete any keys with empty maps. I can't find it in the slack logs. Do you by any chance have a handy pointer to that?
i asked a similar question here and on SO and posted nathan's answer: https://stackoverflow.com/a/57002777/1185536
I saw that, but it looks like the answers are all about scanning through a whole nested map structure removing nil leaves. That's not really what I want. I want things to get removed if the output of the transformation is NONE, and that causes its map to be empty, and so on.
So something like: (setval (keypath-remove-empties :a :c :d) NONE {:b 1, :a {:c {:d 0}}}) gives back {:b 1}
@puzzler I believe you’re looking for compact
?
user=> (sp/setval (sp/compact :a :c :d) sp/NONE {:b 1, :a {:c {:d 0}}})
{:b 1}
@schmee Yes, thanks!
you probably don't want to return NONE if the top-level structure becomes empty, so something like (setval [:a (compact :c :d)] NONE data)
is generally the pattern to use
I've finally figured out why I can't use filterer
after MAP-KEYS; it expects a sequence where MAP-KEYS navigates to a view of many keyword (for me) elements
Which is better: (setval [MAP-KEYS #(#{"a"} (namespace %))] NONE mymap)
or: (setval [(filterer [FIRST NAMESPACE #{"a"}]) MAP-KEYS] NONE mymap)
please?
or y'know something else
The difference seems more substantial when using select
; the latter form allows me to get the whole entries whereas the former has irrevocably descended to the keys
My initial attempt btw was something like (setval [MAP-KEYS (filterer NAMESPACE #{"a"})] NONE mymap)
@alee you can also do (setval [MAP-KEYS (selected? NAMESPACE (pred= "a"))] NONE mymap)