generators.nut 721 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. *Random number function from The Great Computer Language shootout
  3. *converted to a generator func
  4. */
  5. function gen_random(max) {
  6. local last=42
  7. local IM = 139968;
  8. local IA = 3877;
  9. local IC = 29573;
  10. for(;;){ //loops forever
  11. yield (max * (last = (last * IA + IC) % IM) / IM);
  12. }
  13. }
  14. local randtor=gen_random(100);
  15. print("RAND NUMBERS \n")
  16. for(local i=0;i<10;i+=1)
  17. print(">"+resume randtor+"\n");
  18. print("FIBONACCI \n")
  19. function fiboz(n)
  20. {
  21. local prev=0;
  22. local curr=1;
  23. yield 1;
  24. for(local i=0;i<n-1;i+=1)
  25. {
  26. local res=prev+curr;
  27. prev=curr;
  28. yield curr=res;
  29. }
  30. return prev+curr;
  31. }
  32. foreach(val in fiboz(10))
  33. {
  34. ::print(">"+val+"\n");
  35. }