02:20 | <bakkot> | what do people do if they want to take the maximum of an with like 200k items in it, such that Math.max(...arr) overflows |
02:21 | <bakkot> | does this just not happen? |
02:21 | <bakkot> | context: I am thinking about how Math.sum should work - it could take an iterable, which seems like the obvious thing, but then it wouldn't match Math.max |
03:13 | <ljharb> | you’d probably slice it and max the slices? but i think it largely doesn’t happen |
03:15 | <ljharb> | however I’d assume that it should be possible for engines to handle too-large arg arrays that are spread into a call to a builtin function? |
03:24 | <bakkot> | it is possible in principle but difficult in practice; none of the major engines support passing an array of length 1e6 in my testing |
03:31 | <bakkot> | I would if we could reasonably overload Math.max such that if you call it with exactly one argument, and that argument is an object, it does a lookup of Symbol.iterator on that object and then uses that |
03:31 | <bakkot> | possibly this is a bad idea though |
03:31 | <bakkot> | probably someone out there is somehow relying Math.max([0, 1]) being NaN |
03:36 | <Kris Kowal> | what do people do if they want to take the maximum of an with like 200k items in it, such that splice , because it was useful to funnel insertion and deletion operations through a single method. I ended up adding an swap(index, count, replacements) => void that was splice but not-variadic and also didn’t return a useless slice . |
03:39 | <Kris Kowal> | I think the bigger concern with unadic overloads of min and max would be the behavior of Math.min(...array) for arrays that happen to only have one value. |
03:40 | <Kris Kowal> | Perhaps it makes more sense to leave Math behind and Number.min,max,sum . |
03:41 | <Kris Kowal> | Also String.join(iterable) |
04:02 | <bakkot> | I would not want to have both Math.max and also Number.max , especially with different behaviors |
04:03 | <bakkot> |
with my suggestion this would only be problematic if the first value in the array happened to be an object with a Symbol.iterator method (or such that accessing that property was side-effecting, I guess) |
04:04 | <bakkot> | which seems unlikely, or at least unlikely-ish |
04:45 | <Richard Gibson> | I for one don't support that kind of overloading. If such behavior is to be added to the standard library, it should be in the form of new functions that always read Symbol.iterator . |
04:56 | <bakkot> | Richard Gibson do you think Math.sum should be such a new function, inconsistent with Math.max ? or would you want like a Math.maxList and Math.sumList so they could be consistent? |
05:00 | <Richard Gibson> | the latter. We've got precedent for Math functions that operate on an arbitrary number of values to accept each one as an argument, so new Math functions that accept an arbitrary number of values from an iterable should indicate that distinction by a common naming pattern. |
05:11 | <Richard Gibson> | similar to how Array.from process its argument as an iterable but Array does not |
05:41 | <bakkot> | python's max appears to be overloaded exactly as I suggested above, incidentally |
05:41 | <bakkot> | and their sum only takes an iterable |
05:42 | <bakkot> | I guess their max isn't exactly as I suggested; if you pass it exactly one item it assumes it's iterable and throws if not |
05:42 | <bakkot> | which, alas, we could not do at this point |
05:46 | <Jessidhia> | was wondering about “what if you want to sum more than one iterable”, and the obvious answer is to spread them, but that’d be an eager operation that’d result in double iteration and extra GC garbage; perhaps that’s a use case for concat in iteratorhelpers, or perhaps I’m just overthinking |
05:59 | <bakkot> | Jessidhia: yeah, concat in iterator helpers would work, though that's not in V1; flatmap serves the same purpose though |
05:59 | <bakkot> | Math.sumList(Iterator.from(arrayOfIterables).flatMap(x => x)) |
06:00 | <bakkot> | sumList is a terrible name for this API though; hope someone can come up with a better one |
06:16 | <Richard Gibson> | my bikeshed vote would be sumFrom , aligning with Array.from |
06:20 | <bakkot> | I don't hate it |
06:21 | <bakkot> | also if we do add Math.sum (the argument-list-taking version, by contrast to the iterable-taking version) can we make it throw if any of the things are not already numbers, instead of casting |
06:22 | <bakkot> | I don't want Math.sum([1]) to work; the fact that Math.max([1]) works is horrifying |
07:12 | <Ashley Claymore> | what do people do if they want to take the maximum of an with like 200k items in it, such that |
21:44 | <TabAtkins> | yup, or that's simple enough that I feel okay doing an arr.reduce((a,b)=>Math.max(a,b)) |
21:44 | <TabAtkins> | (I don't use reduce if the operation is more than a single simple expression, but this qualifies) |