Skip to content

Instantly share code, notes, and snippets.

Last active April 27, 2016 14:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
// a simple function to compose a function
// you can skip the reading of this function if you want
var compose = function(){
var fns =
return function composed(){
var thisCall = this
var args =
if ( !== '[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{return x.toUpperCase()})
// take a list of Strings and return a list of length
var length = function(list) {
return{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"])
Copy link

// brief explanation of what happens here
var sumCharsOnList = compose(sum, lenght, toUpperCase)
sumCharsOnList(["NX", "PS4", "XboxOne"])

// --> first the list ["NX", "PS4", "XboxOne"] is passed to the function toUpperCase
// --> toUpperCase returns another list and it's the input for the lenght function
// --> the lenght function return a list of integers which will become the input for sum function
// --> sum function will reduce the list summing all the integers and returning the sum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment