I'm just getting started with functional programming and I'm attempting to create a function that multiplies two numbers together and then adds the result to a third number using TypeScript with rambda.
My goal is to have a function that takes three numbers, performs the operation as follows: f(a, b, c) = a + (b * c)
. Here's what I've accomplished so far:
Using Imperative style
const f = (a: number, b: number, c: number) => a + b * c;
Using Functional style
const add = (a: number) => (b: number) => a + b;
const mul = (a: number) => (b: number) => a * b;
const f = (a: number, b: number, c: number) => add(a)(mul(b)(c));
The issue with the above solution is that the function f
lacks readability, which is why I am trying to achieve the same outcome with Rambda. My desired approach would be something like const f = R.pipe(mul, add);
, but I'm unsure how to define the functions add
and mul
to make that work.
Here is my attempt:
const add = (a: number) => (b: number) => a + b;
const mul = (a: number) => (b: number) => a * b;
const f = R.pipe(mul, add);
However, this results in an error:
Argument of type '(a: number) => (b: number) => number' is not assignable to parameter of type '(a: number) => number'.
Type '(b: number) => number' is not assignable to type 'number'.