for more information visit the package's GitHub page
Package contains the following modules:
This library provides a way to simulate lazy lists, or streams, in the form of generators.
The interface for generators is similar to a subset of Haskell's Data.List.Stream
.
The below example shows a classic construction of the Fibonacci (infinite) sequence:
> import Generator as G
> fib = G.iterate (\(a, b) -> (b, b + a)) (0, 1)
> G.take 10 fib
[(0,1),(1,1),(1,2),(2,3),(3,5),(5,8),(8,13),(13,21),(21,34),(34,55)]
> G.map Tuple.first fib |> G.take 10
[0,1,1,2,3,5,8,13,21,34]
Since Lazy
has been eliminated in Elm 0.19, the goal is to provide a convenient way to model lazy lists, or streams.
Generators can be transformed and combined (e.g. map
, zip
, etc) in constant time, with computation performed only when a generator is explicitly advanced.
Unlike streams, generators can be either finite or infinite. A finite generator that has reached its logical end is "empty" and simply emits no further values. (The library supplies an empty
predicate function, but it should rarely be needed as library functions are agnostic to finite or infinite generators.)
Note that the Elm compiler is still strict. So, even using the generator interface, it's not possible to define infinitely recursive functions that work in languages like Haskell.
-- this still doesn't compile!
ruler =
G.interleave (G.repeat 0) (G.map ((+) 1) ruler)
As an example of an iterative formulation of a function that's often defined recursively, see the Sieve of Eratosthenes in Examples/Primes.elm
.
In addition to inline tests in the module documentation, the repo contains several examples
for how to use generators.
For developers: to follow the "full" build for this repo, run the commands in the all.do
script (redo all
if you have redo
, or run it as a shell script like sh all.do
).
See this page for an explanation of the build steps and dependencies.