如何使用拉姆达管道? [英] How to use Ramda pipe?
问题描述
我试图用Ramda编写2个函数,但是我遇到了 pipe
的问题,这意味着我不知道如何使用它。
代码
假设我有一个函数返回一个数组:
var createQuery = params => [getSQLQuery(params),[getMarket()]];
var getSQLQuery =({lang})=> `我的查询是$ {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)
);
其中返回一个函数。
问题
现在让我们说我想执行这个函数:
var comp = params => R.pipe(
createQuery(params),
R.tap(console.log)
)(params);
comp({lang:uk}); //炸毁!?
为什么我用 f.apply炸掉的函数不是函数
??
我在做什么错了?
您遇到的问题是因为您正在调用 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));
Background
I am trying to compose 2 functions using Ramda, but I am having issue with pipe
, meaning I don't know how to use it.
Code
Let's imagine I have a function that returns an array:
var createQuery = params => [ getSQLQuery( params ), [ getMarket() ] ];
var getSQLQuery = ( { lang } ) => `My query is ${lang}`;
var getMarket = () => "en_en"
So, when calling createQuery({ lang: "es" })
I would have the following output: [ "My query is es", ["en_en"] ]
;
Now, let's also assume I am a nasty boy, and I wanna tap this vital information!
R.tap(console.log, createQuery({lang: "es"}))
A composition ( well, a pipe, to be precise ) would be:
R.pipe(
createQuery( {lang: "en"} ),
R.tap(console.log)
);
Which returns a function.
Problem
Now let's say I want to execute said function:
var comp = params => R.pipe(
createQuery( params ),
R.tap(console.log)
)(params);
comp({lang: "uk"}); //Blows Up!?
Why is my function blowing up with f.apply is not a function
??
What am I doing wrong?
The problem you're experiencing is because you are calling createQuery(params)
and then trying to treat the result as a function.
You're example function comp
can be updated like so:
const comp = params => R.pipe(createQuery, R.tap(console.log))(params)
and R.pipe
will pass params
and argument to createQuery
, the result of which is then given to the result of `R.tap(console.log).
This can be simplified to the following, by removing the immediate application of params
:
const comp = R.pipe(createQuery, R.tap(console.log));
这篇关于如何使用拉姆达管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!