Created
November 24, 2022 18:06
-
-
Save DarrenSem/ce27852de130803307cfe5a120759526 to your computer and use it in GitHub Desktop.
Math.sum.js and Math.avg (average) and Math.max and Math.sum -- ALL will automatically flatten (all, args, passed, [[even, [, Arrays]]])
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Math.sum.js and Math.avg and Math.max and Math.sum -- ALL will automatically flatten (all, args, passed, [[even, [, Arrays]]]) | |
console.log(Math.sum); | |
// undefined | |
console.log(Math.avg); | |
// undefined | |
console.log(Math.max); | |
// ƒ max() { [native code] } | |
console.log(Math.min); | |
// ƒ min() { [native code] } | |
console.log(Math.min(5, "2", [3, "1", 4, 6])); | |
// NaN | |
console.log(Math.min(5, "2", ...[3, "1", 4, 6])); | |
// 1 (using a "workaround" to process elements of an Array passed to the built-in Math.max and Math.min) | |
console.log(Math.min(5, "2", 3, "1", 4, 6)); | |
// 1 | |
// ((a,b,{max:c,min:d}=a)=>!a.sum&&(a.sum=(...a)=>b(a).reduce((a,b)=>+b+a,0),a.avg=(...a)=>Math.sum(a)/b(a).length,a.max=(...a)=>c(...b(a)),a.min=(...a)=>d(...b(a))))(Math,a=>a.flat(1/0)) | |
( ( MATH, flat, {max, min} = MATH ) => !MATH.sum && ( // /]/.test(max) && ( | |
MATH.sum = (...args) => flat(args).reduce( (curr, v) => +v + curr, 0), | |
MATH.avg = (...args) => Math.sum(args) / flat(args).length, | |
MATH.max = (...args) => max(...flat(args)), | |
MATH.min = (...args) => min(...flat(args)) | |
) | |
)( Math, args => args.flat(Infinity) ); | |
/////// first attempt -- but it minified 11 characters larger | |
// ((a,{max:b,min:c}=a)=>!a.sum&&(a.sum=(...a)=>a.flat(1/0).reduce((a,b)=>+b+a,0),a.avg=(...a)=>Math.sum(a)/a.flat(1/0).length,a.max=(...a)=>b(...a.flat(1/0)),a.min=(...a)=>c(...a.flat(1/0))))(Math) | |
// ( (M, {max, min} = M) => !M.sum && ( // /]/.test(max) && ( | |
// M.sum = (...args) => args.flat(Infinity).reduce( (curr, v) => +v + curr, 0), | |
// M.avg = (...args) => Math.sum(args) / args.flat(Infinity).length, | |
// M.max = (...args) => max(...args.flat(Infinity)), | |
// M.min = (...args) => min(...args.flat(Infinity)) | |
// ) | |
// )(Math); | |
console.log(Math.sum); | |
// (...args) => flat(args).reduce( (curr, v) => +v + curr, 0) // AKA (...args) => args.flat(Infinity).reduce( (curr, v) => +v + curr, 0) | |
console.log(Math.avg); | |
// (...args) => Math.sum(args) / flat(args).length // AKA (...args) => Math.sum(args) / args.flat(Infinity).length | |
console.log(Math.max); | |
// (...args) => max(...flat(args)) // AKA (...args) => max(...args.flat(Infinity)) | |
console.log(Math.min); | |
// (...args) => min(...flat(args)) // AKA (...args) => min(...args.flat(Infinity)) | |
console.log(Math.min(5, "2", [3, "1", 4, 6])); | |
// 1 (no need for a "workaround" to process elements of an Array passed to the built-in Math.max and Math.min) | |
console.log(Math.min(5, "2", ...[3, "1", 4, 6])); | |
// 1 (BUT original functionality unchanged; code still works if it uses "workaround" to process elements of an Array) | |
console.log(Math.min(5, "2", 3, "1", 4, 6)); | |
// 1 (again, original functionality unchanged) | |
console.log(Math.sum(5, "2", [3, "1", 4, 6])); | |
// 21 | |
console.log(Math.sum(5, "2", ...[3, "1", 4, 6])); | |
// 21 | |
console.log(Math.avg(5, "2", [3, "1", 4, 6])); | |
// 3.5 | |
console.log(Math.avg(5, "2", ...[3, "1", 4, 6])); | |
// 3.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment