-
-
Save marciol/1178543 to your computer and use it in GitHub Desktop.
Mercury + Haml: generating fibonacci numbers
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
require 'luarocks.require' | |
require 'mercury' | |
require 'haml' | |
module('fibonacci', package.seeall, mercury.application) | |
local templates = { | |
index = [[ | |
%html | |
%head | |
%title Fibonacci numbers generator | |
%style(type="text/css") | |
:plain | |
#fibonateUpTo { width: 50px; border: 1px solid #000; } | |
:javascript | |
function fibonate() { | |
// OK OK I know, so do not swear on this fugly piece of code please ;-) | |
document.location = "/fibonacci/" + document.getElementById('fibonateUpTo').value; | |
} | |
%body | |
%p Welcome to the Fibonacci numbers generator! | |
%p You can try one of the following samples: | |
%ul | |
- for _, maxn in pairs(samples) do | |
- local link = "/fibonacci/" .. maxn | |
%li | |
%a(href=link)= "Generate numbers up to " .. maxn | |
%p | |
If you prefer, you can generate Fibonacci numbers up to | |
%input(id="fibonateUpTo" type="text"). | |
%a(href="javascript:fibonate();") Try it yourself! | |
]], | |
fibonacci = [[ | |
%html | |
%head | |
%title Fibonacci numbers generator | |
%body | |
%p | |
:plain | |
Generating Fibonacci numbers up to #{params.limit}! | |
%ul | |
- for number in fibonacci(tonumber(params.limit)) do | |
%li= number | |
]], | |
} | |
helpers(function() | |
function get_samples(how_many) | |
local samples = {} | |
for i = 1, how_many do | |
table.insert(samples, math.random(1, 1000000)) | |
end | |
return samples | |
end | |
function fibonacci(maxn) | |
return coroutine.wrap(function() | |
local x, y = 0, 1 | |
while x <= maxn do | |
coroutine.yield(x) | |
x, y = y, x + y | |
end | |
end) | |
end | |
end) | |
get('/', function() | |
t.haml(templates.index, nil, { samples = get_samples(4) }) | |
end) | |
get('/fibonacci/:limit', function() | |
t.haml(templates.fibonacci) | |
end) |
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
<html> | |
<head> | |
<title>Fibonacci numbers generator</title> | |
</head> | |
<body> | |
<p> | |
Generating Fibonacci numbers up to 372906! | |
</p> | |
<ul> | |
<li>0</li> | |
<li>1</li> | |
<li>1</li> | |
<li>2</li> | |
<li>3</li> | |
<li>5</li> | |
<li>8</li> | |
<li>13</li> | |
<li>21</li> | |
<li>34</li> | |
<li>55</li> | |
<li>89</li> | |
<li>144</li> | |
<li>233</li> | |
<li>377</li> | |
<li>610</li> | |
<li>987</li> | |
<li>1597</li> | |
<li>2584</li> | |
<li>4181</li> | |
<li>6765</li> | |
<li>10946</li> | |
<li>17711</li> | |
<li>28657</li> | |
<li>46368</li> | |
<li>75025</li> | |
<li>121393</li> | |
<li>196418</li> | |
<li>317811</li> | |
</ul> | |
</body> | |
</html> |
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
<html> | |
<head> | |
<title>Fibonacci numbers generator</title> | |
<style type='text/css'> | |
#fibonateUpTo { width: 50px; border: 1px solid #000; } | |
</style> | |
<script type='text/javascript'> | |
//<![CDATA[ | |
function fibonate() { | |
// OK OK I know, so do not swear on this fugly piece of code please ;-) | |
document.location = "/fibonacci/" + document.getElementById('fibonateUpTo').value; | |
} | |
//]]> | |
</script> | |
</head> | |
<body> | |
<p>Welcome to the Fibonacci numbers generator!</p> | |
<p>You can try one of the following samples:</p> | |
<ul> | |
<li> | |
<a href='/fibonacci/372906'>Generate numbers up to 372906</a> | |
</li> | |
<li> | |
<a href='/fibonacci/261117'>Generate numbers up to 261117</a> | |
</li> | |
<li> | |
<a href='/fibonacci/580035'>Generate numbers up to 580035</a> | |
</li> | |
<li> | |
<a href='/fibonacci/961273'>Generate numbers up to 961273</a> | |
</li> | |
</ul> | |
<p> | |
If you prefer, you can generate Fibonacci numbers up to | |
<input id='fibonateUpTo' type='text'>.</input> | |
<a href='javascript:fibonate();'>Try it yourself!</a> | |
</p> | |
</body> | |
</html> |
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
http://127.0.0.1:7654/ | |
http://127.0.0.1:7654/fibonacci/372906 |
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
require 'luarocks.require' | |
require 'xavante' | |
require 'wsapi.xavante' | |
xavante.HTTP{ | |
server = { host = "127.0.0.1", port = 7654 }, | |
defaultHost = { | |
rules = { | |
{ | |
match = { "^/(.-)$" }, | |
with = wsapi.xavante.makeHandler('fibonacci') | |
} | |
} | |
} | |
} | |
xavante.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment