always


a → (* → a)

Parameters

  • valThe value to wrap in a function

Returns function A Function :: * -> val.

Added in v0.1.0

Returns a function that always returns the given value. Note that for non-primitives the value returned is a reference to the original value.

This function is known as const, constant, or K (for K combinator) in other languages and libraries.

  1. const t = R.always('Tee');
  2. t(); //=> 'Tee'

comparator


((a, b) → Boolean) → ((a, b) → Number)

Parameters

  • predA predicate function of arity two which will return true if the first argument is less than the second, false otherwise

Returns function A Function :: a -> b -> Int that returns `-1` if a < b, `1` if b < a, otherwise `0`

Added in v0.1.0

Makes a comparator function out of a function that reports whether the first element is less than the second.

  1. const byAge = R.comparator((a, b) => a.age < b.age);
  2. const people = [
  3. { name: 'Emma', age: 70 },
  4. { name: 'Peter', age: 78 },
  5. { name: 'Mikhail', age: 62 },
  6. ];
  7. const peopleByIncreasingAge = R.sort(byAge, people);
  8. //=> [{ name: 'Mikhail', age: 62 },{ name: 'Emma', age: 70 }, { name: 'Peter', age: 78 }]

compose


((y → z), (x → y), …, (o → p), ((a, b, …, n) → o)) → ((a, b, …, n) → z)

Parameters

  • …functionsThe functions to compose

Returns function

Added in v0.1.0

Performs right-to-left function composition. The last argument may have any arity; the remaining arguments must be unary.

Note: The result of compose is not automatically curried.

See also pipe.

  1. const classyGreeting = (firstName, lastName) => "The name's " + lastName + ", " + firstName + " " + lastName
  2. const yellGreeting = R.compose(R.toUpper, classyGreeting);
  3. yellGreeting('James', 'Bond'); //=> "THE NAME'S BOND, JAMES BOND"
  4. R.compose(Math.abs, R.add(1), R.multiply(2))(-4) //=> 7

construct


(* → {*}) → (* → {*})

Parameters

  • fnThe constructor function to wrap.

Returns function A wrapped, curried constructor function.

Added in v0.1.0

Wraps a constructor function inside a curried function that can be called with the same arguments and returns the same type.

See also invoker.

  1. // Constructor function
  2. function Animal(kind) {
  3. this.kind = kind;
  4. };
  5. Animal.prototype.sighting = function() {
  6. return "It's a " + this.kind + "!";
  7. }
  8. const AnimalConstructor = R.construct(Animal)
  9. // Notice we no longer need the 'new' keyword:
  10. AnimalConstructor('Pig'); //=> {"kind": "Pig", "sighting": function (){...}};
  11. const animalTypes = ["Lion", "Tiger", "Bear"];
  12. const animalSighting = R.invoker(0, 'sighting');
  13. const sightNewAnimal = R.compose(animalSighting, AnimalConstructor);
  14. R.map(sightNewAnimal, animalTypes); //=> ["It's a Lion!", "It's a Tiger!", "It's a Bear!"]

curry


(* → a) → (* → a)

Parameters

  • fnThe function to curry.

Returns function A new, curried function.

Added in v0.1.0

Returns a curried equivalent of the provided function. The curried function has two unusual capabilities. First, its arguments needn’t be provided one at a time. If f is a ternary function and g is R.curry(f), the following are equivalent:

  • g(1)(2)(3)
  • g(1)(2, 3)
  • g(1, 2)(3)
  • g(1, 2, 3)

Secondly, the special placeholder value R.__ may be used to specify “gaps”, allowing partial application of any combination of arguments, regardless of their positions. If g is as above and _ is R.__, the following are equivalent:

  • g(1, 2, 3)
  • g(_, 2, 3)(1)
  • g(_, _, 3)(1)(2)
  • g(_, _, 3)(1, 2)
  • g(_, 2)(1)(3)
  • g(_, 2)(1, 3)
  • g(_, 2)(_, 3)(1)

Please note that default parameters don’t count towards a function arity and therefore curry won’t work well with those.

See also curryN, partial.

  1. const addFourNumbers = (a, b, c, d) => a + b + c + d;
  2. const curriedAddFourNumbers = R.curry(addFourNumbers);
  3. const f = curriedAddFourNumbers(1, 2);
  4. const g = f(3);
  5. g(4); //=> 10
  6. // R.curry not working well with default parameters
  7. const h = R.curry((a, b, c = 2) => a + b + c);
  8. h(1)(2)(7); //=> Error! (`3` is not a function!)

useWith


((x1, x2, …) → z) → [(a → x1), (b → x2), …] → (a → b → … → z)

Parameters

  • fnThe function to wrap.
  • transformersA list of transformer functions

Returns function The wrapped function.

Added in v0.1.0

Accepts a function fn and a list of transformer functions and returns a new curried function. When the new function is invoked, it calls the function fn with parameters consisting of the result of calling each supplied handler on successive arguments to the new function.

If more arguments are passed to the returned function than transformer functions, those arguments are passed directly to fn as additional parameters. If you expect additional arguments that don’t need to be transformed, although you can ignore them, it’s best to pass an identity function so that the new function reports the correct arity.

See also converge.

  1. R.useWith(Math.pow, [R.identity, R.identity])(3, 4); //=> 81
  2. R.useWith(Math.pow, [R.identity, R.identity])(3)(4); //=> 81
  3. R.useWith(Math.pow, [R.dec, R.inc])(3, 4); //=> 32
  4. R.useWith(Math.pow, [R.dec, R.inc])(3)(4); //=> 32

flip


((a, b, c, …) → z) → (b → a → c → … → z)

Parameters

  • fnThe function to invoke with its first two parameters reversed.

Returns * The result of invoking fn with its first two parameters’ order reversed.

Added in v0.1.0

Returns a new function much like the supplied one, except that the first two arguments’ order is reversed.

  1. const mergeThree = (a, b, c) => [].concat(a, b, c);
  2. mergeThree(1, 2, 3); //=> [1, 2, 3]
  3. R.flip(mergeThree)(1, 2, 3); //=> [2, 1, 3]

groupBy


Idx a => (b → a) → [b] → {a: [b]}

Idx = String | Int | Symbol

Parameters

  • fnFunction :: a -> Idx
  • listThe array to group

Returns Object An object with the output of fn for keys, mapped to arrays of elements that produced that key when passed to fn.

Added in v0.1.0

Splits a list into sub-lists stored in an object, based on the result of calling a key-returning function on each element, and grouping the results according to values returned.

Dispatches to the groupBy method of the second argument, if present.

Acts as a transducer if a transformer is given in list position.

See also reduceBy, transduce, indexBy, collectBy.

  1. const byGrade = R.groupBy(function(student) {
  2. const score = student.score;
  3. return score < 65 ? 'F' :
  4. score < 70 ? 'D' :
  5. score < 80 ? 'C' :
  6. score < 90 ? 'B' : 'A';
  7. });
  8. const students = [{name: 'Abby', score: 84},
  9. {name: 'Eddy', score: 58},
  10. // ...
  11. {name: 'Jack', score: 69}];
  12. byGrade(students);
  13. // {
  14. // 'A': [{name: 'Dianne', score: 99}],
  15. // 'B': [{name: 'Abby', score: 84}]
  16. // // ...,
  17. // 'F': [{name: 'Eddy', score: 58}]
  18. // }

identity


a → a

Parameters

  • xThe value to return.

Returns * The input value, x.

Added in v0.1.0

A function that does nothing but return the parameter supplied to it. Good as a default or placeholder function.

  1. R.identity(1); //=> 1
  2. const obj = {};
  3. R.identity(obj) === obj; //=> true

invoker


Number → String → (a → b → … → n → Object → *)

Parameters

  • arityNumber of arguments the returned function should take before the target object.
  • methodName of any of the target object’s methods to call.

Returns function A new curried function.

Added in v0.1.0

Given an arity (Number) and a name (String) the invoker function returns a curried function that takes arity arguments and a context object. It will “invoke” the name‘d function (a method) on the context object.

See also construct.

  1. // A function with no arguments
  2. const asJson = invoker(0, "json")
  3. // Just like calling .then((response) => response.json())
  4. fetch("http://example.com/index.json").then(asJson)
  5. // A function with one argument
  6. const sliceFrom = invoker(1, 'slice');
  7. sliceFrom(6, 'abcdefghijklm'); //=> 'ghijklm'
  8. // A function with two arguments
  9. const sliceFrom6 = invoker(2, 'slice')(6);
  10. sliceFrom6(8, 'abcdefghijklm'); //=> 'gh'
  11. // NOTE: You can't simply pass some of the arguments to the initial invoker function.
  12. const firstCreditCardSection = invoker(2, "slice", 0, 4)
  13. firstCreditCardSection("4242 4242 4242 4242") // => Function<...>
  14. // Since invoker returns a curried function, you may partially apply it to create the function you need.
  15. const firstCreditCardSection = invoker(2, "slice")(0, 4)
  16. firstCreditCardSection("4242 4242 4242 4242") // => "4242"

nAry


Number → (* → a) → (* → a)

