and


a → b → a | b

Added in v0.1.0

Returns the first argument if it is falsy, otherwise the second argument. Acts as the boolean and statement if both inputs are Booleans.

See also both, or.

  1. R.and(true, true); //=> true
  2. R.and(true, false); //=> false
  3. R.and(false, true); //=> false
  4. R.and(false, false); //=> false

isEmpty


a → Boolean

Added in v0.1.0

Returns true if the given value is its type’s empty value; false otherwise.

See also empty, isNotEmpty.

  1. R.isEmpty([1, 2, 3]); //=> false
  2. R.isEmpty([]); //=> true
  3. R.isEmpty(''); //=> true
  4. R.isEmpty(null); //=> false
  5. R.isEmpty({}); //=> true
  6. R.isEmpty({length: 0}); //=> false
  7. R.isEmpty(Uint8Array.from('')); //=> true

not


* → Boolean

Parameters

  • aany value

Returns Boolean the logical inverse of passed argument.

Added in v0.1.0

A function that returns the ! of its argument. It will return true when passed false-y value, and false when passed a truth-y one.

See also complement.

  1. R.not(true); //=> false
  2. R.not(false); //=> true
  3. R.not(0); //=> true
  4. R.not(1); //=> false

or


a → b → a | b

Added in v0.1.0

Returns the first argument if it is truthy, otherwise the second argument. Acts as the boolean or statement if both inputs are Booleans.

See also either, and.

  1. R.or(true, true); //=> true
  2. R.or(true, false); //=> true
  3. R.or(false, true); //=> true
  4. R.or(false, false); //=> false

cond


[[(*… → Boolean),(*… → *)]] → (*… → *)

Parameters

  • pairsA list of [predicate, transformer]

Returns function

Added in v0.6.0

Returns a function, fn, which encapsulates if/else, if/else, ... logic. R.cond takes a list of [predicate, transformer] pairs. All of the arguments to fn are applied to each of the predicates in turn until one returns a “truthy” value, at which point fn returns the result of applying its arguments to the corresponding transformer. If none of the predicates matches, fn returns undefined.

Please note: This is not a direct substitute for a switch statement. Remember that both elements of every pair passed to cond are functions, and cond returns a function.

See also ifElse, unless, when.

  1. const fn = R.cond([
  2. [R.equals(0), R.always('water freezes at 0°C')],
  3. [R.equals(100), R.always('water boils at 100°C')],
  4. [R.T, temp => 'nothing special happens at ' + temp + '°C']
  5. ]);
  6. fn(0); //=> 'water freezes at 0°C'
  7. fn(50); //=> 'nothing special happens at 50°C'
  8. fn(100); //=> 'water boils at 100°C'

ifElse


(*… → Boolean) → (*… → *) → (*… → *) → (*… → *)

Parameters

  • conditionA predicate function
  • onTrueA function to invoke when the condition evaluates to a truthy value.
  • onFalseA function to invoke when the condition evaluates to a falsy value.

Returns function A new function that will process either the onTrue or the onFalse function depending upon the result of the `condition` predicate.

Added in v0.8.0

Creates a function that will process either the onTrue or the onFalse function depending upon the result of the condition predicate.

Note that ifElse takes its arity from the longest of the three functions passed to it.

See also unless, when, cond.

  1. const incCount = R.ifElse(
  2. R.has('count'),
  3. R.over(R.lensProp('count'), R.inc),
  4. R.assoc('count', 1)
  5. );
  6. incCount({ count: 1 }); //=> { count: 2 }
  7. incCount({}); //=> { count: 1 }

allPass


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

Parameters

  • predicatesAn array of predicates to check

Returns function The combined predicate

Added in v0.9.0

Takes a list of predicates and returns a predicate that returns true for a given list of arguments if every one of the provided predicates is satisfied by those arguments.

The function returned is a curried function whose arity matches that of the highest-arity predicate.

See also anyPass, both.

  1. const isQueen = R.propEq('Q', 'rank');
  2. const isSpade = R.propEq('♠︎', 'suit');
  3. const isQueenOfSpades = R.allPass([isQueen, isSpade]);
  4. isQueenOfSpades({rank: 'Q', suit: '♣︎'}); //=> false
  5. isQueenOfSpades({rank: 'Q', suit: '♠︎'}); //=> true

anyPass


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

Parameters

  • predicatesAn array of predicates to check

Returns function The combined predicate

Added in v0.9.0

Takes a list of predicates and returns a predicate that returns true for a given list of arguments if at least one of the provided predicates is satisfied by those arguments.

The function returned is a curried function whose arity matches that of the highest-arity predicate.

See also allPass, either.

  1. const isClub = R.propEq('♣', 'suit');
  2. const isSpade = R.propEq('♠', 'suit');
  3. const isBlackCard = R.anyPass([isClub, isSpade]);
  4. isBlackCard({rank: '10', suit: '♣'}); //=> true
  5. isBlackCard({rank: 'Q', suit: '♠'}); //=> true
  6. isBlackCard({rank: 'Q', suit: '♦'}); //=> false

defaultTo


a → b → a | b

Parameters

  • defaultThe default value.
  • valval will be returned instead of default unless val is null, undefined or NaN.

Returns * The second value if it is not null, undefined or NaN, otherwise the default value

Added in v0.10.0

Returns the second argument if it is not null, undefined or NaN; otherwise the first argument is returned.

  1. const defaultTo42 = R.defaultTo(42);
  2. defaultTo42(null); //=> 42
  3. defaultTo42(undefined); //=> 42
  4. defaultTo42(false); //=> false
  5. defaultTo42('Ramda'); //=> 'Ramda'
  6. // parseInt('string') results in NaN
  7. defaultTo42(parseInt('string')); //=> 42

both


(*… → Boolean) → (*… → Boolean) → (*… → Boolean)

Parameters

  • fA predicate
  • gAnother predicate

Returns function a function that applies its arguments to f and g and &&s their outputs together.

Added in v0.12.0

A function which calls the two provided functions and returns the && of the results. It returns the result of the first function if it is false-y and the result of the second function otherwise. Note that this is short-circuited, meaning that the second function will not be invoked if the first returns a false-y value.

In addition to functions, R.both also accepts any fantasy-land compatible applicative functor.

