minBy.js 840 B

1234567891011121314151617181920212223242526272829303132333435
  1. import _curry3 from "./internal/_curry3.js";
  2. import min from "./min.js";
  3. /**
  4. * Takes a function and two values, and returns whichever value produces the
  5. * smaller result when passed to the provided function.
  6. *
  7. * @func
  8. * @memberOf R
  9. * @since v0.8.0
  10. * @category Relation
  11. * @sig Ord b => (a -> b) -> a -> a -> a
  12. * @param {Function} f
  13. * @param {*} a
  14. * @param {*} b
  15. * @return {*}
  16. * @see R.min, R.maxBy
  17. * @example
  18. *
  19. * // square :: Number -> Number
  20. * const square = n => n * n;
  21. *
  22. * R.minBy(square, -3, 2); //=> 2
  23. *
  24. * R.reduce(R.minBy(square), Infinity, [3, -5, 4, 1, -2]); //=> 1
  25. * R.reduce(R.minBy(square), Infinity, []); //=> Infinity
  26. */
  27. var minBy =
  28. /*#__PURE__*/
  29. _curry3(function minBy(f, a, b) {
  30. var resultB = f(b);
  31. return min(f(a), resultB) === resultB ? b : a;
  32. });
  33. export default minBy;