Last active
April 20, 2018 20:08
-
-
Save hiredgunhouse/1dfb7ee387d09f7030e3b29bc7ec50bc to your computer and use it in GitHub Desktop.
trying to make a point free version of assocPaths
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
import R, { assocPath, apply, compose, flip, reduce, reverse, zip } from 'ramda' | |
const assocPaths = (paths, values, obj) => { | |
const pathsValues = zip(paths, values) | |
const assocPathProxy = (obj, pathValue) => assocPath(pathValue[0], pathValue[1], obj) | |
return reduce(assocPathProxy, obj, pathsValues) | |
} | |
const assocPathsV2 = (paths, values, obj) => | |
reduce( | |
(obj, pathValue) => assocPath(pathValue[0], pathValue[1], obj), | |
obj, | |
zip(paths, values) | |
) | |
const assocPathsV3 = | |
flip( | |
reduce( | |
(obj, pathValue) => assocPath(pathValue[0], pathValue[1], obj), | |
) | |
) | |
const assocPathsV4 = | |
flip( | |
reduce( | |
compose( | |
apply(flip(assocPath)), | |
reverse, | |
Array.prototype.concat, | |
) | |
) | |
) | |
const r1 = assocPaths([['a', 'b'], ['c', 'd']], [1, 2], {a: 1, e: 2}) | |
r1 | |
const r2 = assocPathsV2([['a', 'b'], ['c', 'd']], [1, 2], {a: 1, e: 2}) | |
r2 | |
const r3 = assocPathsV3([[['a', 'b'], 1], [['c', 'd'], 2]], {a: 1, e: 2}) | |
r3 | |
const r4 = assocPathsV4([[['a', 'b'], 1], [['c', 'd'], 2]], {a: 1, e: 2}) | |
r4 | |
const flippedConcat = flip(Array.prototype.concat) | |
// const flippedConcat = flip([].concat) | |
const a1 = Array.prototype.concat({}, [['a', 'b'], [1]]) | |
// const a1 = Array.prototype.concat([['a', 'b'], [1]], {}) | |
a1 | |
const a2 = flippedConcat({}, [['a', 'b'], [1]]) | |
a2 | |
const a3 = Array.prototype.reverse.call(a1) | |
// const a3 = [].reverse.call(a1) | |
a3 | |
var x = [].push(2) | |
x | |
// var x = Array.prototype.push.call([], null) | |
// x | |
var y = apply(flip(assocPath), [1, ['a', 'b'], {}]) | |
y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment