Module lazy

Data Types

generator()

generator(V) = fun(() -> empty | {V, generator(V)})

generator()

generator() = generator(term())

Function Index

all/2Checks if all values produced by a generator satisfy a predicate.
any/2Checks if any of the values produced by a generator satisfies a predicate.
append/1Creates a generator that is the concatenation of all of the given generators.
append/2Creates a generator that is the concatenation of the two given generators.
apply/2Creates a generator that applies the given function to the values produced by the given generator.
cycle/1Creates a generator that cycles the given generator.
drop/2Creates a generator that removes the given number of values from the given generator.
dropwhile/2Creates a generator that removes values from the given generator as long as the predicate holds.
empty/0Creates a generator that produces an empty sequence.
filter/2Creates a generator that produces the values taken from the given generator which satisfy the given predicate.
filtermap/2Creates a generator that combines filter and map into one.
flush/1Flushes a generator.
foldl/3Folds over the sequence the given generator produces from the left.
foldr/3Folds over the sequence the given generator produces from the right.
from_list/1Turns a list into a generator.
iterate/2Creates a generator that produces values by iterative calls of the given function, feeding its own output back in with the next call.
length/1Returns the number of values a generator produces.
map/2Creates a generator that produces values taken from the given generator and mapped by the given function.
next/1Materializes and returns the next value of a generator.
once/1Creates a generator that produces the given value exactly once.
repeat/1Creates a generator that produces repetitions of the given value.
repeatedly/1Creates a generator that produces values by repeated calls of the given function.
reverse/1Creates a generator that produces the values from the given generator in reverse order.
scan/3Creates a generator that works similar to foldl but produces the intermediate accumulator value with each step.
seq/2Creates a generator that produces a sequence of integers, starting with the given start value up to the given end value or infinity, in increments of 1.
seq/3Creates a generator that produces a sequence of integers, starting with the given start value up to the given end value or infinity, in increments of the given step value.
take/2Creates a generator that produces the given number of values taken from the given generator.
takewhile/2Creates a generator that produces the values taken from the given generator as long as the predicate holds.
to_list/1Materializes a generator into a list.
unfold/2Creates a generator similar to iterate/2.
unzip/1Creates two generators from a generator which produces 2-tuples, one for the first value in each tuple, one for the second.
zip/2Creates a generator that produces the values taken from the given generators wrapped together in a tuple.
zipwith/2Creates a generator that combines the values produced by all of the given generators by passing them to the given function.
zipwith/3Creates a generator that combines the values produced by the two given generators by passing them to the given functions.

Function Details

all/2

all(Fun::fun((V) -> boolean()), Generator::generator(V)) -> boolean()

Checks if all values produced by a generator satisfy a predicate.

  1> Gen = lazy:seq(1, 3).
  #Fun<lazy.43.73700886>
 
  2> lazy:all(fun (V) -> V > 0 end, Gen).
  true
 
  3> lazy:all(fun (V) -> V > 1 end, Gen).
  false

any/2

any(Fun::fun((V) -> boolean()), Generator::generator(V)) -> boolean()

Checks if any of the values produced by a generator satisfies a predicate.

  1> Gen = lazy:seq(1, 3).
  #Fun<lazy.43.73700886>
 
  2> lazy:any(fun (V) -> V =< 1 end, Gen).
  true
 
  3> lazy:any(fun (V) -> V =< 0 end, Gen).
  false

append/1

append(Generators::[generator(V)]) -> generator(V)

Creates a generator that is the concatenation of all of the given generators.

  1> Gen0 = lazy:seq(1, 3).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:from_list([a, b, c]).
  #Fun<lazy.1.73700886>
 
  3> Gen2 = lazy:once("foo").
  #Fun<lazy.4.73700886>
 
  4> Gen3 = lazy:append([Gen0, Gen1, Gen2]).
  #Fun<lazy.29.73700886>
 
  5> lazy:to_list(Gen3).
  [1, 2, 3, a, b, c, "foo"]

