Hacker News new | ask | show | jobs
by adriaanmulder 1016 days ago
This is a simple function that I find quite useful. Crawl through an object to update some specific nested value. Useful for things like optimistic updates in a cache (eg. Find every instance of an object with some guid in any random data structure and update a field in it).

Is this function known by any other name?

4 comments

Disliking the generality of this function (as a sibling commenter said.. I mean with the wrong predicate you won't even get to test for list or dict.. but ok for those nested list/dict structures; but why not test first for them?) it is just a map and an if, or a map/list comprehension?
I mean, the paradigm would be a iterable-based dispatch. There's no name (that I know of) for such a specific (and yet, weirdly general) implementation.

I agree with many others in this thread when it comes to this being too specific to be useful and too generic to keep you from shooting yourself in the foot. I would, instead, add this to Array/Object's prototypes and call it on the specific types instead. At least, in that case, you aren't handling multiple fields of concern.

Maybe related to "Scrap your boilerplate"[1] which seems the more general approach.

[1] https://www.microsoft.com/en-us/research/wp-content/uploads/...

basically fmap in haskell. A more complete implementation would have the iterating logic distributed and owned by the prototypes of any arbitrary data container, dynamic dispatch ftw.