A ReasonML generator library
Most of the documentation, together with examples, can be found inline
Represents the next value yielded by a generator
type gen('a)
Represents the api of a generator
gen('a) => next('a)
next :Increments the generator given, and returns its next) value.
Forces the generator to return the 2nd argument and stop.
gen('a), Js.Exn.t) => next('a)
throw : (Throws the error in the generator and forces it to stop.
Creates a generator from an external source, which implements the javascript generator api.
Creates a generator returning numbers from the range
<from; to_>
separated by the distance ofstep
. Creates an int range.
- RE
- ML
range(~to_=10)->foldl((+), 0) // => 55
2310: syntax error, consider adding a `;' before
Converts a list to a generator.
- RE
- ML
[1,2,3]->from_list->foldl((+), 0) // => 6
2310: syntax error, consider adding a `;' before
Creates an infinite generator from an initial value and a successor function.
- RE
- ML
inf(0, a => a + 1) // => <0, 1, 2, 3, ...>
2282: <UNKNOWN SYNTAX ERROR>
Transforms the values of a generator.
- RE
- ML
range(~to_=5, ()) -> map(a => a * 2) // => <0, 2, 4, 6, 8, 10>
2310: syntax error, consider adding a `;' before
Returns only the values of the generator which match the function.
- RE
- ML
range(~to_=10, ()) -> keep(a => a mod 2 == 0) // => <0, 2, 4, 6, 8, 10>
2310: syntax error, consider adding a `;' before
Consumes a generator with a function.
- RE
- ML
// prints an infinite generator inf(0, a => a + 1) -> consume(Js.Console.log) // 0 // 1 // 2 ... // => unit
0: <UNKNOWN SYNTAX ERROR>
Folds a generator into a single value.
- RE
- ML
[1,2,3]->from_list->foldl((+), 0) // => 6
2310: syntax error, consider adding a `;' before
Folds a generator into a single value, taking the first value as the initial value.
- RE
- ML
range(~to_=3, ~from=1,()) ->map(float_of_int) ->foldl1((a, b) => a /. b) // => 1/6
2310: syntax error, consider adding a `;' before
Takes n values of the generator or until it ends.
- RE
- ML
inf(0, a => a + 1) -> take(5) // => <0, 1, 2, 3, 4>
2310: syntax error, consider adding a `;' before
Takes the values of the generator as long as the function returns
true
.
- RE
- ML
inf(0, a => a*2 + 1)->take_while(a => a < 15) // => <0, 1, 3, 7>
2310: syntax error, consider adding a `;' before
Returns the next value of an async generator
Returns the value given (as a promise), and forces the generator to stop
Throw the given error in the generator and forces it to stop
Creates a generator from an external source, which implements the async iterator api.
Converts a sync generator into an async one.
- RE
- ML
inf(1, a => a + 1) ->from_sync ->take_async(100) ->consume_async(Js.Console.log)
2310: syntax error, consider adding a `;' before
Transforms an async generator using the function given.
- RE
- ML
puppy_stream -> map_async(pet) // => <async("doggo_1"), async("doggo_2"), ...>
2310: syntax error, consider adding a `;' before
Folds an async generetor into a single promise
- RE
- ML
open_file("data/numbers.txt") ->map_async(Js.Float.fromString) ->foldl_async((+.), 0.) // => async(21)
2310: syntax error, consider adding a `;' before
gen_async('a), (('b, 'a) => 'b)) => [async('b)]
foldl1_async : (Folds an async generator into a single promise, taking the first value as the initial value.
- RE
- ML
range(~to_=3, ~from=1, ()) ->map(float_of_int) ->from_sync ->foldl1_async((a, b) => a /. b) // => async(1/6)
2310: syntax error, consider adding a `;' before
Consumes an async stream, and returns an empty promise.
- RE
- ML
from_async(Deno.iter(file, ())) ->consume_async(Js.Console.log) // (prints the file) // => async(unit)
2310: syntax error, consider adding a `;' before
Takes the first n elements from the async generator, and returns a new one of the length n.
- RE
- ML
infinite_puppies ->take_async(5) // => async<"puppy1", "puppy2", ... , "puppy5">
2310: syntax error, consider adding a `;' before
Takes elements of the generator as long as the condition given is met. Returns a new async generator.
Returns an async generator of the items which meet the condition given.
open Sync;
let () = inf(0, a => a + 1)
->map(a =>
if (a mod 15 == 0) {
"fizzbuzz";
} else if (a mod 3 == 0) {
"fizz";
} else if (a mod 5 == 0) {
"buzz";
} else {
Js.Int.toString(a);
}
)
->consume(Js.Console.log);
967: syntax error, consider adding a `;' before