append/2

append(Generator1::generator(V1), Generator2::generator(V2)) -> generator(V1 | V2)

Creates a generator that is the concatenation of the two given generators.

  1> Gen0 = lazy:seq(1, 3).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:from_list([a, b, c]).
  #Fun<lazy.1.73700886>
 
  3> Gen2 = lazy:append(Gen0, Gen1).
  #Fun<lazy.29.73700886>
 
  4> lazy:to_list(Gen2).
  [1, 2, 3, a, b, c]

apply/2

apply(Fun, Generator) -> any()

Creates a generator that applies the given function to the values produced by the given generator. The functions return value is ignored and the generator will re-produce the value taken from the given generator unchanged.

For an example, see flush/1.

cycle/1

cycle(Generator::generator(V)) -> generator(V)

Creates a generator that cycles the given generator.

  1> Gen0 = lazy:from_list([a, b]).
  #Fun<lazy.1.73700886>
 
  2> Gen1 = lazy:cycle(Gen0).
  #Fun<lazy.9.73700886>
 
  3> {_, Gen2} = lazy:next(Gen1).
  {a, #Fun<lazy.10.73700886>}
 
  4> {_, Gen3} = lazy:next(Gen2).
  {b, #Fun<lazy.10.73700886>}
 
  5> {_, Gen4} = lazy:next(Gen3).
  {a, #Fun<lazy.10.73700886>}
 
  6> {_, Gen5} = lazy:next(Gen4).
  {b, #Fun<lazy.10.73700886>}
 
  ...

drop/2

drop(N::non_neg_integer(), Generator::generator(V)) -> generator(V)

Creates a generator that removes the given number of values from the given generator.

  1> Gen0 = lazy:seq(1, 10).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:drop(3, Gen0).
  #Fun<lazy.17.73700886>
 
  3> lazy:to_list(Gen1).
  [4, 5, 6, 7, 8, 9, 10]

dropwhile/2

dropwhile(Fun::fun((V) -> boolean()), Generator::generator(V)) -> generator(V)

Creates a generator that removes values from the given generator as long as the predicate holds.

  1> Gen0 = lazy:seq(1, 10).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:dropwhile(fun (V) -> V =<3 end, Gen0).
  #Fun<lazy.18.73700886>
 
  3> lazy:to_list(Gen1).
  [4, 5, 6, 7, 8, 9, 10]

empty/0

empty() -> fun(() -> empty)

Creates a generator that produces an empty sequence.

  1> Gen = lazy:empty().
  #Fun<lazy.3.73700886>
 
  2> lazy:next(Gen).
  empty

filter/2

filter(Fun::fun((V) -> boolean()), Generator::generator(V)) -> generator(V)

Creates a generator that produces the values taken from the given generator which satisfy the given predicate.

  1> Gen0 = lazy:seq(1, 10).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:filter(fun (V) -> V rem 2 =:= 0 end, Gen0).
  #Fun<lazy.21.73700886>
 
  3> lazy:to_list(Gen1).
  [2, 4, 6, 8, 10]

filtermap/2

filtermap(Fun::fun((V0) -> boolean() | {true, V1}), Generator::generator(V0)) -> generator(V0 | V1)

Creates a generator that combines filter and map into one.

  1> Gen0 = lazy:seq(0, 10).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:filtermap(fun (0) -> false; (V) when V rem 2 =:= 0 -> {true, -V}; (_) -> true end, Gen0).
  #Fun<lazy.23.73700886>
 
  3> lazy:to_list(Gen1).
  [1, -2, 3, -4, 5, -6, 7, -8, 9, -10]

flush/1

flush(Generator::generator(term())) -> ok

Flushes a generator. Useful to trigger side effects.

  1> Gen0 = lazy:seq(1, 3).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:apply(fun (V) -> self() ! V end, Gen0).
  #Fun<lazy.31.73700886>
 
  3> lazy:flush(Gen1).
  ok
 
  4> flush().
  Shell got 1
  Shell got 2
  Shell got 3
  ok

foldl/3

foldl(Fun::fun((V, term()) -> term()), Acc0::term(), Generator::generator(V)) -> term()

Folds over the sequence the given generator produces from the left.

  1> Gen = lazy:from_list([{a, 1}, {b, 2}, {a, 3}]).
  #Fun<lazy.1.73700886>
 
  2> lazy:foldl(fun ({K, V}, Acc) -> Acc#{K => V} end, #{}, Gen).
  #{a => 3, b => 2}

foldr/3

foldr(Fun::fun((V, term()) -> term()), Acc0::term(), Generator::generator(V)) -> term()

Folds over the sequence the given generator produces from the right.

  1> Gen = lazy:from_list([{a, 1}, {b, 2}, {a, 3}]).
  #Fun<lazy.1.73700886>
 
  2> lazy:foldr(fun ({K, V}, Acc) -> Acc#{K => V} end, #{}, Gen).
  #{a => 1, b => 2}

from_list/1

from_list(List::[V]) -> generator(V)

Turns a list into a generator.

For an example, see next/1.

iterate/2

iterate(Fun::fun((V0 | V1) -> V1), V0) -> generator(V1)

Creates a generator that produces values by iterative calls of the given function, feeding its own output back in with the next call.

  1> Gen0 = lazy:iterate(fun (V) -> 3 * V end, 1).
  #Fun<lazy.7.9483195>
 
  2> {_, Gen1} = lazy:next(Gen0).
  {1, #Fun<lazy.8.9483195>}
 
  3> {_, Gen2} = lazy:next(Gen1).
  {3, #Fun<lazy.8.9483195>}
 
  4> {_, Gen3} = lazy:next(Gen2).
  {9, #Fun<lazy.8.9483195>}
 
  ...

length/1

length(Generator::generator(term())) -> non_neg_integer()

Returns the number of values a generator produces.

  1> Gen = lazy:empty().
  #Fun<lazy.3.73700886>
 
  2> lazy:length(Gen).
  3

map/2

map(Fun::fun((V0) -> V1), Generator::generator(V0)) -> generator(V1)

Creates a generator that produces values taken from the given generator and mapped by the given function.

  1> Gen0 = lazy:seq(1, 5).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:map(fun (V) -> V * V end, Gen0).
  #Fun<lazy.19.73700886>
 
  3> lazy:to_list(Gen1).
  [1, 4, 9, 16, 25]

next/1

next(Generator::generator(V)) -> empty | {V, generator(V)}

Materializes and returns the next value of a generator.

  1> Gen0 = lazy:from_list([a, b, c]).
  #Fun<lazy.1.73700886>
 
  2> {_, Gen1} = lazy:next(Gen0).
  {a, #Fun<lazy.2.73700886>}
 
  3> {_, Gen2} = lazy:next(Gen1).
  {b, #Fun<lazy.2.73700886>}
 
  4> {_, Gen3} = lazy:next(Gen2).
  {c, #Fun<lazy.2.73700886>}
 
  5> lazy:next(Gen3).
  empty

once/1

once(V) -> generator(V)

Creates a generator that produces the given value exactly once.

  1> Gen0 = lazy:once(foo).
  #Fun<lazy.4.73700886>
 
  2> {_, Gen1} = lazy:next(Gen0).
  {foo, #Fun<lazy.50.73700886>}
 
  3> lazy:next(Gen1).
  empty

repeat/1

repeat(V) -> generator(V)

Creates a generator that produces repetitions of the given value.

  1> Gen0 = lazy:repeat(foo).
  #Fun<lazy.5.73700886>
 
  2> {_, Gen1} = lazy:next(Gen0).
  {foo, #Fun<lazy.49.73700886>}
 
  3> {_, Gen2} = lazy:next(Gen1).
  {foo, #Fun<lazy.49.73700886>}
 
  4> {_, Gen3} = lazy:next(Gen2).
  {foo, #Fun<lazy.49.73700886>}
 
  ...

repeatedly/1

repeatedly(Fun::fun(() -> V)) -> generator(V)

Creates a generator that produces values by repeated calls of the given function.

  1> Gen0 = lazy:repeatedly(fun () -> erlang:monotonic_time(millisecond) end).
  #Fun<lazy.6.73700886>
 
  2> {_, Gen1} = lazy:next(Gen0).
  {-576458245575, #Fun<lazy.48.73700886>}
 
  3> {_, Gen2} = lazy:next(Gen1).
  {-576458239259, #Fun<lazy.48.73700886>}
 
  4> {_, Gen3} = lazy:next(Gen2).
  {-576458232403, #Fun<lazy.48.73700886>}
 
  ...

reverse/1

reverse(Generator::generator(V)) -> generator(V)

Creates a generator that produces the values from the given generator in reverse order.

  1> Gen0 = lazy:seq(1, 10).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:reverse(Gen0).
  #Fun<lazy.1.73700886>
 
  3> lazy:to_list(Gen1).
  [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

scan/3

scan(Fun::fun((V1, term()) -> V2), Acc0::term(), Generator::generator(V1)) -> generator(V2)

Creates a generator that works similar to foldl but produces the intermediate accumulator value with each step.

  1> Gen0 = lazy:seq(1, 5).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:scan(fun (V, Acc) -> [V * V|Acc] end, [], Gen0).
  #Fun<lazy.26.73700886>
 
  3> lazy:to_list(Gen1).
  [[1], [4, 1], [9, 4, 1], [16, 9, 4, 1], [25, 16, 9, 4, 1]]

seq/2

seq(N1::integer(), N2::integer()) -> generator(integer())

Creates a generator that produces a sequence of integers, starting with the given start value up to the given end value or infinity, in increments of 1.

  1> Gen0 = lazy:seq(1, 3).
  #Fun<lazy.43.73700886>
 
  2> lazy:to_list(Gen0).
  [1, 2, 3]
 
  3> Gen1 = lazy:seq(1, infinity).
  #Fun<lazy.42.73700886>
 
  4> {_, Gen2} = lazy:next(Gen1).
  {1, #Fun<lazy.45.73700886>}
 
  5> {_, Gen3} = lazy:next(Gen2).
  {2, #Fun<lazy.45.73700886>}
 
  6> {_, Gen4} = lazy:next(Gen3).
  {3, #Fun<lazy.45.73700886>}
 
  7> {_, Gen5} = lazy:next(Gen4).
  {4, #Fun<lazy.45.73700886>}
 
  ...

seq/3

seq(N1::integer(), N2::integer() | infinity, Step::integer()) -> generator(integer())

Creates a generator that produces a sequence of integers, starting with the given start value up to the given end value or infinity, in increments of the given step value.

  1> Gen0 = lazy:seq(1, 5, 2).
  #Fun<lazy.43.73700886>
 
  2> lazy:to_list(Gen0).
  [1, 3, 5]
 
  3> Gen1 = lazy:seq(1, infinity, 2).
  #Fun<lazy.42.73700886>
 
  4> {_, Gen2} = lazy:next(Gen1).
  {1, #Fun<lazy.45.73700886>}
 
  5> {_, Gen3} = lazy:next(Gen2).
  {3, #Fun<lazy.45.73700886>}
 
  6> {_, Gen4} = lazy:next(Gen3).
  {5, #Fun<lazy.45.73700886>}
 
  7> {_, Gen5} = lazy:next(Gen4).
  {7, #Fun<lazy.45.73700886>}
 
  ...

take/2

take(N::non_neg_integer(), Generator::generator(V)) -> generator(V)

Creates a generator that produces the given number of values taken from the given generator.

  1> Gen0 = lazy:seq(1, 10).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:take(3, Gen0).
  #Fun<lazy.13.73700886>
 
  3> lazy:to_list(Gen1).
  [1, 2, 3]

takewhile/2

takewhile(Fun::fun((V) -> boolean()), Generator::generator(V)) -> generator(V)

Creates a generator that produces the values taken from the given generator as long as the predicate holds.

  1> Gen0 = lazy:seq(1, 10).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:takewhile(fun (V) -> V =<3 end, Gen0).
  #Fun<lazy.15.73700886>
 
  3> lazy:to_list(Gen1).
  [1, 2, 3]

to_list/1

to_list(Generator::generator(V)) -> [V]

Materializes a generator into a list.

  1> Gen = lazy:seq(1, 3).
  #Fun<lazy.43.73700886>
 
  2> lazy:to_list(Gen).
  [1, 2, 3]

unfold/2

unfold(Fun::fun((Acc0 | Acc1) -> empty | {V, Acc1}), Acc0) -> generator(V)

Creates a generator similar to iterate/2.

The given function must return a 2-tuple consisting of the value to return and an accumulator which will be fed back into the function with the next call.

  1> Gen = lazy:unfold(fun (0) -> empty; (V) -> {V, V div 2} end, 256).
  #Fun<lazy.11.73700886>
 
  2> lazy:to_list(Gen).
  [256, 128, 64, 32, 16, 8, 4, 2, 1]

unzip/1

unzip(Generator::generator({V1, V2})) -> {generator(V1), generator(V2)}

Creates two generators from a generator which produces 2-tuples, one for the first value in each tuple, one for the second.

  1> Gen0 = lazy:from_list([{a, 1}, {b, 2}, {c, 3}]).
  #Fun<lazy.1.73700886>
 
  2> {Gen1, Gen2} = lazy:unzip(Gen0).
  {#Fun<lazy.35.73700886>, #Fun<lazy.36.73700886>}
 
  3> lazy:to_list(Gen1).
  [a, b, c]
 
  4> lazy:to_list(Gen2).
  [1, 2, 3]

zip/2

zip(Generator1::generator(V1), Generator2::generator(V2)) -> generator({V1, V2})

Creates a generator that produces the values taken from the given generators wrapped together in a tuple.

  1> Gen0 = lazy:seq(1, 3).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:from_list([a, b, c]).
  #Fun<lazy.1.73700886>
 
  3> Gen2 = lazy:zip(Gen0, Gen1).
  #Fun<lazy.33.73700886>
 
  4> lazy:to_list(Gen2).
  [{1, a}, {2, b}, {3, c}]

zipwith/2

zipwith(Fun::fun((...) -> V1), Generators::[generator(V0)]) -> generator(V1)

Creates a generator that combines the values produced by all of the given generators by passing them to the given function. The arity of the given function must match the number of given generators.

  1> Gen0 = lazy:seq(1, 3).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:seq(4, 6).
  #Fun<lazy.43.73700886>
 
  3> Gen2 = lazy:seq(7, 9).
  #Fun<lazy.43.73700886>
 
  4> Gen3 = lazy:zipwith(fun (V1, V2, V3) -> (V1 + V2) * V3 end, [Gen0, Gen1, Gen2]).
  #Fun<lazy.40.73700886>
 
  6> lazy:to_list(Gen3).
  [35, 56, 81]

zipwith/3

zipwith(Fun::fun((V1, V2) -> V3), Generator1::generator(V1), Generator2::generator(V2)) -> generator(V3)

Creates a generator that combines the values produced by the two given generators by passing them to the given functions.

  1> Gen0 = lazy:seq(1, 3).
  #Fun<lazy.43.73700886>
 
  2> Gen1 = lazy:seq(4, 6).
  #Fun<lazy.43.73700886>
 
  3> Gen2 = lazy:zipwith(fun (V1, V2) -> V1 + V2 end, Gen0, Gen1).
  #Fun<lazy.40.73700886>
 
  4> lazy:to_list(Gen2).
  [5, 7, 9]


Generated by EDoc