cond

Using cond in functional pipelines (5 min. read)

Sometimes we have too many conditions, making switch statements a great choice.

  1. const findAnimal = (animal) => {
  2. switch (animal) {
  3. case 'lion':
  4. return 'Africa and India';
  5. case 'tiger':
  6. return 'China, Russia, India, Vietnam, and many more';
  7. case 'hyena':
  8. return 'African Savannah'
  9. case 'grizzly bear':
  10. return 'North America';
  11. default:
  12. return 'Not sure, try Googling it!'
  13. }
  14. };
  15. console.log(findAnimal('cow'));

We could mimic this with pipe and when, but coding for a default case is tough.

  1. import { always, equals, ifElse, pipe, when } from 'ramda';
  2. const findAnimal = pipe(
  3. when(equals('lion'), always('Africa and India')),
  4. when(equals('tiger'), always('China, Russia, India, Vietnam, and many more')),
  5. when(equals('hyena'), always('African Savannah')),
  6. when(equals('grizzly bear'), always('North America')),
  7. );
  8. console.log(findAnimal('cow'));

Or we can use cond, which is built into languages like Lisp. It takes an array of if/then statements, which are arrays themselves.
The first function is the predicate, and the second function is what to run if the predicate returns true.
Here’s an example
或者我们可以使用cond,它内置于像Lisp这样的语言中。它需要一个if/then语句数组,这些语句本身就是数组。 第一个函数是谓词,第二个函数是谓词返回true时要运行的函数。 这里有一个例子

  1. import { always, cond, equals } from 'ramda';
  2. const findAnimal = cond([
  3. [equals('lion'), always('Africa and India')],
  4. [equals('tiger'), always('China, Russia, India, Vietnam, and many more')],
  5. [equals('hyena'), always('African Savannah')],
  6. [equals('grizzly bear'), always('North America')]
  7. ]);
  8. console.log(findAnimal('lion'));

It runs through each array. If the array’s first function returns true, the array’s second function is called and the logic’s cut off.
But how do we support the default case?

  1. default:
  2. return 'Not sure, try Googling it!';

Have a function that always returns true at the very end. If nothing else runs, it will succeed and be called!

  1. import { always, cond, equals } from 'ramda';
  2. const findAnimal = cond([
  3. [equals('lion'), always('Africa and India')],
  4. [equals('tiger'), always('China, Russia, India, Vietnam, and many more')],
  5. [equals('hyena'), always('African Savannah')],
  6. [equals('grizzly bear'), always('North America')],
  7. [always(true), always('Not sure, try Googling it!')]
  8. ]);
  9. console.log(findAnimal('cow'));

condLogic

[[(… → Boolean),(… → )]] → (… → *)
Parameters
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.
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'

https://ramdajs.com/docs/#T

TFunction

  • → Boolean
    Parameters
    Added in v0.9.0
    A function that always returns true. Any passed in parameters are ignored.
    See also F.
    1. R.T(); //=> true

    https://ramdajs.com/docs/#F

    FFunction

  • → Boolean
    Parameters
    Added in v0.9.0
    A function that always returns false. Any passed in parameters are ignored.
    See also T.
    1. R.F(); //=> false