将咖喱函数与 lodash 链接起来

chaining curry functions with lodash

本文关键字:lodash 链接 起来 函数      更新时间:2023-09-26

受到这篇关于咖喱函数的文章的启发,我试图将它们链接起来并提出了这个解决方案。不过,我不确定混合,因为我不知何故觉得 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)

在 Hugh Jackson 的(优秀!)文章中,then函数可能来自 Promise 库,可能与 AJAX 调用相关联。 因此,如果您从那里开始,则不需要自己做。 像这样将其混合到_中确实看起来很奇怪。

Lo-Dash和Underscore将允许您使用其curry功能执行此操作。 一些较新的库(如 RamdaFKit)会自动执行此操作。 他们的getmap版本已经变得柯里化,并被设计为以这种方式使用。(披露:我是Ramda的作者之一)