Parameters

  • nThe desired arity of the new function.
  • fnThe function to wrap.

Returns function A new function wrapping fn. The new function is guaranteed to be of arity n.

Added in v0.1.0

Wraps a function of any arity (including nullary) in a function that accepts exactly n parameters. Any extraneous parameters will not be passed to the supplied function.

See also binary, unary.

  1. const takesTwoArgs = (a, b) => [a, b];
  2. takesTwoArgs.length; //=> 2
  3. takesTwoArgs(1, 2); //=> [1, 2]
  4. const takesOneArg = R.nAry(1, takesTwoArgs);
  5. takesOneArg.length; //=> 1
  6. // Only `n` arguments are passed to the wrapped function
  7. takesOneArg(1, 2); //=> [1, undefined]

once


(a… → b) → (a… → b)

Parameters

  • fnThe function to wrap in a call-only-once wrapper.

Returns function The wrapped function.

Added in v0.1.0

Accepts a function fn and returns a function that guards invocation of fn such that fn can only ever be called once, no matter how many times the returned function is invoked. The first value calculated is returned in subsequent invocations.

  1. const addOneOnce = R.once(x => x + 1);
  2. addOneOnce(10); //=> 11
  3. addOneOnce(addOneOnce(50)); //=> 11

pipe


(((a, b, …, n) → o), (o → p), …, (x → y), (y → z)) → ((a, b, …, n) → z)

Parameters

  • functions

Returns function

Added in v0.1.0

Performs left-to-right function composition. The first argument may have any arity; the remaining arguments must be unary.

In some libraries this function is named sequence.

Note: The result of pipe is not automatically curried.

See also compose.

  1. const f = R.pipe(Math.pow, R.negate, R.inc);
  2. f(3, 4); // -(3^4) + 1

tap


(a → *) → a → a

Parameters

  • fnThe function to call with x. The return value of fn will be thrown away.
  • x

Returns * x.

Added in v0.1.0

Runs the given function with the supplied object, then returns the object.

Acts as a transducer if a transformer is given as second parameter.

  1. const sayX = x => console.log('x is ' + x);
  2. R.tap(sayX, 100); //=> 100
  3. // logs 'x is 100'

binary


(a → b → c → … → z) → ((a, b) → z)

Parameters

  • fnThe function to wrap.

Returns function A new function wrapping fn. The new function is guaranteed to be of arity 2.

Added in v0.2.0

Wraps a function of any arity (including nullary) in a function that accepts exactly 2 parameters. Any extraneous parameters will not be passed to the supplied function.

See also nAry, unary.

  1. const takesThreeArgs = function(a, b, c) {
  2. return [a, b, c];
  3. };
  4. takesThreeArgs.length; //=> 3
  5. takesThreeArgs(1, 2, 3); //=> [1, 2, 3]
  6. const takesTwoArgs = R.binary(takesThreeArgs);
  7. takesTwoArgs.length; //=> 2
  8. // Only 2 arguments are passed to the wrapped function
  9. takesTwoArgs(1, 2, 3); //=> [1, 2, undefined]

unary


(a → b → c → … → z) → (a → z)

Parameters

  • fnThe function to wrap.

Returns function A new function wrapping fn. The new function is guaranteed to be of arity 1.

Added in v0.2.0

Wraps a function of any arity (including nullary) in a function that accepts exactly 1 parameter. Any extraneous parameters will not be passed to the supplied function.

See also binary, nAry.

  1. const takesTwoArgs = function(a, b) {
  2. return [a, b];
  3. };
  4. takesTwoArgs.length; //=> 2
  5. takesTwoArgs(1, 2); //=> [1, 2]
  6. const takesOneArg = R.unary(takesTwoArgs);
  7. takesOneArg.length; //=> 1
  8. // Only 1 argument is passed to the wrapped function
  9. takesOneArg(1, 2); //=> [1, undefined]

ap


[a → b] → [a] → [b]

Apply f => f (a → b) → f a → f b

(r → a → b) → (r → a) → (r → b)

Added in v0.3.0

ap applies a list of functions to a list of values.

Dispatches to the ap method of the first argument, if present. Also treats curried functions as applicatives.

  1. R.ap([R.multiply(2), R.add(3)], [1,2,3]); //=> [2, 4, 6, 4, 5, 6]
  2. R.ap([R.concat('tasty '), R.toUpper], ['pizza', 'salad']); //=> ["tasty pizza", "tasty salad", "PIZZA", "SALAD"]
  3. // R.ap can also be used as S combinator
  4. // when only two functions are passed
  5. R.ap(R.concat, R.toUpper)('Ramda') //=> 'RamdaRAMDA'

