Ramda’s R.maxBy() answers a projection selection question: which of two values wins when you compare them through a function f? You pass a unary projection, then two values, and get back one of the originals — whichever produces the larger f result. It is not a Boolean test like R.eqBy. Cousin R.max compares values directly; R.minBy picks the smaller by projection. Previous helper is R.max. Next is R.min.
The official sample is the whole idea. const square = n => n * n; then R.maxBy(square, -3, 2) returns -3 because square(-3) is 9 while square(2) is 4. Raw numeric order would pick 2, but projection order picks -3. The result is always one of the two inputs, never f(a) or f(b).
Syntax
Call R.maxBy. Start with this line.
Ramda
R.maxBy(f, a, b)
Call
What it does
R.maxBy(f, a, b)
Primary signature.
R.maxBy(...)
Call the relation helper.
Prefer import { maxBy } from "ramda" when you want a named import.
Library declarations
Ramda
R.maxBy(f, a, b)
Parameters
Parameter
Description
f
Required. Function. Unary projection applied to both a and b. Its results are compared for ordering.
a
Required. Any. First value. One of a or b is returned. Not mutated.
b
Required. Any. Second value to compare against. Not mutated.
Return Value
Return Value
Description
Same type as inputs
The original a or b whose projection f(...) is larger. Never f(a), never a Boolean.
Ramda Examples
📚 Getting Started
The official Ramda square sample and how projection order differs from direct comparison.
Example 1 — Official square Sample
From the Ramda docs. R.maxBy compares projections and returns the original value, not f(result).
Ramda
const square = n => n * n;
R.maxBy(square, -3, 2);
//=> -3
R.prop('score') extracts each score. 92 > 88, so the full userA record is returned. This is the idiomatic pattern for picking the winning record by one field.
Example 4 — Maximum by Projection with R.reduce
Official list sample. Fold to the element with the largest square. Empty list returns the seed.
R.reduce walks the list, keeping the element whose square is largest. square(-5) is 25, beating every other element. On an empty list the seed 0 is returned unchanged.
Example 5 — Curried Partial Application with R.__
Fix the projection and one record. The placeholder waits for the other value in a pipeline.
R.maxBy(R.prop('score'), R.__, userB) fixes the projection and the baseline record. Each call compares a new candidate against userB and returns whichever record has the higher score.
Compatibility
Browser Support
R.maxBy() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Ramda 0.8.0+ · Any JS runtime
Ramda.js R.maxBy()
Load Ramda 0.8.0 or later via CDN or npm, then call R.maxBy with a projection function and two values. No extra browser API is required.
100%With Ramda 0.8.0+
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
R.maxBy()Universal
Bottom line: Safe in any project that includes Ramda 0.8.0 or later. Use a CDN build such as ramda.min.js in the browser or install ramda from npm for Node. These labs use [email protected].
Frequently Asked Questions
R.maxBy(f, a, b) returns whichever of a or b produces the larger result when passed to f. Type: Ord b => (a → b) → a → a → a. Arity 3, curried. Added in v0.8.0.
const square = n => n * n; R.maxBy(square, -3, 2) returns -3 because square(-3) is 9 and square(2) is 4. The result is the original value, not the projection.
R.max compares a and b directly. R.maxBy compares f(a) vs f(b) and returns the original a or b whose projection is larger. On (-3, 2), R.max returns 2 while R.maxBy(square) returns -3.
R.minBy(fn, a, b) picks the smaller by projection. R.eqBy(f, x, y) returns a Boolean when f(x) equals f(y). R.maxBy picks the larger original value by ordering on f.
Always the original a or b — never f(a) or f(b). You get back the winning input ready to use in pipelines.
No. R.maxBy is a stable Ramda relation helper from v0.8.0. It is not deprecated, experimental, or non-standard.