3 ms·
The basic function is simply defined as: function clamp(num, min, max) { return Math.max(min, Math.min(num, max)); } That is, if you don't try to do a
by STRML 6y ago
The basic function is simply defined as:
function clamp(num, min, max) {
return Math.max(min, Math.min(num, max));
}
That is, if you don't try to do anything fancy and make any parameters optional. Lodash does and it makes the implementation much more complex.
_.clamp(input: number, lower?: number, upper: number): number;
https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L13981 https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb7...
- deleted 6y ago[deleted]
- bonoboTP 6y agoOr to make sure it's crystal clear what's going on: function clamp(num, min, max) { if (num > max) return max; if (num < min) return min; return num; }
- matthewmacleod 6y agoSeriously. This is about a million times better.
- mywittyname 6y agoAbsolutely. With the above implementation, I can see exactly what's going on, with the others, I'm trying to work out potential edge cases.
- brandmeyer 6y agoI prefer the min/max-based definition for the same reason: Its easier to work out the edge cases around NaN and comparisons against NaN.
- cogman10 6y agoNaN always returns false on comparisons. So seems pretty straight forward that this would return NaN if NaN is passed in. That doesn't seem unreasonable. (In fact, that's what happens with the min/max approach).
- jaffathecake 6y agoYep, I came to the same conclusion https://twitter.com/jaffathecake/status/1296423819238944768 https://twitter.com/jaffathecake/status/1296423819238944768
- Akronymus 6y agoNow in C# with pattern matching: int clamp(num, min, max) { return num switch { _ when num > max => max, _ when num < min => min, _ => num }; } Or as a lambda: Func<int, int, int, int> clamp = (num, min, max) => num switch {_ when num > max => max, _ when num < min => min,_ => num};
- 52-6F-62 6y agoSpeaking only to JS is there any reason to write it any other way outside of being clever or as a lambda for singular use? I definitely prefer this version. (Assuming any necessary runtime checks are included for a given project)
- chasd00 6y agothose extra newline characters slow down the page load :)
- tobyhinloopen 6y agoYou’re joking right
- beirut_bootleg 6y agoThere are many reasons to forego readability, especially when writing a library: performance, compatibility, requirements, interpreter/compiler optimizations or even cyclomatic complexity. In lodash's case it might even be all of the above, although I can't speak for the intentions of the authors since there are no comments to guide readers through the process. Note GP's link points to what looks like the v3 branch. Check out the latest implementation of clamp, with a few less if statements, and what looks like a NaN check using strict equality if you want your mind blown. https://github.com/lodash/lodash/blob/86a852fe763935bb64c12589df5391fd7d3bb14d/clamp.js#L22-L23 https://github.com/lodash/lodash/blob/86a852fe763935bb64c125...
- robocat 6y agoReading that code it looks to me that: clamp(null) returns 0 clamp(undefined) returns NaN clamp(1, NaN, NaN) returns 0 clamp(1) returns 0 clamp(1, 5, NaN) returns 5 JavaScript is hard to write safe code for.