JSFiddle - React, Tailwind, and code Playground

by John Schulz

JavaScript

var double = function (x) { return x * 2 }
console.log('double', 5, double(5))

var square = function (x) { return x * x }
console.log('square', 5, square(5))

var triple = function (x) { return x * 3 }

var compose = function(){
    var functions = Array.prototype.slice.call(arguments);
      var nextFunc = function(initial){
         var finalResult = functions.reduceRight(function(val, fn){
            return fn(val);
         }, initial);
				 return finalResult;
      }    
    return  nextFunc;
}

// ES6 #1 (ooo)
function compose1(...fns) {
  return function(x) {
    fns.reduceRight((val, fn) => fn(val), x)
  }
}

// ES6 #2 (one-liner madness)
const compose2 = (...fns) => (x) => fns.reduceRight((val, fn) => fn(val), x)

// given these functions, implement `compose` such that:
var squareThenDouble = compose(double, square)
// squareThenDouble(5); // 50
console.log('tim: squareThenDouble(5)', squareThenDouble(5))
var doubleThenSquare = compose(square, double)
// doubleThenSquare(5); // 100
console.log('doubleThenSquare(5)', doubleThenSquare(5))
console.log(compose(triple, double, square)(5)); // 150

var nums = [12,34,56,78,90];
var add = function (x, y) { return x + y }
console.log('add', add(12, 34)) // 46

// sum `nums` using `add` & reduce
// var sum = 



var total = nums.reduce(add);
console.log(total)