Created
March 14, 2017 21:19
-
-
Save dotproto/2d75ec7cf06c58a48d3f20c9b902e008 to your computer and use it in GitHub Desktop.
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
// TODO: I was going through various alternative forms when I realized I was spending too much time on this. | |
let data = { | |
"a": "alpha", | |
"b": "beta,", | |
"c": "gamma", | |
"d": "delta", | |
"e": "epsilon", | |
"g": "eta", | |
"h": "theta", | |
"i": "iota", | |
"k": "kappa", | |
"l": "lambda", | |
"m": "mu", | |
"n": "nu", | |
"o": "omicron", | |
"p": "pi", | |
"r": "rho", | |
"s": "sigma", | |
"t": "tau", | |
"u": "phi", | |
"v": "chi", | |
"w": "psi", | |
"x": "omega", | |
"z": "zeta" | |
}; | |
// d = destination | |
// s = source | |
// == Object.assign used directly == | |
// Object.assign | |
// == Wrapper for Object.assign == | |
// w = wrapper | |
// Exp = expression | |
// Dcl = declaration | |
// Arw = arrow | |
// Wrapper expression | |
let wExp = function(d, s) { | |
return Object.assign(d, s); | |
} | |
// Wrapper declaration | |
function wDcl(d, s) { | |
return Object.assign(d, s); | |
} | |
let wArw = (d, s) => Object.assign(d, s); | |
// == forEach Entries == | |
// fe = for each | |
function fe1(d, s) { | |
function fe1In(v) { | |
d[v[0]] = v[1]; | |
} | |
Object.entries(s).forEach(fe1In); | |
return d; | |
} | |
function fe2(d, s) { | |
Object.entries(s).forEach(function fe2In(v) { | |
d[v[0]] = v[1]; | |
}); | |
return d; | |
} | |
function fe3(d, s) { | |
function fe3In([k, v]) { | |
d[k] = v; | |
} | |
Object.entries(s).forEach(fe3In); | |
return d; | |
} | |
function fe4(d, s) { | |
Object.entries(s).forEach(function fe4In([k, v]) { | |
d[k] = v; | |
}); | |
return d; | |
} | |
// == Reducing entries == | |
// re = reduce Object.entries | |
// ac = accumulator | |
// In = inner function | |
// Reduce entries w/ inline function expression | |
function re1(d, s) { | |
return Object.entries(s).reduce(function(ac, v){ | |
ac[v[0]] = v[1]; | |
return ac; | |
}, d); | |
} | |
// Reduce entries w/ static function definition | |
function re2(d, s) { | |
return Object.entries(s).reduce(re2In, d); | |
} | |
function re2In(ac, s) { | |
ac[v[0]] = v[1]; | |
return ac; | |
} | |
// Reduce entries w/ inline function expression - destructuring | |
function re3(d, s) { | |
return Object.entries(s).reduce(function(ac, [k, v]){ | |
ac[k] = v; | |
return ac; | |
}, d); | |
} | |
// Reduce entries w/ static function definition - destructuring | |
function re4(d, s) { | |
return Object.entries(s).reduce(re4In, d); | |
} | |
function re4In(ac, [k, v]) { | |
ac[k] = v; | |
return ac; | |
} | |
// == reduce keys == | |
// unsafe | |
function rk1(d, s) { | |
return Object.keys(s).reduce(function(ac, k) { | |
ac[k] = s[k]; | |
return ac; | |
}, d); | |
} | |
// safe | |
function rk2(d, s) { | |
return Object.keys(s).reduce(function(ac, k) { | |
if (s.hasOwnProperty(k)) { | |
ac[k] = s[k]; | |
} | |
return ac; | |
}, d); | |
} | |
// == for loop == | |
// Unsafe | |
function f1(d, s) { | |
for (var k in s) { | |
d[k] = s[k]; | |
} | |
return d; | |
} | |
// Safe | |
function f2(d, s) { | |
for (var k in s) { | |
if (s.hasOwnProperty(k)) { | |
d[k] = s[k]; | |
} | |
} | |
return d; | |
} | |
// Inline lamda | |
function fnReduceEntriesDcl1(d, s) { | |
return Object.entries(s) | |
.reduce(function(acc, e) { | |
acc[e[0]] = e[1]; | |
return acc; | |
}, {}); | |
} | |
function fnReduceDcl1 (d, s) { | |
return Object.entries(s) | |
.reduce(fnReduceInner, d); | |
} | |
let fnReduceExp1 = function(d, s) { | |
return Object.entries(s) | |
.reduce(fnReduceExp1Inner, d); | |
} | |
function fnReduceExp1Inner(acc, e) { | |
acc[e[0]]=e[1]; | |
return acc; | |
} | |
let arrowAssign = (d, s) => Object.assign(d, s); | |
let arrowReduce = (d, s) => Object.entries(s).reduce((acc, [k,v]) => (acc[k]=v, acc), d); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment