-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompose.js
More file actions
30 lines (25 loc) · 878 Bytes
/
Compose.js
File metadata and controls
30 lines (25 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Compose should return a function that is the composition of a list of functions of arbitrary length.
// Each function is called on the return value of the function that follows.
// You can think of compose as moving right to left through its arguments.
'use strict';
// Example 1
var greet = function(name){ return 'hi: ' + name;};
var exclaim = function(statement) { return statement.toUpperCase() + '!';};
var welcome = compose(greet, exclaim);
console.log(welcome('phillip')); //=> 'hi: PHILLIP!'
function compose () {
let arg = Array.prototype.slice.call(arguments);
return function (x) {
return arg.reduceRight ( function (acc, cur) {
return cur(acc);
}, x);
};
}
function pipe () {
var arg = Array.prototype.slice.call(arguments);
return function (x) {
return arg.reduce ( function (acc, cur) {
return cur(acc);
}, x);
};
}