empty


a → a

Added in v0.3.0

Returns the empty value of its argument’s type. Ramda defines the empty value of Array ([]), Object ({}), String (''), TypedArray (Uint8Array [], Float32Array [], etc), and Arguments. Other types are supported if they define <Type>.empty, <Type>.prototype.empty or implement the FantasyLand Monoid spec.

Dispatches to the empty method of the first argument, if present.

  1. R.empty(Just(42)); //=> Nothing()
  2. R.empty([1, 2, 3]); //=> []
  3. R.empty('unicorns'); //=> ''
  4. R.empty({x: 1, y: 2}); //=> {}
  5. R.empty(Uint8Array.from('123')); //=> Uint8Array []

of


(* → {*}) → a → {a}

Parameters

  • CtorA constructor
  • valany value

Returns * An instance of the Ctor wrapping val.

Added in v0.3.0

  1. R.of(Array, 42); //=> [42]
  2. R.of(Array, [42]); //=> [[42]]
  3. R.of(Maybe, 42); //=> Maybe.Just(42)

constructN


Number → (* → {*}) → (* → {*})

Parameters

  • nThe arity of the constructor function.
  • FnThe constructor function to wrap.

Returns function A wrapped, curried constructor function.

Added in v0.4.0

Wraps a constructor function inside a curried function that can be called with the same arguments and returns the same type. The arity of the function returned is specified to allow using variadic constructor functions.

  1. // Variadic Constructor function
  2. function Salad() {
  3. this.ingredients = arguments;
  4. }
  5. Salad.prototype.recipe = function() {
  6. const instructions = R.map(ingredient => 'Add a dollop of ' + ingredient, this.ingredients);
  7. return R.join('\n', instructions);
  8. };
  9. const ThreeLayerSalad = R.constructN(3, Salad);
  10. // Notice we no longer need the 'new' keyword, and the constructor is curried for 3 arguments.
  11. const salad = ThreeLayerSalad('Mayonnaise')('Potato Chips')('Ketchup');
  12. console.log(salad.recipe());
  13. // Add a dollop of Mayonnaise
  14. // Add a dollop of Potato Chips
  15. // Add a dollop of Ketchup

converge


((x1, x2, …) → z) → [((a, b, …) → x1), ((a, b, …) → x2), …] → (a → b → … → z)

Parameters

  • afterA function. after will be invoked with the return values of fn1 and fn2 as its arguments.
  • functionsA list of functions.

Returns function A new function.

Added in v0.4.2

Accepts a converging function and a list of branching functions and returns a new function. The arity of the new function is the same as the arity of the longest branching function. When invoked, this new function is applied to some arguments, and each branching function is applied to those same arguments. The results of each branching function are passed as arguments to the converging function to produce the return value.

See also useWith.

  1. const average = R.converge(R.divide, [R.sum, R.length])
  2. average([1, 2, 3, 4, 5, 6, 7]) //=> 4
  3. const strangeConcat = R.converge(R.concat, [R.toUpper, R.toLower])
  4. strangeConcat("Yodel") //=> "YODELyodel"

curryN


Number → (* → a) → (* → a)

Parameters

  • lengthThe arity for the returned function.
  • fnThe function to curry.

Returns function A new, curried function.

Added in v0.5.0

Returns a curried equivalent of the provided function, with the specified arity. The curried function has two unusual capabilities. First, its arguments needn’t be provided one at a time. If g is R.curryN(3, f), the following are equivalent:

  1. * `g(1)(2)(3)`
  2. * `g(1)(2, 3)`
  3. * `g(1, 2)(3)`
  4. * `g(1, 2, 3)`

Secondly, the special placeholder value R.__ may be used to specify “gaps”, allowing partial application of any combination of arguments, regardless of their positions. If g is as above and _ is R.__, the following are equivalent:

  1. * `g(1, 2, 3)`
  2. * `g(_, 2, 3)(1)`
  3. * `g(_, _, 3)(1)(2)`
  4. * `g(_, _, 3)(1, 2)`
  5. * `g(_, 2)(1)(3)`
  6. * `g(_, 2)(1, 3)`
  7. * `g(_, 2)(_, 3)(1)`

See also curry.

  1. const sumArgs = (...args) => R.sum(args);
  2. const curriedAddFourNumbers = R.curryN(4, sumArgs);
  3. const f = curriedAddFourNumbers(1, 2);
  4. const g = f(3);
  5. g(4); //=> 10

__


Added in v0.6.0

A special placeholder value used to specify “gaps” within curried functions, allowing partial application of any combination of arguments, regardless of their positions.