See also either, allPass, and.

  1. const gt10 = R.gt(R.__, 10)
  2. const lt20 = R.lt(R.__, 20)
  3. const f = R.both(gt10, lt20);
  4. f(15); //=> true
  5. f(30); //=> false
  6. R.both(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(false)
  7. R.both([false, false, 'a'], [11]); //=> [false, false, 11]

complement


(*… → *) → (*… → Boolean)

Added in v0.12.0

Takes a function f and returns a function g such that if called with the same arguments when f returns a “truthy” value, g returns false and when f returns a “falsy” value g returns true.

R.complement may be applied to any functor

See also not.

  1. const isNotNil = R.complement(R.isNil);
  2. R.isNil(null); //=> true
  3. isNotNil(null); //=> false
  4. R.isNil(7); //=> false
  5. isNotNil(7); //=> true

either


(*… → Boolean) → (*… → Boolean) → (*… → Boolean)

Parameters

  • fa predicate
  • ganother predicate

Returns function a function that applies its arguments to f and g and ||s their outputs together.

Added in v0.12.0

A function wrapping calls to the two functions in an || operation, returning the result of the first function if it is truth-y and the result of the second function otherwise. Note that this is short-circuited, meaning that the second function will not be invoked if the first returns a truth-y value.

In addition to functions, R.either also accepts any fantasy-land compatible applicative functor.

See also both, anyPass, or.

  1. const gt10 = x => x > 10;
  2. const even = x => x % 2 === 0;
  3. const f = R.either(gt10, even);
  4. f(101); //=> true
  5. f(8); //=> true
  6. R.either(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(55)
  7. R.either([false, false, 'a'], [11]) // => [11, 11, "a"]

propSatisfies


(a → Boolean) → String → {String: a} → Boolean

Parameters

  • pred
  • name
  • obj

Returns Boolean

Added in v0.16.0

Returns true if the specified object property satisfies the given predicate; false otherwise. You can test multiple properties with R.where.

See also where, propEq, propIs.

  1. R.propSatisfies(x => x > 0, 'x', {x: 1, y: 2}); //=> true

unless


(a → Boolean) → (a → b) → a → a | b

Parameters

  • predA predicate function
  • whenFalseFnA function to invoke when the pred evaluates to a falsy value.
  • xAn object to test with the pred function and pass to whenFalseFn if necessary.

Returns * Either x or the result of applying x to whenFalseFn.

Added in v0.18.0

Tests the final argument by passing it to the given predicate function. If the predicate is not satisfied, the function will return the result of calling the whenFalseFn function with the same argument. If the predicate is satisfied, the argument is returned as is.

See also ifElse, when, cond.

  1. let safeInc = R.unless(R.isNil, R.inc);
  2. safeInc(null); //=> null
  3. safeInc(1); //=> 2

when


(a → Boolean) → (a → b) → a → a | b

Parameters

  • predA predicate function
  • whenTrueFnA function to invoke when the condition evaluates to a truthy value.
  • xAn object to test with the pred function and pass to whenTrueFn if necessary.

Returns * Either x or the result of applying x to whenTrueFn.

Added in v0.18.0

Tests the final argument by passing it to the given predicate function. If the predicate is satisfied, the function will return the result of calling the whenTrueFn function with the same argument. If the predicate is not satisfied, the argument is returned as is.

See also ifElse, unless, cond.

  1. // truncate :: String -> String
  2. const truncate = R.when(
  3. R.propSatisfies(R.gt(R.__, 10), 'length'),
  4. R.pipe(R.take(10), R.append('…'), R.join(''))
  5. );
  6. truncate('12345'); //=> '12345'
  7. truncate('0123456789ABC'); //=> '0123456789…'

pathSatisfies


(a → Boolean) → [Idx] → {a} → Boolean

Idx = String | Int | Symbol

Parameters

  • pred
  • propPath
  • obj

Returns Boolean

Added in v0.19.0

Returns true if the specified object property at given path satisfies the given predicate; false otherwise.

See also propSatisfies, path.

  1. R.pathSatisfies(y => y > 0, ['x', 'y'], {x: {y: 2}}); //=> true
  2. R.pathSatisfies(R.is(Object), [], {x: {y: 2}}); //=> true

until


(a → Boolean) → (a → a) → a → a

Parameters

  • predA predicate function
  • fnThe iterator function
  • initInitial value

Returns * Final value that satisfies predicate

Added in v0.20.0

Takes a predicate, a transformation function, and an initial value, and returns a value of the same type as the initial value. It does so by applying the transformation until the predicate is satisfied, at which point it returns the satisfactory value.

  1. R.until(R.gt(R.__, 100), R.multiply(2))(1) // => 128

xor


a → b → Boolean

Parameters

  • a
  • b

Returns Boolean true if one of the arguments is truthy and the other is falsy

Added in v0.27.1

Exclusive disjunction logical operation. Returns true if one of the arguments is truthy and the other is falsy. Otherwise, it returns false.

See also or, and.

  1. R.xor(true, true); //=> false
  2. R.xor(true, false); //=> true
  3. R.xor(false, true); //=> true
  4. R.xor(false, false); //=> false

isNotEmpty


a → Boolean

Added in v0.29.2

Returns false if the given value is its type’s empty value; true otherwise.

See also empty, isEmpty.

  1. R.isNotEmpty([1, 2, 3]); //=> true
  2. R.isNotEmpty([]); //=> false
  3. R.isNotEmpty(''); //=> false
  4. R.isNotEmpty(null); //=> true
  5. R.isNotEmpty({}); //=> false
  6. R.isNotEmpty({length: 0}); //=> true
  7. R.isNotEmpty(Uint8Array.from('')); //=> false