Created
August 11, 2014 01:18
-
-
Save J-Chaniotis/4d4f516de7bd0acd38a8 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
/* | |
Sweet.js version: 0.7.0 | |
node.js 0.10.30 | |
more info about tco function at: https://gist.github.com/Gozala/1697037 | |
*/ | |
var tco = function (f) { | |
var value, active = false, accumulated = []; | |
return function accumulator() { | |
accumulated.push(arguments); | |
if (!active) { | |
active = true; | |
while (accumulated.length) { | |
value = f.apply(this, accumulated.shift()); | |
} | |
active = false; | |
return value; | |
} | |
}; | |
}; | |
let tailFunction = macro { | |
/* | |
function inside object | |
var o = { | |
example: tailFunction () {...} | |
} | |
*/ | |
case infix {$name :|tailFunction ($args (,) ...) { $body ... }} => { | |
return #{$name: tco(function ($args (,) ...){ | |
$body ... | |
})} | |
} | |
/* | |
function expression | |
var example = tailFunction () {...} | |
*/ | |
case {_ ($args (,) ...) { $body ... }} => { | |
return #{tco(function ($args (,) ...){ | |
$body ... | |
})} | |
} | |
/* | |
function declaration and named function expression | |
tailFunction example () {...} | |
var example = tailFunction example () {...} | |
*/ | |
case {_ $name ($args (,) ...) { $body ... }} => { | |
return #{function $name(){ | |
if(! $name.__tco) { | |
$name.__tco = tco(function ($args (,) ...) { | |
$body... | |
}); | |
} | |
return $name.__tco.apply($name.__tco, arguments); | |
}} | |
} | |
} | |
/* | |
Recognized Syntax | |
insipred from: http://us6.campaign-archive2.com/?u=2cc20705b76fa66ab84a6634f&id=9bd21a4dce | |
*/ | |
// Function declaration | |
tailFunction isEvenNaiveS1 (num) { | |
if (num === 0) { | |
return true; | |
} | |
if (num === 1) { | |
return false; | |
} | |
return isEvenNaiveS1(Math.abs(num) - 2); | |
} | |
console.log(isEvenNaiveS1(99999)); | |
// Function expression | |
var isEvenNaiveS2 = tailFunction (num) { | |
if (num === 0) { | |
return true; | |
} | |
if (num === 1) { | |
return false; | |
} | |
return isEvenNaiveS2(Math.abs(num) - 2); | |
} | |
console.log(isEvenNaiveS2(99999)); | |
// Inside object | |
var o = { | |
isEvenNaiveS3: tailFunction (num) { | |
if (num === 0) { | |
return true; | |
} | |
if (num === 1) { | |
return false; | |
} | |
return o.isEvenNaiveS3(Math.abs(num) - 2); | |
} | |
}; | |
console.log(o.isEvenNaiveS3(99999)); | |
// Named function expression | |
var isEvenNaiveS4 = tailFunction isEvenNaiveS4(num) { | |
if (num === 0) { | |
return true; | |
} | |
if (num === 1) { | |
return false; | |
} | |
return isEvenNaiveS4(Math.abs(num) - 2); | |
} | |
console.log(isEvenNaiveS4(99999)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/trxcllnt/96d81d6afbbaa25cd841