Last active
October 31, 2025 14:46
-
-
Save achinaou/617145ba2c6a1ac4c959687741d3d7ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const identity = x => x | |
| const of = x => [x] | |
| const empty = () => [] | |
| const concat = (xs, ys) => [...xs, ...ys] | |
| const foldRight = f => acc => ([x, ...xs]) => x | |
| ? f(x, foldRight(f)(acc)(xs)) | |
| : acc | |
| const foldLeft = f => acc => ([x, ...xs]) => x | |
| ? foldLeft(f)(f(acc, x))(xs) | |
| : acc | |
| const map = f => foldLeft((acc, value) => concat(acc, of(f(value))))(empty()) | |
| const flatMap = f => foldLeft((acc, value) => concat(acc, f(value)))(empty()) | |
| const filter = f => foldLeft((acc, value) => f(value) ? concat(acc, of(value)) : acc)(empty()) | |
| const compose = (...args) => foldLeft((acc, value) => x => acc(value(x)))(identity)(args) | |
| const process = compose( | |
| foldLeft((acc, value) => acc + value)(0), | |
| map(x => x * 5), | |
| filter(x => x % 2 === 0), | |
| flatMap(identity) | |
| ) | |
| const input = [[1, 2], [3, 4], [5, 6], [7, 8]] | |
| const output = process(input) | |
| console.log(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment