test7.d 758 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. alias uint8 = ubyte;
  2. alias uint64 = ulong;
  3. import std.stdio;
  4. import core.thread : Fiber;
  5. uint64 fact(uint64 n){
  6. return fact(n, 1);
  7. }
  8. uint64 fact(uint64 n, uint64 a){
  9. if(n == 0){
  10. return a;
  11. }else{
  12. return fact(n - 1, n * a);
  13. }
  14. }
  15. void test7_spawner(){
  16. uint64[] inputs = [5, 7, 10, 3, 8];
  17. uint64[] results = new uint64[inputs.length];
  18. Fiber[] fibers;
  19. fibers.length = inputs.length;
  20. for(int i = 0; i < inputs.length; i++){
  21. int index = i;
  22. fibers[i] = new Fiber({
  23. results[index] = fact(inputs[index]);
  24. });
  25. fibers[i].call();
  26. writefln("fact(%s) = %s", inputs[i], results[i]);
  27. }
  28. }
  29. /*
  30. ./vtest2
  31. hello here!
  32. fact(5) = 120
  33. fact(7) = 5040
  34. fact(10) = 3628800
  35. fact(3) = 6
  36. fact(8) = 40320
  37. */