File: generators.nut

package info (click to toggle)
squirrel3 3.1-8.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,380 kB
  • sloc: cpp: 12,722; ansic: 917; makefile: 316; python: 40
file content (42 lines) | stat: -rw-r--r-- 721 bytes parent folder | download | duplicates (12)
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
/*
*Random number function from The Great Computer Language shootout
*converted to a generator func
*/

function gen_random(max) {
    local last=42
    local IM = 139968;
    local IA = 3877;
    local IC = 29573;
    for(;;){  //loops forever
        yield (max * (last = (last * IA + IC) % IM) / IM);
    }
}

local randtor=gen_random(100);

print("RAND NUMBERS \n")

for(local i=0;i<10;i+=1)
    print(">"+resume randtor+"\n");

print("FIBONACCI \n")
function fiboz(n)
{
    local prev=0;
    local curr=1;
    yield 1;

    for(local i=0;i<n-1;i+=1)
    {
        local res=prev+curr;
        prev=curr;
        yield curr=res;
    }
    return prev+curr;
}

foreach(val in fiboz(10))
{
    ::print(">"+val+"\n");
}