Skip to content

Instantly share code, notes, and snippets.

@alanrsoares
Created April 22, 2015 21:34
Show Gist options
  • Save alanrsoares/cc5f0bf42dbc699c898e to your computer and use it in GitHub Desktop.
Save alanrsoares/cc5f0bf42dbc699c898e to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/dohopo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var ofType, fib, assertEqual;
ofType = curry$(function(type, x){
return typeof x === type;
});
fib = function(n, cache){
if (!cache) {
cache = [0, 1];
}
if (cache[n] !== undefined) {
return cache[n];
}
return cache[n] = (function(){
var ref$;
switch (ref$ = [n], false) {
case !isNaN(ref$[0]):
return 0;
case !ofType('string')(ref$[0]):
return 0;
case !(function(it){
return it < 0;
})(ref$[0]):
return null;
default:
return fib(n - 1, cache) + fib(n - 2, cache);
}
}());
};
assertEqual = function(x, y){
return console.log(
x === y);
};
assertEqual(fib(-1), null);
assertEqual(fib(0), 0);
assertEqual(fib(1), 1);
assertEqual(fib('a'), 0);
assertEqual(fib('10'), 0);
assertEqual(fib(10), 55);
assertEqual(fib(100), 354224848179262000000);
assertEqual(fib(1000), 4.346655768693743e+208);
function curry$(f, bound){
var context,
_curry = function(args) {
return f.length > 1 ? function(){
var params = args ? args.concat() : [];
context = bound ? context || this : this;
return params.push.apply(params, arguments) <
f.length && arguments.length ?
_curry.call(context, params) : f.apply(context, params);
} : f;
};
return _curry();
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">of-type = (type, x) --> typeof x is type
fib = (n, cache) ->
cache = [0, 1] if not cache
return cache[n] if cache[n] is not undefined
cache[n] =
match n
| isNaN => 0
| of-type \string => 0
| (< 0) => null
| otherwise => (fib n - 1, cache) +
(fib n - 2, cache)
assert-equal = (x, y) -> (x is y) |> console.log
assert-equal (fib -1) , null
assert-equal (fib 0) , 0
assert-equal (fib 1) , 1
assert-equal (fib 'a') , 0
assert-equal (fib '10'), 0
assert-equal (fib 10) , 55
assert-equal (fib 100) , 354224848179262000000
assert-equal (fib 1000), 4.346655768693743e+208
</script></body>
</html>
var ofType, fib, assertEqual;
ofType = curry$(function(type, x){
return typeof x === type;
});
fib = function(n, cache){
if (!cache) {
cache = [0, 1];
}
if (cache[n] !== undefined) {
return cache[n];
}
return cache[n] = (function(){
var ref$;
switch (ref$ = [n], false) {
case !isNaN(ref$[0]):
return 0;
case !ofType('string')(ref$[0]):
return 0;
case !(function(it){
return it < 0;
})(ref$[0]):
return null;
default:
return fib(n - 1, cache) + fib(n - 2, cache);
}
}());
};
assertEqual = function(x, y){
return console.log(
x === y);
};
assertEqual(fib(-1), null);
assertEqual(fib(0), 0);
assertEqual(fib(1), 1);
assertEqual(fib('a'), 0);
assertEqual(fib('10'), 0);
assertEqual(fib(10), 55);
assertEqual(fib(100), 354224848179262000000);
assertEqual(fib(1000), 4.346655768693743e+208);
function curry$(f, bound){
var context,
_curry = function(args) {
return f.length > 1 ? function(){
var params = args ? args.concat() : [];
context = bound ? context || this : this;
return params.push.apply(params, arguments) <
f.length && arguments.length ?
_curry.call(context, params) : f.apply(context, params);
} : f;
};
return _curry();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment