Using when/unless in functional pipelines (4 min. read)

Sometimes you only need the if statement, and the else simply returns the value unchanged.

  1. const isEven = (num) => num % 2 === 0;
  2. const doubleIfEven = (num) => {
  3. if (isEven(num)) {
  4. return num * 2;
  5. }
  6. return num;
  7. };
  8. console.log(
  9. doubleIfEven(100),
  10. doubleIfEven(101)
  11. );

Again, ternaries can work well here. 三元

  1. const isEven = (num) => num % 2 === 0;
  2. const doubleIfEven = (num) => isEven(num) ? num * 2 : num;
  3. console.log(
  4. doubleIfEven(100),
  5. doubleIfEven(101)
  6. );

But we can also use the when function. It takes three arguments

  1. Predicate (function that returns true or false)
  2. Function to run if predicate returns true
  3. The value to use

    但是我们也可以使用when函数。
    这需要三个论点 谓词(返回true或false的函数) 当谓词返回true时要运行的函数 使用的价值
    ```javascript import { when } from ‘ramda’;

const isEven = (num) => num % 2 === 0;

const doubleIfEven = when( isEven, (num) => num * 2 );

console.log( doubleIfEven(100), doubleIfEven(101) );

  1. We can make it point-free. <br /> 我们可以让它自由指向。
  2. ```javascript
  3. import { multiply, when } from 'ramda';
  4. const isEven = (num) => num % 2 === 0;
  5. const doubleIfEven = when(
  6. isEven,
  7. multiply(2)
  8. );
  9. console.log(
  10. doubleIfEven(100),
  11. doubleIfEven(101)
  12. );

Conveniently enough, Ramda lets you express the opposite logic using unless.
This runs your function if the predicate returns false.
非常方便,Ramda允许您使用除非来表达相反的逻辑。 如果谓词返回false,则该函数将运行。

  1. import { multiply, unless } from 'ramda';
  2. const isEven = (num) => num % 2 === 0;
  3. const doubleIfOdd = unless(
  4. isEven,
  5. multiply(2)
  6. );
  7. console.log(
  8. doubleIfOdd(100),
  9. doubleIfOdd(101)
  10. );

Now this function only doubles odd numbers. If we wanted doubleIfEven, our predicate must flip as well.

  1. import { multiply, unless } from 'ramda';
  2. const isOdd = (num) => num % 2 !== 0;
  3. const doubleIfEven = unless(
  4. isOdd,
  5. multiply(2)
  6. );
  7. console.log(
  8. doubleIfEven(100),
  9. doubleIfEven(101)
  10. );

https://ramdajs.com/docs/#multiply 数学乘法

Multiplies two numbers. Equivalent to a * b but curried.
See also divide.

  1. const double = R.multiply(2);
  2. const triple = R.multiply(3);
  3. double(3); //=> 6
  4. triple(4); //=> 12
  5. R.multiply(2, 5); //=> 10

https://ramdajs.com/docs/#when 逻辑条件

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…'

propSatisfieshttps://ramdajs.com/docs/#propSatisfies

逻辑
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

takehttps://ramdajs.com/docs/#take

List

Returns the first n elements of the given list, string, or transducer/transformer (or object with a take method).
Dispatches to the take method of the second argument, if present.

  1. R.take(1, ['foo', 'bar', 'baz']); //=> ['foo']
  2. R.take(2, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
  3. R.take(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  4. R.take(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  5. R.take(3, 'ramda'); //=> 'ram'
  6. const personnel = [
  7. 'Dave Brubeck',
  8. 'Paul Desmond',
  9. 'Eugene Wright',
  10. 'Joe Morello',
  11. 'Gerry Mulligan',
  12. 'Bob Bates',
  13. 'Joe Dodge',
  14. 'Ron Crotty'
  15. ];
  16. const takeFive = R.take(5);
  17. takeFive(personnel);
  18. //=> ['Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan']

appendhttps://ramdajs.com/docs/#append

appendList

a → [a] → [a]
Parameters
Added in v0.1.0
Returns a new list containing the contents of the given list, followed by the given element.
See also prepend.

  1. R.append('tests', ['write', 'more']); //=> ['write', 'more', 'tests']
  2. R.append('tests', []); //=> ['tests']
  3. R.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]

joinhttps://ramdajs.com/docs/#join

joinList

String → [a] → String
Parameters
Added in v0.1.0
Returns a string made by inserting the separator between each element and concatenating all the elements into a single string.
See also split.

  1. const spacer = R.join(' ');
  2. spacer(['a', 2, 3.4]); //=> 'a 2 3.4'
  3. R.join('|', [1, 2, 3]); //=> '1|2|3'

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

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