-
-
Save Pet3ris/988538 to your computer and use it in GitHub Desktop.
Y combinator in 140byt.es
This file contains hidden or 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( | |
// we take Haskell's fixed point combinator | |
// λf.(λx.f(xx))(λx.f(xx)) | |
// and add an additional argument (_ stands for arguments of function) | |
// λf.(λx_.f(xx)_)(λx_.f(xx)_) | |
// abstract further | |
// λf.(λg.gg)(λx_.f(xx)_) | |
// curry the last bit | |
// λf.(λg.gg)(λx.λ_.f(xx)_) | |
// and alpha-rename variables (note there is no collision with the c's) | |
// λa.(λc.cc)(λc.λ_.a(cc)_) | |
// or more explictly | |
// λa.(λc.cc)(λc.λ"arguments".a(cc)"arguments") | |
a, // λa. | |
// a function that calls its argument as if it was itself, e.g., the identity function(I){return function(n){return n==0?0:I(n-1)+1}} | |
b // undefined placeholder for pure functions or this. for the recursive function a | |
){ | |
return( // ( | |
function(c){ // λc. | |
return c(c) // cc | |
} | |
) // ) | |
( // ( | |
function(c){ // λc. | |
return function(){ // λ_. | |
return a(c(c)). // a(cc) | |
apply(b,arguments) // _ | |
} | |
} | |
) // ) | |
} |
This file contains hidden or 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(a,b){return(function(c){return c(c)})(function(c){return function(){return a(c(c)).apply(b,arguments)}})} |
This file contains hidden or 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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Peteris Erins http://peteriserins.com | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains hidden or 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
{ | |
"name": "ycombinator", | |
"description": "A Y combinator.", | |
"keywords": [ | |
"combinator", | |
"functional", | |
"lambda" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah, ok, now I get it. You might be able to save some bytes if you were using more imperative features. I found a version which is shorter than yours and has to be used differently: