3 ms·
I'd say there's a reasonable middle ground here where you can define isOdd where you need it: isOdd = x => x % 2 === 1 ... It's one of those things th
by aidos 2y ago
I'd say there's a reasonable middle ground here where you can define isOdd where you need it:
isOdd = x => x % 2 === 1
...
It's one of those things that doesn't pop up all that often and never changes. I've looked through my whole codebase and there are 3 occurrences.
Aside: I don't use Dart, but does that mean that you have to rely on each different type of iterable to implement these functions?
- vsnf 2y agoI don't quite know what you mean by "rely on" here, but the isEmpty fields are implemented on the iterator interface, so any of the standard container types implement it by default, including lists, sets, and maps. Also to address other posts in the thread, while I don't think this specific example is worth indicting JS developers over, I do think the culture of infinite dependencies common to JS developers is. This shouldn't be an entire additional dependency. Just implement the modulo operation, or as parent suggests, make a nicely named utility function that wraps the modulo operation. But when the culture strongly encourages downloading micro packages for every little thing, I can hardly blame them for adding this into the pile of yet more things they've downloaded.
- aidos 2y agoAhhh, I see my confusion here. Got mixed up with the abstractions (isEven != isEmtpy) and thought there was something crazy going on with lists of numbers implementing isEmpty. On the whole I do agree with you about all this mess. The JS stdlib has a lot to answer for.
- firebaze 2y agoNot defending isOdd in any way, but the example shows that even something as clear-cut as testing a number for oddness isn't that easy to get right: isOdd(-1) === false (-1 % 2 === -1)
- aidos 2y agoHa! Good catch. You’d think that with a math degree including algebraic number systems I would have spotted that.