3 ms·
Good stuff! Normally I cringe to see new language features, but these are pretty good (Java after lambdas, even arguably after generics, is pretty crufty, IMHO)
by javajosh 7y ago
Good stuff! Normally I cringe to see new language features, but these are pretty good (Java after lambdas, even arguably after generics, is pretty crufty, IMHO)
One thing I'd to request is that `Object.fromEntries()` simply skip undefined entries, rather than do...strange things. I wrote an `mapObject` function that looks like `(a,fn) => Object.fromEntries(Object.entries(a).map(fn))` and occasionally the fn needs to skip an entry and the easiest way to do this is just return `undefined`.
- masklinn 7y ago> One thing I'd to request is that `Object.fromEntries()` simply skip undefined entries, rather than do...strange things. Not sure what strange things it does. fromEntries simply takes each entry, sets its first item as key (stringifying if necessary as object keys are necessarily strings) and the second item as value. Naturally falls from these that a null or undefined entry will error, and an empty entry will create an {undefined: undefined} item. > I wrote an `mapObject` function that looks like `(a,fn) => Object.fromEntries(Object.entries(a).map(fn))` and occasionally the fn needs to skip an entry and the easiest way to do this is just return `undefined`. Use flatMap instead?
- javajosh 7y agoYes, it errors out. And flatMap isn't applicable here. Here's some complete test code you can try in your browser right now: ``` let p = (a,fn) => Object.fromEntries(Object.entries(a).map(fn)) p({a:1, b:2}, ([k,v])=> v === 2 ? undefined : [k,v]) ``` My intent is to skip the entry with `b:2`. However, both map and flatMap error out. It would be nice if `fromEntries` did what I think is the appropriate thing, which is just skip undefined and null. Is this not the behavior you'd expect? Note that without special treatment in fromEntries, there's no way for the mapping function to indicate "skip this entry". You have to do it in another pass, or some other way.
- masklinn 7y ago> And flatMap isn't applicable here. It's absolutely applicable, flatMap can trivially act as a filterMap by returning an empty array in the "remove this element" case: let p = (a,fn) => Object.fromEntries(Object.entries(a).flatMap(fn)) p({a:1, b:2}, ([k,v])=> v === 2 ? [] : [[k,v]]) there you go. You can even make the adaptation transparent by wrapping the fn: let p = (a,fn) => Object.fromEntries(Object.entries(a).flatMap((v) => { let r = fn(v); return r == null ? [] : [r]; })); p({a:1, b:2}, ([k,v])=> v === 2 ? undefined : [k,v]) > My intent is to skip the entry with `b:2`. However, both map and flatMap error out. Return an empty list from flatmap to remove the entry and a singleton list containing the new pair to alter it. > Is this not the behavior you'd expect? No. I would much rather have the existing function which has a very clear and straightforward behaviour and is not prone to silently misbehaving on buggy code. > Note that without special treatment in fromEntries, there's no way for the mapping function to indicate "skip this entry". I fail to see an issue with that. If you want to remove an entry, remove the entry.