Package

purescript-js-iterators

Repository
gbagan/purescript-js-iterators
License
MIT
Uploaded by
pacchettibotti
Published on
2023-05-09T14:36:28Z

ffi bindings for JavaScript's iterators, iterables and generators. It also contains functions inspired by Python itertools and Scala iterators.

Documentation

Documentation is published on Pursuit

Install

spago install js-iterators

Example

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
Modules
JS.Iterable
JS.Iterator.ST
Dependencies