mirror of
https://github.com/swc-project/swc.git
synced 2024-12-23 13:51:19 +03:00
37 lines
811 B
TypeScript
37 lines
811 B
TypeScript
|
// Loaded from https://deno.land/x/ramda@v0.27.2/source/median.js
|
||
|
|
||
|
|
||
|
import _curry1 from './internal/_curry1.js';
|
||
|
import mean from './mean.js';
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Returns the median of the given list of numbers.
|
||
|
*
|
||
|
* @func
|
||
|
* @memberOf R
|
||
|
* @since v0.14.0
|
||
|
* @category Math
|
||
|
* @sig [Number] -> Number
|
||
|
* @param {Array} list
|
||
|
* @return {Number}
|
||
|
* @see R.mean
|
||
|
* @example
|
||
|
*
|
||
|
* R.median([2, 9, 7]); //=> 7
|
||
|
* R.median([7, 2, 10, 9]); //=> 8
|
||
|
* R.median([]); //=> NaN
|
||
|
*/
|
||
|
var median = _curry1(function median(list) {
|
||
|
var len = list.length;
|
||
|
if (len === 0) {
|
||
|
return NaN;
|
||
|
}
|
||
|
var width = 2 - len % 2;
|
||
|
var idx = (len - width) / 2;
|
||
|
return mean(Array.prototype.slice.call(list, 0).sort(function(a, b) {
|
||
|
return a < b ? -1 : a > b ? 1 : 0;
|
||
|
}).slice(idx, idx + width));
|
||
|
});
|
||
|
export default median;
|