//Enumerators
(10).times(function(i) {
console.log(i);
});
/*
0
1
2
3
4
5
6
7
8
9
*/
(10).map(function(e) { return e;});
/*
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
*/
//Secure Random Number Generators
(10).random();
/* generates random number between 0..10 */
//Metaprogramming send method
(30).send('days').send('from_now').send('to_human')
//nonrecursive fibonacci generation
(10).map().reduce(function(f, i) {
f[i] = (i<2)? 1 : f[i-1] + f[i-2];
return f;
}, [])
//→ [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
//Clock under logo
($_ = function(){
$('#clock').html((0).seconds().from_now().to_human())
})();
setInterval($_, (1).second());
//Random code theme changer
var change_theme = function(theme) {
$('#theme').attr('href', ['/css/', theme, '.css'].join(''));
}
setInterval(function() {
change_theme(themes[themes.length.random()]);
}, (10).seconds());
//Code used to update realtime output within comments in top example
(demo = function() {
//cascade example methods using send and reduce
var build_demo = function(n, methods) {
return {
method: ["(", n, ").",
methods.map(function(m) {
return m + "()";
}).join(".")
].join(''),
output: methods.reduce(function(n, method) {
return n.send(method)
}, n)
}
},
//keep comments vertically aligned and easy to read
//emulates printf "%42s" since the maximum cascaded method
//is 42 characters in length
pad = function(n, s) {
return (n - s.length).map(function() {
return ' '
}).join('');
},
//with the two helpers above,
//iterate through date methods and cascade them with the
//date calculations: from_now / ago
//finished with the
//readable date method to_human
recompute_timestamps = ["",
"from_now",
"ago"].map(function(time_method) {
return ["milliseconds",
"seconds",
"minutes",
"hours",
"days"].map(function(duration) {
return (time_method.length == 0)?
build_demo(30, [duration]) :
build_demo(30, [duration, time_method, 'to_human']);
});
}),
//build markup after flatting array generated above
code_demo = [].
concat.
apply([], recompute_timestamps).
map(function(example, i) {
return [example.method,
pad(42, example.method),
"//→ ",
example.output,
(((i+1)%5)==0)? "\n": ""].join("");
}).join("\n");
//apply code demo and reload syntax highlighting
$($('pre code')[0]).html(
["//Legible Date/Time calculations\n\n",
code_demo].join('')
);
hljs.highlightBlock($('pre code')[0]);
})();
setInterval(demo, (1).second()); //reload demo every second
_ _
_ __ _ _ _ __ ___ | |__ ___ _ __(_)___
| '_ \| | | | '_ ` _ \| '_ \ / _ \ '__| / __|
| | | | |_| | | | | | | |_) | __/ | | \__ \
|_| |_|\__,_|_| |_| |_|_.__/ \___|_| _/ |___/
|__/