最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use Ramda pipe? - Stack Overflow

programmeradmin2浏览0评论

Background

I am trying to pose 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 position ( 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 p = params => R.pipe( 
        createQuery( params ), 
        R.tap(console.log) 
    )(params);

p({lang: "uk"}); //Blows Up!?

Why is my function blowing up with f.apply is not a function ?? What am I doing wrong?

Background

I am trying to pose 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 position ( 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 p = params => R.pipe( 
        createQuery( params ), 
        R.tap(console.log) 
    )(params);

p({lang: "uk"}); //Blows Up!?

Why is my function blowing up with f.apply is not a function ?? What am I doing wrong?

Share Improve this question asked Jan 9, 2018 at 9:28 Flame_PhoenixFlame_Phoenix 17.6k40 gold badges144 silver badges284 bronze badges 3
  • The problem is that you don't have a type checker telling you what's wrong with the intermediate types (of your partially applied functions). – user6445533 Commented Jan 9, 2018 at 10:06
  • I am confused. I fail to see how this is an error that could e from type checking. – Flame_Phoenix Commented Jan 9, 2018 at 10:07
  • ftor, so passive aggressive lol – Mulan Commented Jan 9, 2018 at 15:31
Add a ment  | 

1 Answer 1

Reset to default 6

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 p can be updated like so:

const p = 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 p = R.pipe(createQuery, R.tap(console.log));
发布评论

评论列表(0)

  1. 暂无评论