IIFE.k 897 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. promise {
  2. // Ordinary IIFE
  3. var x = 0
  4. let y = invoke {
  5. reset x = 1
  6. return 2^10
  7. }
  8. assert x == 1
  9. assert y == 1024
  10. // Iterator (Generator IIFE)
  11. let i = iterator {
  12. for n in [3,5,7] {
  13. yield n
  14. }
  15. }
  16. assert i is Iterator
  17. var sum = 0
  18. for n in i {
  19. reset sum += n
  20. }
  21. assert sum == 3+5+7
  22. // Promise (Async IIFE)
  23. var q = 'a'
  24. let p = promise {
  25. reset q = 'b'
  26. return 1
  27. }
  28. assert p is Promise
  29. await r = p
  30. assert q == 'b'
  31. assert r == 1
  32. // Async Iterator (Async Generator IIFE)
  33. let ai = async iterator {
  34. for n in [2,3,5] {
  35. await v = promise { return n }
  36. yield v
  37. }
  38. }
  39. assert ai is AsyncIterator
  40. var product = 1
  41. await element in ai {
  42. reset product *= element
  43. }
  44. assert product == 2*3*5
  45. }