Last active
July 31, 2019 12:10
-
-
Save monochromer/99c55e316b6143841d7a4231e87f50c7 to your computer and use it in GitHub Desktop.
Decoupling methods Композиция Compose
This file contains 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 compose = (...fns) => x => fns.reduce((v, fn) => fn(v), x); | |
// | |
const pipe = (...fns) => (...args) => fns | |
.reduce((args, fn) => [fn(...args)], args); | |
const compose = (...fns) => (...args) => fns.reverse() | |
.reduce((args, fn) => [fn(...args)], args); | |
// --- | |
const compose = (...fns) => (...args) => { | |
if (fns.length === 0) return args[0]; | |
let res = fns[0](...args); | |
for (let i = 1; i < fns.length; i++) { | |
res = fns[i](res); | |
} | |
return res; | |
}; |
This file contains 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
// https://medium.com/devschacht/joel-thoms-functional-javascript-decoupling-methods-from-their-objects-9a2686096418 | |
const map = fn => ctx => Array.prototype.map.call(ctx, fn); | |
const filter = fn => ctx => Array.prototype.filter.call(ctx, fn); | |
const reduce = fn => ctx => Array.prototype.reduce.call(ctx, fn); | |
// ============================================================================= | |
const items = document.querySelectorAll('div'); | |
map(doSomething)(items) | |
// вы можете написать каждую функцию списка с reduceRight? | |
// https://medium.com/devschacht/tom-harding-reductio-and-abstract-em-b42b956efe56 | |
const head = ([x, ...xs]) => x | |
const cons = ( x, xs ) => [x, ...xs] | |
const map = f => xs => xs.reduceRight( | |
(acc, x) => cons(f(x), acc), | |
[] | |
) | |
// [2, 3, 4] | |
map(x => x + 1)([1, 2, 3]) | |
const filter = p => xs => xs.reduceRight( | |
(acc, x) => p(x) ? cons(x, acc) | |
: acc, | |
[] | |
) | |
// [1, 2] | |
filter(x => x < 3)([1, 2, 3, 4]) | |
const append = x => xs => xs.reduceRight( | |
(acc, h) => cons(h, acc), | |
[x] | |
) | |
// [1, 2, 3, 4] | |
append(4)([1, 2, 3]) | |
const concat = xs => ys => | |
xs.reduceRight( | |
(acc, h) => cons(h, acc), | |
ys | |
) | |
// [1, 2, 3, 4] | |
concat([1, 2])([3, 4]) | |
const reverse = xs => xs.reduceRight( | |
(acc, x) => append(x, acc), [] | |
) | |
// [3, 2, 1] | |
reverse([1, 2, 3]) | |
const length = xs => xs.reduceRight( | |
(n, _) => n + 1, 0 | |
) | |
// 4 | |
length([1, 2, 3, 4]) | |
const elemAt = (n, xs) => head(xs.reduce( | |
([e, n], x) => [n == 0 ? x : e, n - 1], | |
[undefined, n] | |
)) | |
// 3 | |
elemAt(2, [1, 2, 3]) | |
const reduce = (f, acc, xs) => | |
xs.reduceRight( | |
(accF, x) => z => accF(f(z, x)), | |
x => x | |
)(acc) | |
// 7 | |
reduce((x, y) => x - y, 10, [1, 2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment