5

I am trying to return the largest element in a list page:

page = [1,2,3];
R.max(page); // returns a function.
R.max(-Infinity, page); // seems correct but doesn't work as expected.
Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75
Jim
  • 14,952
  • 15
  • 80
  • 167
  • 1
    as Javascript is tagged, `Math.min.apply(this,[1,2,3])` – vinayakj Jul 19 '15 at 18:15
  • Note that your title contradicts your question -- do you want the smallest or largest item? I answered for the largest item, getting the smallest one should be easy if you understand the mechanics. – Frédéric Hamidi Jul 19 '15 at 18:25
  • 1
    Yes, as of [0.16](https://github.com/ramda/ramda/issues/1277) Ramda's `max` and `min` functions are binary, for reasons described in [Issue 1230](https://github.com/ramda/ramda/issues/1230) and implemented in [PR 1231](https://github.com/ramda/ramda/issues/1230). The answer from @FrédéricHamidi is probably the simplest way to do this. Obviously you could create a stand-alone function for this if you need it often. – Scott Sauyet Jul 19 '15 at 21:59

2 Answers2

22

I don't have the ramda package installed, so this is untested, but from the documentation max() only takes two arguments, so you would have to reduce() your array upon it:

var page = [1, 2, 3],
    result = R.reduce(R.max, -Infinity, page);
// 'result' should be 3.
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Tested on ramda's online repl: https://ramdajs.com/repl/#?reduce%28max%2C%20-Infinity%2C%20%5B1%2C%202%2C%203%5D%29 – mlg Jul 13 '20 at 15:35
1

Use the apply function: https://ramdajs.com/0.22.1/docs/#apply

const page = [1, 2, 3];
R.apply(Math.max, page); //=> 3
Xuan
  • 5,255
  • 1
  • 34
  • 30