maxBy.js.flow 643 B

12345678910111213141516171819202122232425
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @providesModule maxBy
  8. * @flow
  9. */
  10. var minBy = require('./minBy');
  11. var compareNumber = (a, b) => a - b;
  12. /**
  13. * Returns the maximum element as measured by a scoring function f. Returns the
  14. * first such element if there are ties.
  15. */
  16. function maxBy<A, B>(as: Iterable<A>, f: (a: A) => B, compare?: ?(u: B, v: B) => number): ?A {
  17. compare = compare || (compareNumber: any);
  18. return minBy(as, f, (u, v) => (compare: any)(v, u));
  19. }
  20. module.exports = maxBy;