If g is a curried ternary function and _ is R.__, the following are equivalent:

  • g(1, 2, 3)
  • g(_, 2, 3)(1)
  • g(_, _, 3)(1)(2)
  • g(_, _, 3)(1, 2)
  • g(_, 2, _)(1, 3)
  • g(_, 2)(1)(3)
  • g(_, 2)(1, 3)
  • g(_, 2)(_, 3)(1)
  1. const greet = R.replace('{name}', R.__, 'Hello, {name}!');
  2. greet('Alice'); //=> 'Hello, Alice!'

bind


(* → *) → {*} → (* → *)

Parameters

  • fnThe function to bind to context
  • thisObjThe context to bind fn to

Returns function A function that will execute in the context of thisObj.

Added in v0.6.0

Creates a function that is bound to a context. Note: R.bind does not provide the additional argument-binding capabilities of Function.prototype.bind.

See also partial.

  1. const log = R.bind(console.log, console);
  2. R.pipe(R.assoc('a', 2), R.tap(log), R.assoc('a', 3))({a: 1}); //=> {a: 3}
  3. // logs {a: 2}

apply


(*… → a) → [*] → a

Parameters

  • fnThe function which will be called with args
  • argsThe arguments to call fn with

Returns * result The result, equivalent to fn(...args)

Added in v0.7.0

Applies function fn to the argument list args. This is useful for creating a fixed-arity function from a variadic function. fn should be a bound function if context is significant.

See also call, unapply.

  1. const nums = [1, 2, 3, -99, 42, 6, 7];
  2. R.apply(Math.max, nums); //=> 42

lift


(*… → *) → ([*]… → [*])

Parameters

  • fnThe function to lift into higher context

Returns function The lifted function.

Added in v0.7.0

“lifts” a function of arity >= 1 so that it may “map over” a list, Function or other object that satisfies the FantasyLand Apply spec.

See also liftN.

  1. const madd3 = R.lift((a, b, c) => a + b + c);
  2. madd3([100, 200], [30, 40], [5, 6, 7]); //=> [135, 136, 137, 145, 146, 147, 235, 236, 237, 245, 246, 247]
  3. const madd5 = R.lift((a, b, c, d, e) => a + b + c + d + e);
  4. madd5([10, 20], [1], [2, 3], [4], [100, 200]); //=> [117, 217, 118, 218, 127, 227, 128, 228]

liftN


Number → (*… → *) → ([*]… → [*])

Parameters

  • fnThe function to lift into higher context

Returns function The lifted function.

Added in v0.7.0

“lifts” a function to be the specified arity, so that it may “map over” that many lists, Functions or other objects that satisfy the FantasyLand Apply spec.

See also lift, ap.

  1. const madd3 = R.liftN(3, (...args) => R.sum(args));
  2. madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]

unapply


([*…] → a) → (*… → a)

Added in v0.8.0

Takes a function fn, which takes a single array argument, and returns a function which:

  • takes any number of positional arguments;
  • passes these arguments to fn as an array; and
  • returns the result.

In other words, R.unapply derives a variadic function from a function which takes an array. R.unapply is the inverse of R.apply.

See also apply.

  1. R.unapply(JSON.stringify)(1, 2, 3); //=> '[1,2,3]'

call


((*… → a), *…) → a

Parameters

  • fnThe function to apply to the remaining arguments.
  • argsAny number of positional arguments.

Returns *

Added in v0.9.0

Returns the result of calling its first argument with the remaining arguments. This is occasionally useful as a converging function for R.converge: the first branch can produce a function while the remaining branches produce values to be passed to that function as its arguments.

See also apply.

  1. R.call(R.add, 1, 2); //=> 3
  2. const indentN = R.pipe(
  3. R.repeat(' '),
  4. R.join(''),
  5. R.replace(/^(?!$)/gm)
  6. );
  7. const format = R.converge(
  8. R.call,
  9. [
  10. R.pipe(R.prop('indent'), indentN),
  11. R.prop('value')
  12. ]
  13. );
  14. format({indent: 2, value: 'foo\nbar\nbaz\n'}); //=> ' foo\n bar\n baz\n'

F


* → Boolean

Added in v0.9.0

A function that always returns false. Any passed in parameters are ignored.

See also T.

  1. R.F(); //=> false

nthArg


Number → *… → *

Added in v0.9.0

Returns a function which returns its nth argument.

  1. R.nthArg(1)('a', 'b', 'c'); //=> 'b'
  2. R.nthArg(-1)('a', 'b', 'c'); //=> 'c'

T


* → Boolean

Added in v0.9.0

A function that always returns true. Any passed in parameters are ignored.

See also F.

  1. R.T(); //=> true