gpt4 book ai didi

javascript - 使用 lodash 链接 curry 函数

转载 作者:行者123 更新时间:2023-11-29 19:31:49 32 4
gpt4 key购买 nike

灵感来自 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)

最佳答案

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

Lo-Dash 和 Underscore 将允许您使用它们的 curry 函数来执行此操作。一些较新的库,例如 Ramda FKit 自动执行此操作。他们的 getmap 版本已经柯里化(Currying),并且设计为以这种方式使用。 (披露:我是 Ramda 的作者之一)

关于javascript - 使用 lodash 链接 curry 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27044793/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com