peterschussheim
9/22/2016 - 4:15 PM

Good Parts of JS w Doug Crockford

Good Parts of JS w Doug Crockford

document.wr


function log(arg) {
  document.writeln(arg);
}

function identity(x) {
  return x;
}

// log(identity(3))

var firstNum = 3;
var secondNum = 4;

// add :: (Num, Num) -> Num
var add = function add(first, second) {
  return first + second;
}

// log(add(3, 4));

// sub :: (Num, Num) -> Num
var sub = function sub(first, second) {
  return first - second;
}

// log(sub(firstNum, secondNum))

// mul :: (Num, Num) -> Num
var mul = function mul(first, second) {
  return first * second;
}

// log(mul(firstNum, secondNum))

// Write a function identityf that takes an argument
// and returns a function that returns that argument.
// var three = identityf(3);
// three() ----> 3
function identityf(x) {
  return function () {
    return x;
  };
}

var three = identityf(3);
// log(three())


// Write a function addf that adds from two invocations.
function addf(first) {
  return function(second) {
    return first + second
  }
}

// log(addf(3)(4))

// Write a function liftf that takes a binary function
// and makes it callable with two invocations.
// var addf = liftf(add);
// addf(3)(4)
// liftf(mul)(5)(6)

function liftf(binary) {
  return function (first) {
    return function (second) {
      return binary(first, second);
    }
  }
}

// var addf = liftf(add);
// // log(addf(3)(4))
// log(liftf(mul)(5)(6))

function curry(binary, first) {
  return function (second) {
    return binary(first, second);
  };
}

// Using liftf
/*
function curry(binary, first) {
  return liftf(binary)(first);
}
*/

// refactor using destructuring
function curry(func) {
  var slice = Array.prototype.slice;
  var args = slice.call(arguments, 1); 
  return function () {
    return func.apply(
      null,
      args.concat(slice.call(arguments, 0))
    );
  };
}

// es6
// function curry(func, ...first) {
//   return function (...second) {
//     return function(...first, ...second);
//   };
// }

function twice(binary) {
  return function(a) {
    return binary(a, a);
  };
}

function reverse(binary) {
  return function(first, second) {
    return binary(second, first);
  }
}

// es6 syntax
function reverse(func) {
  return function(...args) {
    return func(...args.reverse());
  };
}


// Write a function composeu that takes two unary functions
// and returns a unary function that calls them both.
// composeu(doubl, square)(5) // -> 100

// Takes functions f and g and returns a function that takes function an argument a and returns the result of calling g(f(a))
function composeu(f, g) {
  return function(a) {
    return g(f(a));
  }
}

function composeb(f, g) {
  return function(a, b, c) {
    g(f(a, b), c);
  };
}

// call a binary function N times (count)
function limit(binary, count) {
  return function (a, b) {
    if (count >= 1) {
      count -= 1;
      return binary(a,b);
    }
    return undefined;
  };
}





// left off at the beginning of "Function Challenge 4"
// left off at the beginning of "Function Challenge 4"