5

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?

Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266

1 Answers1

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 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));
Scott Christopher
  • 6,458
  • 23
  • 26
  • Ok, fair enough. But what if `comp` takes 2 params instead of 1, and `createQuery` only uses the second argument? How would you do it then? Also, upvoted! – Flame_Phoenix Jan 09 '18 at 10:53
  • 1
    @Flame_Phoenix How do you mean? Like this `const comp = (params1, params2) => R.pipe(createQuery, R.tap(console.log))(params2)` ? An alternative to that would be `const comp = R.flip(R.pipe)(createQuery, R.tap(console.log));` if I am not mistaken. – fredrik.hjarner Jan 09 '18 at 18:32