This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a simple function to compose a function | |
// DO NOT USE THIS IN PRODUCTION | |
// you can skip the reading of this function if you want | |
var compose = function(){ | |
var fns = Array.prototype.slice.call(arguments) | |
return function composed(){ | |
var thisCall = this | |
var args = Array.prototype.slice.call(arguments) | |
fns.reverse().forEach(function(fn){ | |
if (Object.prototype.toString.call(args) !== '[object Array]') args = [args] | |
args = fn.apply(thisCall, args) | |
}) | |
return args | |
} | |
} | |
// take a list of Strings and return a list of up case strings | |
var toUpperCase = function(list) { | |
return list.map(function(x){return x.toUpperCase()}) | |
} | |
// take a list of Strings and return a list of length | |
var length = function(list) { | |
return list.map(function(x){return x.length}) | |
} | |
// take a list of Integer and return the sum | |
var sum = function(list) { | |
var sum = 0 | |
list.forEach(function(x){sum += x}) | |
return sum | |
} | |
// chain all these functions and produce a new | |
var sumCharsOnList = compose(sum, length, toUpperCase) | |
sumCharsOnList(["NX", "PS4", "XboxOne"]) |
Author
leandromoreira
commented
Apr 27, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment