The Squirrel interpreter. See http://www.squirrel-lang.org/
Diff: samples/generators.nut
- Revision:
- 0:97a4f8cc534c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/generators.nut Tue Dec 16 10:20:34 2014 +0000 @@ -0,0 +1,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"); +} \ No newline at end of file