Skip to content

Instantly share code, notes, and snippets.

View daronwolff's full-sized avatar
🏠
Working from home

daronwolff daronwolff

🏠
Working from home
  • Me
  • México
View GitHub Profile
@daronwolff
daronwolff / promises_example.js
Last active May 23, 2016 20:53
Promises Example
(function() {
var restante = 0;
'use strict';
function wait() {
return new Promise(function(done, reject) {
setTimeout(function() {
if (restante > 0) {
done();
} else {
@daronwolff
daronwolff / exercise_10.js
Last active May 23, 2016 17:03
Make a revocable function that takes a nice function and returns a revoke function that denies the access to the nice function, and an invoke function it is invokes the nice function until be revoked
var revoke = (function(func) {
var tmp = func;
return {
invoke: function(p) {
tmp(p);
},
revoke: function() {
tmp = null;
}
}
@daronwolff
daronwolff / exercise_9.js
Last active May 23, 2016 16:51
Write a function that returns two functions to up/down counter
var counterf = (function(n){
var inc = function() {
return n += 1;
};
var dec = function() {
return n -= 1;
};
return {
dec: dec,
inc: inc
@daronwolff
daronwolff / exercise_9.js
Last active May 23, 2016 16:49
Write a function that returns two functions that implement up/down counter
function counterf(n) {
var n = n;
var inc = function() {
return n += 1;
};
var dec = function() {
return n -= 1;
};
return {
dec: dec,
function multiMax(multi){
// Make an array of all but the first argument
var allButFirst = Array().slice.call(arguments);
// Find the largest number in that array of arguments
var largestAllButFirst = Math.max.apply(Math,allButFirst);
// Return the multiplied result
return multi * largestAllButFirst;
}
@daronwolff
daronwolff / exercise_8.js
Created May 20, 2016 22:31
Write a TWICE, function that takes a binary function and returns a unitary function that passes its arguments to the binary function twice Example var double = twice(add); double(11); // 22 var square = twice(mul); square(11); // 121
var add = (function(a) {
return function(b) {
return a + b;
}
});
var mul = (function(a) {
return function(b) {
var total = a * b;
return total;
}
@daronwolff
daronwolff / exercise_7.js
Last active May 20, 2016 22:15
Write a DEmethodize, a function that converts a method to a binary function Example demethodize(Number.prototype.add)(5)(6) // 11
function demethodize(func){
return function(that,y){
return func.call(that,y);
}
}
var add = (function(a) {
return function(b) {
return a + b;
}
});
@daronwolff
daronwolff / exercise_6.js
Created May 20, 2016 22:03
Write a methodize, a function that converts a binary function to a method
function add(a,b) {
return (a + b);
}
function methodize(func) {
return function(y){
return func(this,y);
}
}
Number.prototype.add = methodize(add);
(2).add(3) // 5
@daronwolff
daronwolff / exercise_5.js
Created May 20, 2016 21:07
Write a function that takes a function and an argument, and returns a function that can supply a second argument add3 = curry(add,3); add3(4); // 7
var add = (function(a) {
return function(b) {
return a + b;
}
});
var mul = (function(a) {
return function(b) {
var total = a * b;
var add = (function(a) {
return function(b) {
return a + b;
}
});
var mul = (function(a) {
return function(b) {
var total = a * b;