Skip to content

Instantly share code, notes, and snippets.

@TobiasWooldridge
Created December 10, 2013 22:45
Show Gist options
  • Save TobiasWooldridge/7901792 to your computer and use it in GitHub Desktop.
Save TobiasWooldridge/7901792 to your computer and use it in GitHub Desktop.
<!--
Use the following or similar to execute JS
-->
<html>
<body>
<pre>
<script>
function log(arg) {
document.writeln(arg);
}
function identity(x) {
return x;
}
var add = function(x, y) {
return x + y;
}
var mul = function(x, y) {
return x * y;
}
// write a function which returns a function which returns the original functions arg
var identityf = function(n) {
return function() {
return n;
}
}
// write a function that executes an operator on following invocations over three invocations
// applyf(add)(3)(4)
var applyf = function(operator) {
return function(x) {
return function(y) {
return operator(x,y);
}
}
}
// write a function that adds from two invocations
var addf = function(x) {
return applyf(add)(x);
}
// write a function that takes a function and an arg, and returns a function that can take a second arg
var curry = function(binary, fixedArg) {
return function(arg) {
return binary(fixedArg, arg);
}
}
log(identity(3));
</script>
</pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment