Created
July 6, 2016 19:11
-
-
Save miguelfrias/a3081cfe0cfb2156367912d850ea966b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
function revocable(func1) { | |
return { | |
invoke: function(a) { | |
return func1.apply( | |
this, | |
arguments | |
); | |
}, | |
revoke: function() { | |
func1 = null; | |
} | |
}; | |
} | |
function counterf(a) { | |
return { | |
inc: function() { | |
return a + 1; | |
}, | |
dec: function() { | |
return a - 1; | |
} | |
}; | |
} | |
function once(func1) { | |
return function() { | |
var f = func1; | |
func = null; | |
return f.apply( | |
this, | |
arguments | |
); | |
}; | |
} | |
function composeb(func1 , func2) { | |
return function(a, b, c) { | |
return func2( | |
func1(a, b), | |
c | |
); | |
}; | |
} | |
function composeu(func1, func2) { | |
return function(y) { | |
return func2( | |
func1(y) | |
); | |
}; | |
} | |
function twice(binary) { | |
return function(y) { | |
return binary(y, y); | |
}; | |
} | |
function demethodize(func) { | |
return function(that, y) { | |
return func.call(that, y); | |
}; | |
} | |
function methodize(func) { | |
return function(y) { | |
return func(this, y); | |
}; | |
} | |
var inc = curry(add, 1); | |
function curry(func, x) { | |
// Solution 1 | |
// return function(y) { | |
// return func(x, y); | |
// }; | |
// Solution 2 | |
return applyf(func)(first); | |
} | |
function applyf(binary) { | |
return function (x) { | |
return function (y) { | |
return binary(x, y); | |
}; | |
}; | |
} | |
function addf(x) { | |
return function(y) { | |
return x + y; | |
}; | |
} | |
function identityf(x) { | |
return function() { | |
return x; | |
}; | |
} | |
function mul(a, b) { | |
return a * b; | |
} | |
function add(a, b) { | |
return a + b; | |
} | |
function identity(x) { | |
return x; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment