Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Created December 23, 2013 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotlolita/8101034 to your computer and use it in GitHub Desktop.
Save robotlolita/8101034 to your computer and use it in GitHub Desktop.
// 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