I'm new to functional programming and am trying to implement a multiplication -> addition function using in typescript with rambda.
I want a function that takes three numbers, multiplies two together, then adds the result to the third, like so: f(a, b, c) = a + (b * c)
. Here is the progress I've made so far:
Imperative style
const f = (a: number, b: number, c: number) => a + b * c;
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 problem with the above solution is the lack of readability of the function f
, which is why I am trying to implement the same with Rambda. What I want to be able to say is something like const f = R.pipe(mul, add);
, but I'm not sure how to define the functions add
and mul
to allow that.
Here is what I tried:
const add = (a: number) => (b: number) => a + b;
const mul = (a: number) => (b: number) => a * b;
const f = R.pipe(mul, add);
And the resulting 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'.