danprince / zaphod

🌌 Clojure's API for JavaScript's Objects
https://zaphod.surge.sh
MIT License
54 stars 1 forks source link

lazy interface #6

Open danprince opened 8 years ago

danprince commented 8 years ago

It'd be great to have a zaphod/lazy interface that exposed functions that worked lazily with iterables.

import { range, map, take } from 'zaphod/lazy';

range()::map(n => n * 2)::take(3)
// => [2, 4, 6]

In this example range() returns an iterator which yields an infinite range of numbers. Map returns another iterator which requests values from range, applies f to them, then yields them to take.

danprince commented 8 years ago

This should be possible with a symbol polyfill, allowing each function to return an iterator which would be respected by for..of, ..., etc.

danprince commented 8 years ago

Functions that generate iterators in non-ES6 environments can use "@@iterator" as a Symbol.iterator polyfill. All functions will check alternative strategies before dropping back to using iterators. This should allow lazy/iterators to be added to main namespace.