Update when.md
This commit is contained in:
@ -1,7 +1,8 @@
|
|||||||
### when
|
### when
|
||||||
|
|
||||||
Tests a value, `x`, against a predicate function. If `true`, return `fn(x)`. Else, return `x`.
|
Tests a value, `x`, against a predicate function. If `true`, return `fn(x)`. Else, return `x`.
|
||||||
`when(pred, whenTrue)` returns a function expecting `x` for better composability.
|
|
||||||
|
Return a function expecting a single value, `x`, that returns the appropriate value based on `pred`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const when = (pred, whenTrue) => (x) => pred(x) ? whenTrue(x) : x;
|
const when = (pred, whenTrue) => (x) => pred(x) ? whenTrue(x) : x;
|
||||||
@ -12,7 +13,6 @@ const doubleEvenNumbers = when(
|
|||||||
(x) => x % 2 === 0,
|
(x) => x % 2 === 0,
|
||||||
(x) => x * 2
|
(x) => x * 2
|
||||||
);
|
);
|
||||||
|
doubleEvenNumbers(2); // 4
|
||||||
doubleEvenNumbers(2) // 4
|
doubleEvenNumbers(1); // 1
|
||||||
doubleEvenNumbers(1) // 1
|
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user