ffi bindings for JavaScript's iterators, iterables and generators. It also contains functions inspired by Python itertools and Scala iterators.
Documentation is published on Pursuit
spago install js-iterators
Consider the following javascript function
const counter = function*(n) {
let m = n;
while(true) {
yield m;
m += 1;
}
}
The type of the function in Purescript will be
foreign import counter :: Int -> Iterable Int
You can manipulate iterables via functions in JS.Iterables
For example,
import JS.Iterale as I
a :: Array Int
a = I.toArray $ I.take 5 $ counter 10
-- a = [10, 11, 12, 13, 14]
You can also manipulate iterators via the ST
monad
import Control.Monad.ST as ST
import JS.Iterale as I
import JS.Iterator.ST as STI
a :: Int
a = ST.run (do
it <- STI.iterator $ counter 10
_ <- STI.next it
_ <- STI.next it
STI.next it
)
-- a = 12