Skip to content

Instantly share code, notes, and snippets.

@bookercodes
Last active August 29, 2015 14:22
Show Gist options
  • Save bookercodes/0fa804b8731fdd5ec34f to your computer and use it in GitHub Desktop.
Save bookercodes/0fa804b8731fdd5ec34f to your computer and use it in GitHub Desktop.
These are my solutions to the "problems" chapter in Douglas Crockford's workshop entitled "Javascript the Good Parts" (http://www.pluralsight.com/courses/javascript-good-parts).
function identity(num) {
return num;
}
function add(num1, num2) {
return num1 + num2;
}
function mul(num1, num2) {
return num1 * num2;
}
function identityf(num) {
return function() {
return num;
};
}
function addf(num1) {
return function(num2) {
return num1 + num2;
};
}
function applyf(fun) {
return function(num1) {
return function(num2) {
return fun(num1, num2);
};
};
}
function curry(fun, num1) {
return function(num2) {
return fun(num1, num2);
};
}
function methodize(func) {
return function(num2) {
return func(this, num2);
};
}
Number.prototype.add = methodize(add);
function demethodize(func) {
return function(num1, num2) {
return func.call(num1, num2);
};
}
function twice(func) {
return function(num) {
return func(num, num);
};
}
var double = twice(add);
var square = twice(mul);
function composeu(func1, func2) {
return function(num) {
return func2(func1(num));
};
}
function composeb(func1, func2) {
return function(num1, num2, num3) {
return func2(func1(num1, num2), num3);
};
}
function once(func) {
return function() {
var f = func;
func = null;
return f.apply(this, arguments);
};
}
function counterf(num) {
return {
inc: function() {
num = num += 1;
return num;
},
dec: function() {
num = num -= 1;
return num;
}
};
}
function revocable(func) {
return {
invoke: function() {
return func.apply(this, arguments);
},
revoke: function() {
func = null;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment