Live data from Hacker News

UpdateWhere is a handy function

twitter.com

11–20 of 32 posts

Re: UpdateWhere is a handy function

#11
Function-ified visitor pattern I guess? Python's NodeTransformer[0] is a good example of a thing like this though only across AST nodes.

Going along some previous discussions about jq, and stuff like specter (for Clojure), I do think it would be cool if there were more syntactic niceties for path expressions, extracting data from nested data structures/lists/etc, and applying transformations on them.

Specter can have a "nice DSL" (for certain definitions of nice!) thanks to Clojure's macro functionality. Haskell lenses get somewhere thanks to custom infix operator syntax being a thing. I have yet to see a library that tackles this nicely without those two tools though.

[0]: https://docs.python.org/3/library/ast.html#ast.NodeTransform...

Re: UpdateWhere is a handy function

#13

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?

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

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

Re: UpdateWhere is a handy function

#15

Feels like something had gone very wrong if a function like this is useful. Would only be useful for unstructured data since if you had structure data you could write a more specific function. This can easily match the wrong value and cause problems. And why would it match the wrong value? Because you have unstructured data and it might not be knowable if you might have false positives

I don't like how it could result in surprises. e.g. you use it to update an array, but there are objects inside, which you accidentally modify.

I think these surprises could be reduced if you turn them into explicit parameters in the signature, e.g.

  export const updateWhere = (whereFn, updatefn, thing, recurseArray=true , recurseObjects=true)

Re: UpdateWhere is a handy function

#19

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?

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.

Re: UpdateWhere is a handy function

#20
post #8

This reminds me of Haskell's 'functor' [1] [1] https://hackage.haskell.org/package/base-4.18.0.0/docs/Data-...

Very much so, it’s a functor instance on some sort of tree, except the `map` includes a conditional dispatch.

It’s essentially

    fmap $ ap fromMaybe
Except this doesn’t quite work, I’m sure there’s a pointfree version, best I can achieve is

    \p -> fmap $ ap fromMaybe p
Which takes an `a -> Maybe a` and an `[a]`, and replaces all the values for which the callback returns `Some`, and leaves as-is all the values for which it returns `None`.

(I collapsed the `whereFn` and `updateFn` into a single one because there’s no reason not to)

Post reply on HN