Files
30-seconds-of-code/snippets/when.md
Angelos Chalaris a9a09a746c Update when.md
2018-04-19 20:01:20 +03:00

19 lines
423 B
Markdown

### when
Tests a value, `x`, against a predicate function. If `true`, return `fn(x)`. Else, return `x`.
Return a function expecting a single value, `x`, that returns the appropriate value based on `pred`.
```js
const when = (pred, whenTrue) => (x) => pred(x) ? whenTrue(x) : x;
```
```js
const doubleEvenNumbers = when(
(x) => x % 2 === 0,
(x) => x * 2
);
doubleEvenNumbers(2); // 4
doubleEvenNumbers(1); // 1
```