如何使用 Ramda 管道?
How to use Ramda pipe?
背景
我正在尝试使用 Ramda 组合 2 个函数,但我遇到了 pipe
的问题,这意味着我不知道如何使用它。
代码
假设我有一个函数 returns 一个数组:
var createQuery = params => [ getSQLQuery( params ), [ getMarket() ] ];
var getSQLQuery = ( { lang } ) => `My query is ${lang}`;
var getMarket = () => "en_en"
所以,当调用 createQuery({ lang: "es" })
时,我将得到以下输出:[ "My query is es", ["en_en"] ]
;
现在,让我们也假设我是一个讨厌的男孩,我想挖掘这个重要信息!
R.tap(console.log, createQuery({lang: "es"}))
一个组合(准确地说,是一个管道)将是:
R.pipe(
createQuery( {lang: "en"} ),
R.tap(console.log)
);
其中returns一个函数。
问题
现在假设我要执行所述功能:
var comp = params => R.pipe(
createQuery( params ),
R.tap(console.log)
)(params);
comp({lang: "uk"}); //Blows Up!?
为什么我的函数会因 f.apply is not a function
而爆炸??
我做错了什么?
您遇到的问题是因为您正在调用 createQuery(params)
然后试图将结果作为函数处理。
您是示例函数 comp
可以像这样更新:
const comp = params => R.pipe(createQuery, R.tap(console.log))(params)
和 R.pipe
将传递 params
和参数给 createQuery
,然后将其结果提供给 `R.tap(console.log ).
这可以简化为以下内容,通过删除 params
的立即应用:
const comp = R.pipe(createQuery, R.tap(console.log));
背景
我正在尝试使用 Ramda 组合 2 个函数,但我遇到了 pipe
的问题,这意味着我不知道如何使用它。
代码
假设我有一个函数 returns 一个数组:
var createQuery = params => [ getSQLQuery( params ), [ getMarket() ] ];
var getSQLQuery = ( { lang } ) => `My query is ${lang}`;
var getMarket = () => "en_en"
所以,当调用 createQuery({ lang: "es" })
时,我将得到以下输出:[ "My query is es", ["en_en"] ]
;
现在,让我们也假设我是一个讨厌的男孩,我想挖掘这个重要信息!
R.tap(console.log, createQuery({lang: "es"}))
一个组合(准确地说,是一个管道)将是:
R.pipe(
createQuery( {lang: "en"} ),
R.tap(console.log)
);
其中returns一个函数。
问题
现在假设我要执行所述功能:
var comp = params => R.pipe(
createQuery( params ),
R.tap(console.log)
)(params);
comp({lang: "uk"}); //Blows Up!?
为什么我的函数会因 f.apply is not a function
而爆炸??
我做错了什么?
您遇到的问题是因为您正在调用 createQuery(params)
然后试图将结果作为函数处理。
您是示例函数 comp
可以像这样更新:
const comp = params => R.pipe(createQuery, R.tap(console.log))(params)
和 R.pipe
将传递 params
和参数给 createQuery
,然后将其结果提供给 `R.tap(console.log ).
这可以简化为以下内容,通过删除 params
的立即应用:
const comp = R.pipe(createQuery, R.tap(console.log));