comprehension.js 841 B

12345678910111213141516171819202122232425
  1. let iterator_comprehension = fun (
  2. 'function comprehension (v: Function, l: List, f: Function) -> Iterator',
  3. (v, l, f) => {
  4. foreach(l, (element, index) => {
  5. ensure(is(element, Types.Iterable), 'not_iterable', index+1)
  6. })
  7. l = l.map(element => iter(element))
  8. return (function* () {
  9. for (let values of zip(l, x => x)) {
  10. let ok = call(f, values)
  11. assert(is(ok, Types.Bool))
  12. if (ok) {
  13. yield call(v, values)
  14. }
  15. }
  16. })()
  17. }
  18. )
  19. let list_comprehension = fun (
  20. 'function list_comprehension (v: Function, l: List, f: Function) -> List',
  21. (v, l, f, scope) => list(iterator_comprehension[WrapperInfo].raw(scope))
  22. )