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.
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.
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?
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.
A practical functional library for JavaScript programmers.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With