How about augmenting the built-in Array object to use Math.max/Math.min instead:
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
let p = [35,2,65,7,8,9,12,121,33,99];
console.log(`Max value is: ${p.max()}` +
`\nMin value is: ${p.min()}`);
Here is a JSFiddle.
Augmenting the built-ins can cause collisions with other libraries (some see), so you may be more comfortable with just apply'ing Math.xxx() to your array directly:
var min = Math.min.apply(null, arr),
max = Math.max.apply(null, arr);
Alternately, assuming your browser supports ECMAScript 6, you can use spread syntax which functions similarly to the apply method:
var min = Math.min( ...arr ),
max = Math.max( ...arr );
Answer from Roatin Marth on Stack OverflowHow about augmenting the built-in Array object to use Math.max/Math.min instead:
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
let p = [35,2,65,7,8,9,12,121,33,99];
console.log(`Max value is: ${p.max()}` +
`\nMin value is: ${p.min()}`);
Here is a JSFiddle.
Augmenting the built-ins can cause collisions with other libraries (some see), so you may be more comfortable with just apply'ing Math.xxx() to your array directly:
var min = Math.min.apply(null, arr),
max = Math.max.apply(null, arr);
Alternately, assuming your browser supports ECMAScript 6, you can use spread syntax which functions similarly to the apply method:
var min = Math.min( ...arr ),
max = Math.max( ...arr );
var max_of_array = Math.max.apply(Math, array);
For a full discussion see: http://aaroncrane.co.uk/2008/11/javascript_max_api/
Videos
Function.prototype.apply() states that the arguments parameter can be any array-like object.
Typed Arrays are array-like objects, so they should be accepted by the apply method.
Furthermore, the Math.min specification states that its arguments do not even need to be numbers:
Given zero or more arguments, this function calls ToNumber on each of the arguments and returns the smallest of the resulting values.
Knowing all of the above, what you are trying to do looks correct and it seems like a TypeScript bug.
As for why that is happening, currently the definitions for Math.min and CallableFunction.apply are as follows:
min(...values: number[]): number;
apply<T, A extends any[], R>(
this: (this: T, ...args: A) => R,
thisArg: T,
args: A
): R;
Most likely both these definitions need to be adapted to act according to the standards.
EDIT: Likely only the apply definition needs to be changed to something like:
apply<T, A extends Iterable<AT>, AT, R>(
this: (this: T, ...args: AT[]) => R,
thisArg: T,
args: A
): R;
Or to be more correct, ArrayLike<AT> should be used instead of Iterable<AT>, but for the above to work ArrayLike would need to extend from Iterable
The TypeScript-safe way to write this would be:
let min_value = Math.min(...arr);
Which compiles down to Math.min.apply(Math, arr);
Edit: I am not asking for a way to use these methods with arrays. What I am asking is why in the first place do they not accept arrays.
However, I will list the solutions mentioned:
Math.max.apply(null, arr)Math.max(...arr)arr.reduce((max, current) => (current > max ? current : max), -Infinity);