受到this article on curry functions的启发,我试图将它们联系起来,并提出了这个解决方案。我不确定mixin,因为我不知何故觉得lodash可能已经有了这样的功能。如果是这样,这个函数叫什么?
var _ = require('lodash');
var get = _.curry(function(property, object) {return object[property]});
var map = _.curry(function(fn, objects){ return objects.map(fn) });
var json = {
"user": "hughfdjackson",
"posts": [
{ "title": "why curry?", "contents": "..." },
{ "title": "prototypes: the short(est possible) story", "contents": "..." }
]
}
_.mixin({
then:function(input, fn) {return fn(input);}
});
_(json)
.then(get('posts'))
.then(map(get('title')))
.tap(console.log)