nrk (owner)

Revisions

gist: 184844 Download_button fork
public
Description:
Mercury + Haml: generating fibonacci numbers
Public Clone URL: git://gist.github.com/184844.git
Embed All Files: show embed
fibonacci.lua #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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)
fibonacci_372906.html #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<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>
fibonacci_root.html #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<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>
sample_requests.txt #
1
2
http://127.0.0.1:7654/
http://127.0.0.1:7654/fibonacci/372906
xavante-runner.lua #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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()