0

I'm trying some basic FP techniques in JavaScript. The compose function takes two functions and give out their Composition function. Now I want to make compose a part of the Function such that I can chain it with ease.

var compose = (func1, func2) => (args => func2(func1(args)));
var square = x => x * x;
var cube = x => x * x * x;
var sixthPower = compose(cube, square);
console.log(sixthPower(2)); // 64
console.log(compose(cube, x => x)(3)); // 27
Function.prototype.compose = function(func) {
  return function(args) {
    func(/* not sure what to write here */)
  }
}
// to make this possible
console.log(square.compose(cube)(3));
2
  • What do you mean saying "their Composition function" Commented Jul 14, 2016 at 11:42
  • Composition Function of functions f1 and f2 is f3 such that f3(x) = f2(f1(x)) Commented Jul 14, 2016 at 11:44

2 Answers 2

3

You need to store the original this reference, so you can use it inside the returned function:

var square = x => x * x;
var cube = x => x * x * x;

Function.prototype.compose = function(func) {
  var self = this;
  return function(args) {
    return func(self(args))
  }
}

console.log(square.compose(cube)(3));

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, exactly what I was looking for, Thanks.
An alternative would be with the lambda syntax and thus not requiring this to be saved to a variable. Function.prototype.compose = function(func) { return (args) => func(this(args)) }
1

Personally I am against your approach because of the following reasons:

  1. Extending on Function prototype chain. The Prototype js framework is a good example indicating such actions is harmful. Here is an article from MDN talking about this in detail.

  2. Now compose is a method instead of a function.

I suggest you check the compose function on Ramda (a js functional library)

5 Comments

Or use function composition in its original sense: const comp = f => g => x => f(g(x)); comp(cube)(square)(3);
@YizhengShen ... Opinion: Function.prototype almost from its very beginning did feature apply and call. Later it even got assigned bind. Compose methods provide far more benefit than the latter. Function.prototype therefore is the perfect place for standard implementations of e.g. curry, compose, before, after, around (... only mentioning a few).
That makes me really sad: "Function.prototype therefore is the perfect place for standard implementations of e.g. curry, compose..."
@PeterSeliger I think it's better to favor composition over inheritance, like this: Object.assign(square, {compose(){...}}. I also find a link from MDN which talks about the extending native prototypes. PS: I'm not sure about this but I think apply, call, and bind is irrelevant here because they are for language problems, and now with ES6 there are spread operator, arrow function for binding lexical scope to help
I mean no offense for my following words. If you feel so, I apologize. I think function composition is closer to functional programming function composition than extending square with a compose function which only have the same name and function as the one in functional programming but lack what's indeed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.