Created
December 23, 2013 17:22
-
-
Save robotlolita/8101034 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
// Returns a list of `[key, value]` pairs for an object | |
function values(object) { | |
return Object.keys(object).map(function(key) { | |
return [key, object[key]] | |
}) | |
} | |
// Computes something by applying a binary function to | |
// a seed value, and a `[key, value]` pair. | |
function foldRight(object, seed, computation) { | |
return values(object).reduce(function(accumulated, pair) { | |
return computation(pair[0], pair[1], accumulated) | |
}, seed) | |
} | |
// Returns all items of an array, but the first one. | |
function rest(a) { | |
return a.slice(1) | |
} | |
// Compute a new object with basis on the previous object | |
foldRight({"Line1": "Hi", "LINE_2": "Sorella", "line03":"How are you"} // The object | |
,[{}, ["Line1", "LINE_2", "line03"]] // The seed (what will be the starting point of our computation) | |
,function(key, value, context) { | |
var result = context[0] | |
var keys = context[1] | |
if (key === "LINE_2") return [result, keys] | |
else { | |
result[keys[0]] = value | |
return [result, rest(keys)] | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment