You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
759 B

4 years ago
  1. import _curry1 from "./internal/_curry1.js";
  2. import mean from "./mean.js";
  3. /**
  4. * Returns the median of the given list of numbers.
  5. *
  6. * @func
  7. * @memberOf R
  8. * @since v0.14.0
  9. * @category Math
  10. * @sig [Number] -> Number
  11. * @param {Array} list
  12. * @return {Number}
  13. * @see R.mean
  14. * @example
  15. *
  16. * R.median([2, 9, 7]); //=> 7
  17. * R.median([7, 2, 10, 9]); //=> 8
  18. * R.median([]); //=> NaN
  19. */
  20. var median =
  21. /*#__PURE__*/
  22. _curry1(function median(list) {
  23. var len = list.length;
  24. if (len === 0) {
  25. return NaN;
  26. }
  27. var width = 2 - len % 2;
  28. var idx = (len - width) / 2;
  29. return mean(Array.prototype.slice.call(list, 0).sort(function (a, b) {
  30. return a < b ? -1 : a > b ? 1 : 0;
  31. }).slice(idx, idx + width));
  32. });
  33. export default median;