Hacker News new | ask | show | jobs
by pyrolistical 1016 days ago
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

4 comments

This is often useful if you're rewriting something that looks like an abstract syntax tree.

> And why would it match the wrong value?

Because you may be searching the tree for something that looks like `(op(const)(const))` to replace it with `(const)`. But that may live many levels deep inside the structure.

Also, from existing names, that seems very close to MapIf (https://resources.wolframcloud.com/FunctionRepository/resour...) just with added recursion.

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)
If `thing` contains any cycles then it will explode. E.g.

    const thing = {};
    thing.self = thing;
or

    const thing = [];
    thing.push(thing);
If this sounds far-fetched to you just know that all DOM elements have these circular references through (for example) parent / child references.

Also any non-enumerable properties that may have been defined on objects are lost.

Basically the function is only suitable for recently parsed JSON.

That's a great point
I disagree. For example, It's useful if you have a bunch of cached network requests that each list some resources and you want to update all of the instances of a specific resource (matching by uuid).
So store the requests in a map and each request exposes an update() method.
I don’t know the domain where this function is used, but stuff like that is used constantly in data science.

    df[df[mySelection] == wrongVal] = rightVal