loop.js 965 B

1234567891011121314151617181920212223242526272829303132333435
  1. let for_loop_e = fun (
  2. 'function for_loop_enumerate (e: Enumerable) -> Iterator',
  3. e => {
  4. let list = enum_(e)
  5. let keys = list.get('keys')
  6. let values = list.get('values')
  7. return (function* () {
  8. assert(keys.length == values.length)
  9. let L = keys.length
  10. for (let i = 0; i < L; i += 1) {
  11. yield { key: keys[i], value: values[i] }
  12. }
  13. })()
  14. }
  15. )
  16. let for_loop_i = fun (
  17. 'function for_loop_iterate (i: Iterable) -> Iterator',
  18. i => map(iter(i), (e, i) => ({ key: i, value: e }))
  19. )
  20. let for_loop_v = overload([for_loop_e, for_loop_i], 'for_loop_v')
  21. let for_loop_a = fun (
  22. 'function for_loop_async (ai: AsyncIterable) -> AsyncIterator',
  23. ai => (async function* () {
  24. for await (let e of async_iter(ai)) {
  25. yield { key: Nil, value: e }
  26. }
  27. })()
  28. )