Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Ramda pipe?

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?

like image 511
Flame_Phoenix Avatar asked Jan 09 '18 09:01

Flame_Phoenix


People also ask

What is ramda Curry?

Auto currying All functions in Ramda are auto-curried. This means that you can call the function with less parameters than it needs, and it will return a partially applied function. Functions will keep returning functions they have been called with their final parameter, at which point they will compute the result.

What is ramda JavaScript?

A practical functional library for JavaScript programmers.


1 Answers

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));
like image 84
Scott Christopher Avatar answered Oct 20 '22 20:10

Scott Christopher