Last active
December 28, 2015 04:49
-
-
Save pasberth/7445035 to your computer and use it in GitHub Desktop.
Lazy K in CoffeeScript
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
lazy = (x) -> () -> x | |
thaw = (x) -> x() | |
app = () -> [].slice.call(arguments, 0).reduce (x, y) -> () -> thaw(x)(y) | |
S = (x) -> (y) -> (z) -> thaw app(x, z, app(y, z)) | |
K = (x) -> (y) -> thaw x | |
I = (x) -> thaw x | |
parseLazyK = (source) -> | |
stack = [[]] | |
while source | |
c = source[0] | |
source = source[1..-1] | |
switch c | |
when 's' | |
stack[0].push S | |
when 'k' | |
stack[0].push K | |
when 'i' | |
stack[0].push I | |
when '`' | |
stack.unshift [] | |
while stack.length > 1 && stack[0].length > 1 | |
x = stack.shift() | |
stack[0].push x | |
stack[0] | |
execLazyK = (node) -> | |
tmp = node.map (x) -> | |
if x instanceof Array | |
execLazyK x | |
else | |
lazy x | |
tmp.reduce (x, y) -> app x, y | |
evalLazyK = (source) -> execLazyK parseLazyK source | |
LazyK = (source, input) -> | |
program = evalLazyK source | |
output = app program, strToScottList(input) | |
js_output = scottListToStr output | |
console.log thaw js_output | |
churchNatToInt = (x) -> app x, lazy((x) -> thaw(x) + 1), lazy(0) | |
churchNatToStr = (x) -> lazy String.fromCharCode thaw churchNatToInt x | |
scottListToStr = (xs) -> app xs, lazy((h) -> (t) -> if (thaw churchNatToInt h) > 255 then "" else (thaw churchNatToStr h) + (thaw scottListToStr t)) | |
intToChurchNat = (i) -> lazy((f) -> (x) -> | |
j = i | |
y = x | |
while j != 0 | |
y = app f, y | |
j = j - 1 | |
thaw y) | |
strToChurchNat = (s) -> intToChurchNat s.charCodeAt 0 | |
strToScottList = (s) -> | |
if s | |
lazy((f) -> thaw app(f, strToChurchNat(s[0]), strToScottList(s[1..-1]))) | |
else | |
lazy((f) -> thaw app(f, intToChurchNat(256), strToScottList(""))) | |
@LazyK = LazyK | |
@LazyK.parse = parseLazyK | |
@LazyK.eval = evalLazyK | |
@LazyK.helloworld = "`k``s``si`k``s`k```sii``s``s`kski``s``s`ksk``s``s`ksk```s``siii``s``s`kski`k``s``si`k``s``s`ksk```s``s`kski``s`k``s``s`kski``s``s`ksk```sii``s``s`kski`k``s``si`k``s`k```sii``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k``s`k```sii``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k``s``s`ksk``s`k``s``s`kski``s``s`ksk``s`k``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k````s``s`ksk```s``siii``s``s`kski`s``s`ksk```sii``s``s`ksk``s``s`kski`k``s``si`k``s`k``s``s`kski```s``siii``s``s`kski`k``s``si`k``s`k``s``s`ksk``s`k``s``s`kski``s``s`ksk``s``s`kski``s``s`ksk```s``siii``s``s`kski`k``s``si`k``s``s`ksk``s`k``s``s`kski``s``s`ksk``s`k``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k``s`k``s``s`kski``s``s`ksk``s`k``s``s`kski``s``s`ksk```sii``s``s`ksk``s``s`kski`k``s``si`k``s`k```sii``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k```s``s`kski``s`k``s``s`kski``s``s`ksk```sii``s``s`kski`k``s``si`k``s``s`ksk``s`k``s``s`kski```s``siii``s``s`kski`k``s``si`k``s`k``s``s`kski``s``s`ksk```sii``s``s`kski`k``s``si`k```sii```sii``s``s`kski`k```sii```sii``s``s`kski" |
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
// Generated by CoffeeScript 1.3.3 | |
(function() { | |
var I, K, LazyK, S, app, churchNatToInt, churchNatToStr, evalLazyK, execLazyK, intToChurchNat, lazy, parseLazyK, scottListToStr, strToChurchNat, strToScottList, thaw; | |
lazy = function(x) { | |
return function() { | |
return x; | |
}; | |
}; | |
thaw = function(x) { | |
return x(); | |
}; | |
app = function() { | |
return [].slice.call(arguments, 0).reduce(function(x, y) { | |
return function() { | |
return thaw(x)(y); | |
}; | |
}); | |
}; | |
S = function(x) { | |
return function(y) { | |
return function(z) { | |
return thaw(app(x, z, app(y, z))); | |
}; | |
}; | |
}; | |
K = function(x) { | |
return function(y) { | |
return thaw(x); | |
}; | |
}; | |
I = function(x) { | |
return thaw(x); | |
}; | |
parseLazyK = function(source) { | |
var c, stack, x; | |
stack = [[]]; | |
while (source) { | |
c = source[0]; | |
source = source.slice(1); | |
switch (c) { | |
case 's': | |
stack[0].push(S); | |
break; | |
case 'k': | |
stack[0].push(K); | |
break; | |
case 'i': | |
stack[0].push(I); | |
break; | |
case '`': | |
stack.unshift([]); | |
} | |
while (stack.length > 1 && stack[0].length > 1) { | |
x = stack.shift(); | |
stack[0].push(x); | |
} | |
} | |
return stack[0]; | |
}; | |
execLazyK = function(node) { | |
var tmp; | |
tmp = node.map(function(x) { | |
if (x instanceof Array) { | |
return execLazyK(x); | |
} else { | |
return lazy(x); | |
} | |
}); | |
return tmp.reduce(function(x, y) { | |
return app(x, y); | |
}); | |
}; | |
evalLazyK = function(source) { | |
return execLazyK(parseLazyK(source)); | |
}; | |
LazyK = function(source, input) { | |
var js_output, output, program; | |
program = evalLazyK(source); | |
output = app(program, strToScottList(input)); | |
js_output = scottListToStr(output); | |
return console.log(thaw(js_output)); | |
}; | |
churchNatToInt = function(x) { | |
return app(x, lazy(function(x) { | |
return thaw(x) + 1; | |
}), lazy(0)); | |
}; | |
churchNatToStr = function(x) { | |
return lazy(String.fromCharCode(thaw(churchNatToInt(x)))); | |
}; | |
scottListToStr = function(xs) { | |
return app(xs, lazy(function(h) { | |
return function(t) { | |
if ((thaw(churchNatToInt(h))) > 255) { | |
return ""; | |
} else { | |
return (thaw(churchNatToStr(h))) + (thaw(scottListToStr(t))); | |
} | |
}; | |
})); | |
}; | |
intToChurchNat = function(i) { | |
return lazy(function(f) { | |
return function(x) { | |
var j, y; | |
j = i; | |
y = x; | |
while (j !== 0) { | |
y = app(f, y); | |
j = j - 1; | |
} | |
return thaw(y); | |
}; | |
}); | |
}; | |
strToChurchNat = function(s) { | |
return intToChurchNat(s.charCodeAt(0)); | |
}; | |
strToScottList = function(s) { | |
if (s) { | |
return lazy(function(f) { | |
return thaw(app(f, strToChurchNat(s[0]), strToScottList(s.slice(1)))); | |
}); | |
} else { | |
return lazy(function(f) { | |
return thaw(app(f, intToChurchNat(256), strToScottList(""))); | |
}); | |
} | |
}; | |
this.LazyK = LazyK; | |
this.LazyK.parse = parseLazyK; | |
this.LazyK["eval"] = evalLazyK; | |
this.LazyK.helloworld = "`k``s``si`k``s`k```sii``s``s`kski``s``s`ksk``s``s`ksk```s``siii``s``s`kski`k``s``si`k``s``s`ksk```s``s`kski``s`k``s``s`kski``s``s`ksk```sii``s``s`kski`k``s``si`k``s`k```sii``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k``s`k```sii``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k``s``s`ksk``s`k``s``s`kski``s``s`ksk``s`k``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k````s``s`ksk```s``siii``s``s`kski`s``s`ksk```sii``s``s`ksk``s``s`kski`k``s``si`k``s`k``s``s`kski```s``siii``s``s`kski`k``s``si`k``s`k``s``s`ksk``s`k``s``s`kski``s``s`ksk``s``s`kski``s``s`ksk```s``siii``s``s`kski`k``s``si`k``s``s`ksk``s`k``s``s`kski``s``s`ksk``s`k``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k``s`k``s``s`kski``s``s`ksk``s`k``s``s`kski``s``s`ksk```sii``s``s`ksk``s``s`kski`k``s``si`k``s`k```sii``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k```s``s`kski``s`k``s``s`kski``s``s`ksk```sii``s``s`kski`k``s``si`k``s``s`ksk``s`k``s``s`kski```s``siii``s``s`kski`k``s``si`k``s`k``s``s`kski``s``s`ksk```sii``s``s`kski`k``s``si`k```sii```sii``s``s`kski`k```sii```sii``s``s`kski"; | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment