Packages
gleam_stdlib
0.19.2
1.0.3
1.0.2
1.0.1
1.0.0
0.71.0
0.70.0
0.69.0
0.68.1
0.68.0
0.67.1
0.67.0
0.65.0
0.64.0
0.63.2
0.63.1
0.63.0
0.62.1
0.62.0
0.61.0
0.60.0
0.59.0
0.58.0
0.57.0
0.56.0
0.55.0
0.54.0
0.53.0
0.52.0
0.51.0
0.50.0
0.49.0
0.48.0
0.47.0
0.46.0
0.45.0
0.44.0
0.43.0
0.42.0
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.1
0.35.0
0.34.0
0.33.1
0.33.0
0.32.1
0.32.0
0.31.0
0.30.2
0.30.1
0.30.0
0.29.2
0.29.1
0.29.0
0.28.2
0.28.1
0.28.0
0.27.0
0.26.1
0.26.0
0.25.0
0.24.0
0.23.0
0.22.3
0.22.2
0.22.1
0.22.0
0.21.0
0.20.0
0.19.3
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.18.0-rc1
0.17.1
0.17.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.1
0.10.0
0.9.0
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.4.0-rc1
0.3.1
0.3.0
0.2.0
retired
A standard library for the Gleam programming language
Current section
Files
Jump to
Current section
Files
src/gleam@iterator.erl
-module(gleam@iterator).
-compile(no_auto_import).
-export([unfold/2, repeatedly/1, repeat/1, from_list/1, fold/3, run/1, to_list/1, step/1, take/2, drop/2, map/2, append/2, flatten/1, flat_map/2, filter/2, cycle/1, range/2, find/2, index/1, iterate/2, take_while/2, drop_while/2, scan/3, zip/2, chunk/2, sized_chunk/2, intersperse/2, any/2, all/2, group/2, reduce/2, last/1, empty/0, once/1, single/1, interleave/2, fold_until/3, try_fold/3, first/1, at/2]).
-export_type([action/1, iterator/1, step/2, chunk/2, sized_chunk/1]).
-type action(BBJ) :: stop | {continue, BBJ, fun(() -> action(BBJ))}.
-opaque iterator(BBK) :: {iterator, fun(() -> action(BBK))}.
-type step(BBL, BBM) :: {next, BBL, BBM} | done.
-type chunk(BBN, BBO) :: {another_by,
list(BBN),
BBO,
BBN,
fun(() -> action(BBN))} |
{last_by, list(BBN)}.
-type sized_chunk(BBP) :: {another, list(BBP), fun(() -> action(BBP))} |
{last, list(BBP)} |
no_more.
-spec stop() -> action(any()).
stop() ->
stop.
-spec do_unfold(BBU, fun((BBU) -> step(BBV, BBU))) -> fun(() -> action(BBV)).
do_unfold(Initial, F) ->
fun() -> case F(Initial) of
{next, X, Acc} ->
{continue, X, do_unfold(Acc, F)};
done ->
stop
end end.
-spec unfold(BBZ, fun((BBZ) -> step(BCA, BBZ))) -> iterator(BCA).
unfold(Initial, F) ->
_pipe = Initial,
_pipe@1 = do_unfold(_pipe, F),
{iterator, _pipe@1}.
-spec repeatedly(fun(() -> BCE)) -> iterator(BCE).
repeatedly(F) ->
unfold(nil, fun(_) -> {next, F(), nil} end).
-spec repeat(BCG) -> iterator(BCG).
repeat(X) ->
repeatedly(fun() -> X end).
-spec from_list(list(BCI)) -> iterator(BCI).
from_list(List) ->
Yield = fun(Acc) -> case Acc of
[] ->
done;
[Head | Tail] ->
{next, Head, Tail}
end end,
unfold(List, Yield).
-spec do_fold(fun(() -> action(BCL)), fun((BCN, BCL) -> BCN), BCN) -> BCN.
do_fold(Continuation, F, Accumulator) ->
case Continuation() of
{continue, Elem, Next} ->
do_fold(Next, F, F(Accumulator, Elem));
stop ->
Accumulator
end.
-spec fold(iterator(BCO), BCQ, fun((BCQ, BCO) -> BCQ)) -> BCQ.
fold(Iterator, Initial, F) ->
_pipe = erlang:element(2, Iterator),
do_fold(_pipe, F, Initial).
-spec run(iterator(any())) -> nil.
run(Iterator) ->
fold(Iterator, nil, fun(_, _) -> nil end).
-spec to_list(iterator(BCT)) -> list(BCT).
to_list(Iterator) ->
_pipe = Iterator,
_pipe@1 = fold(_pipe, [], fun(Acc, E) -> [E | Acc] end),
gleam@list:reverse(_pipe@1).
-spec step(iterator(BCW)) -> step(BCW, iterator(BCW)).
step(Iterator) ->
case (erlang:element(2, Iterator))() of
stop ->
done;
{continue, E, A} ->
{next, E, {iterator, A}}
end.
-spec do_take(fun(() -> action(BDB)), integer()) -> fun(() -> action(BDB)).
do_take(Continuation, Desired) ->
fun() -> case Desired > 0 of
false ->
stop;
true ->
case Continuation() of
stop ->
stop;
{continue, E, Next} ->
{continue, E, do_take(Next, Desired - 1)}
end
end end.
-spec take(iterator(BDE), integer()) -> iterator(BDE).
take(Iterator, Desired) ->
_pipe = erlang:element(2, Iterator),
_pipe@1 = do_take(_pipe, Desired),
{iterator, _pipe@1}.
-spec do_drop(fun(() -> action(BDH)), integer()) -> action(BDH).
do_drop(Continuation, Desired) ->
case Continuation() of
stop ->
stop;
{continue, E, Next} ->
case Desired > 0 of
true ->
do_drop(Next, Desired - 1);
false ->
{continue, E, Next}
end
end.
-spec drop(iterator(BDK), integer()) -> iterator(BDK).
drop(Iterator, Desired) ->
_pipe = fun() -> do_drop(erlang:element(2, Iterator), Desired) end,
{iterator, _pipe}.
-spec do_map(fun(() -> action(BDN)), fun((BDN) -> BDP)) -> fun(() -> action(BDP)).
do_map(Continuation, F) ->
fun() -> case Continuation() of
stop ->
stop;
{continue, E, Continuation@1} ->
{continue, F(E), do_map(Continuation@1, F)}
end end.
-spec map(iterator(BDR), fun((BDR) -> BDT)) -> iterator(BDT).
map(Iterator, F) ->
_pipe = erlang:element(2, Iterator),
_pipe@1 = do_map(_pipe, F),
{iterator, _pipe@1}.
-spec do_append(fun(() -> action(BDV)), fun(() -> action(BDV))) -> action(BDV).
do_append(First, Second) ->
case First() of
{continue, E, First@1} ->
{continue, E, fun() -> do_append(First@1, Second) end};
stop ->
Second()
end.
-spec append(iterator(BDZ), iterator(BDZ)) -> iterator(BDZ).
append(First, Second) ->
_pipe = fun() ->
do_append(erlang:element(2, First), erlang:element(2, Second))
end,
{iterator, _pipe}.
-spec do_flatten(fun(() -> action(iterator(BED)))) -> action(BED).
do_flatten(Flattened) ->
case Flattened() of
stop ->
stop;
{continue, It, Next_iterator} ->
do_append(
erlang:element(2, It),
fun() -> do_flatten(Next_iterator) end
)
end.
-spec flatten(iterator(iterator(BEH))) -> iterator(BEH).
flatten(Iterator) ->
_pipe = fun() -> do_flatten(erlang:element(2, Iterator)) end,
{iterator, _pipe}.
-spec flat_map(iterator(BEL), fun((BEL) -> iterator(BEN))) -> iterator(BEN).
flat_map(Iterator, F) ->
_pipe = Iterator,
_pipe@1 = map(_pipe, F),
flatten(_pipe@1).
-spec do_filter(fun(() -> action(BEQ)), fun((BEQ) -> boolean())) -> action(BEQ).
do_filter(Continuation, Predicate) ->
case Continuation() of
stop ->
stop;
{continue, E, Iterator} ->
case Predicate(E) of
true ->
{continue, E, fun() -> do_filter(Iterator, Predicate) end};
false ->
do_filter(Iterator, Predicate)
end
end.
-spec filter(iterator(BET), fun((BET) -> boolean())) -> iterator(BET).
filter(Iterator, Predicate) ->
_pipe = fun() -> do_filter(erlang:element(2, Iterator), Predicate) end,
{iterator, _pipe}.
-spec cycle(iterator(BEW)) -> iterator(BEW).
cycle(Iterator) ->
_pipe = repeat(Iterator),
flatten(_pipe).
-spec range(integer(), integer()) -> iterator(integer()).
range(Start, Stop) ->
Increment = case Start < Stop of
true ->
1;
false ->
-1
end,
Next_step = fun(Current) -> case Current =:= Stop of
true ->
done;
false ->
{next, Current, Current + Increment}
end end,
unfold(Start, Next_step).
-spec do_find(fun(() -> action(BFA)), fun((BFA) -> boolean())) -> {ok, BFA} |
{error, nil}.
do_find(Continuation, F) ->
case Continuation() of
stop ->
{error, nil};
{continue, E, Next} ->
case F(E) of
true ->
{ok, E};
false ->
do_find(Next, F)
end
end.
-spec find(iterator(BFE), fun((BFE) -> boolean())) -> {ok, BFE} | {error, nil}.
find(Haystack, Is_desired) ->
_pipe = erlang:element(2, Haystack),
do_find(_pipe, Is_desired).
-spec do_index(fun(() -> action(BFI)), integer()) -> fun(() -> action({integer(),
BFI})).
do_index(Continuation, Next) ->
fun() -> case Continuation() of
stop ->
stop;
{continue, E, Continuation@1} ->
{continue, {Next, E}, do_index(Continuation@1, Next + 1)}
end end.
-spec index(iterator(BFL)) -> iterator({integer(), BFL}).
index(Iterator) ->
_pipe = erlang:element(2, Iterator),
_pipe@1 = do_index(_pipe, 0),
{iterator, _pipe@1}.
-spec iterate(BFO, fun((BFO) -> BFO)) -> iterator(BFO).
iterate(Initial, F) ->
unfold(Initial, fun(Element) -> {next, Element, F(Element)} end).
-spec do_take_while(fun(() -> action(BFQ)), fun((BFQ) -> boolean())) -> fun(() -> action(BFQ)).
do_take_while(Continuation, Predicate) ->
fun() -> case Continuation() of
stop ->
stop;
{continue, E, Next} ->
case Predicate(E) of
false ->
stop;
true ->
{continue, E, do_take_while(Next, Predicate)}
end
end end.
-spec take_while(iterator(BFT), fun((BFT) -> boolean())) -> iterator(BFT).
take_while(Iterator, Predicate) ->
_pipe = erlang:element(2, Iterator),
_pipe@1 = do_take_while(_pipe, Predicate),
{iterator, _pipe@1}.
-spec do_drop_while(fun(() -> action(BFW)), fun((BFW) -> boolean())) -> action(BFW).
do_drop_while(Continuation, Predicate) ->
case Continuation() of
stop ->
stop;
{continue, E, Next} ->
case Predicate(E) of
false ->
{continue, E, Next};
true ->
do_drop_while(Next, Predicate)
end
end.
-spec drop_while(iterator(BFZ), fun((BFZ) -> boolean())) -> iterator(BFZ).
drop_while(Iterator, Predicate) ->
_pipe = fun() -> do_drop_while(erlang:element(2, Iterator), Predicate) end,
{iterator, _pipe}.
-spec do_scan(fun(() -> action(BGC)), fun((BGE, BGC) -> BGE), BGE) -> fun(() -> action(BGE)).
do_scan(Continuation, F, Accumulator) ->
fun() -> case Continuation() of
stop ->
stop;
{continue, El, Next} ->
Accumulated = F(Accumulator, El),
{continue, Accumulated, do_scan(Next, F, Accumulated)}
end end.
-spec scan(iterator(BGG), BGI, fun((BGI, BGG) -> BGI)) -> iterator(BGI).
scan(Iterator, Initial, F) ->
_pipe = erlang:element(2, Iterator),
_pipe@1 = do_scan(_pipe, F, Initial),
{iterator, _pipe@1}.
-spec do_zip(fun(() -> action(BGK)), fun(() -> action(BGM))) -> fun(() -> action({BGK,
BGM})).
do_zip(Left, Right) ->
fun() -> case Left() of
stop ->
stop;
{continue, El_left, Next_left} ->
case Right() of
stop ->
stop;
{continue, El_right, Next_right} ->
{continue,
{El_left, El_right},
do_zip(Next_left, Next_right)}
end
end end.
-spec zip(iterator(BGP), iterator(BGR)) -> iterator({BGP, BGR}).
zip(Left, Right) ->
_pipe = do_zip(erlang:element(2, Left), erlang:element(2, Right)),
{iterator, _pipe}.
-spec next_chunk(fun(() -> action(BGX)), fun((BGX) -> BGZ), BGZ, list(BGX)) -> chunk(BGX, BGZ).
next_chunk(Continuation, F, Previous_key, Current_chunk) ->
case Continuation() of
stop ->
{last_by, gleam@list:reverse(Current_chunk)};
{continue, E, Next} ->
Key = F(E),
case Key =:= Previous_key of
true ->
next_chunk(Next, F, Key, [E | Current_chunk]);
false ->
{another_by,
gleam@list:reverse(Current_chunk),
Key,
E,
Next}
end
end.
-spec do_chunk(fun(() -> action(BHD)), fun((BHD) -> BHF), BHF, BHD) -> action(list(BHD)).
do_chunk(Continuation, F, Previous_key, Previous_element) ->
case next_chunk(Continuation, F, Previous_key, [Previous_element]) of
{last_by, Chunk} ->
{continue, Chunk, fun stop/0};
{another_by, Chunk@1, Key, El, Next} ->
{continue, Chunk@1, fun() -> do_chunk(Next, F, Key, El) end}
end.
-spec chunk(iterator(BHI), fun((BHI) -> any())) -> iterator(list(BHI)).
chunk(Iterator, F) ->
_pipe = fun() -> case (erlang:element(2, Iterator))() of
stop ->
stop;
{continue, E, Next} ->
do_chunk(Next, F, F(E), E)
end end,
{iterator, _pipe}.
-spec next_sized_chunk(fun(() -> action(BHQ)), integer(), list(BHQ)) -> sized_chunk(BHQ).
next_sized_chunk(Continuation, Left, Current_chunk) ->
case Continuation() of
stop ->
case Current_chunk of
[] ->
no_more;
Remaining ->
{last, gleam@list:reverse(Remaining)}
end;
{continue, E, Next} ->
Chunk = [E | Current_chunk],
case Left > 1 of
false ->
{another, gleam@list:reverse(Chunk), Next};
true ->
next_sized_chunk(Next, Left - 1, Chunk)
end
end.
-spec do_sized_chunk(fun(() -> action(BHU)), integer()) -> fun(() -> action(list(BHU))).
do_sized_chunk(Continuation, Count) ->
fun() -> case next_sized_chunk(Continuation, Count, []) of
no_more ->
stop;
{last, Chunk} ->
{continue, Chunk, fun stop/0};
{another, Chunk@1, Next_element} ->
{continue, Chunk@1, do_sized_chunk(Next_element, Count)}
end end.
-spec sized_chunk(iterator(BHY), integer()) -> iterator(list(BHY)).
sized_chunk(Iterator, Count) ->
_pipe = erlang:element(2, Iterator),
_pipe@1 = do_sized_chunk(_pipe, Count),
{iterator, _pipe@1}.
-spec do_intersperse(fun(() -> action(BIC)), BIC) -> action(BIC).
do_intersperse(Continuation, Separator) ->
case Continuation() of
stop ->
stop;
{continue, E, Next} ->
Next_interspersed = fun() -> do_intersperse(Next, Separator) end,
{continue, Separator, fun() -> {continue, E, Next_interspersed} end}
end.
-spec intersperse(iterator(BIF), BIF) -> iterator(BIF).
intersperse(Iterator, Elem) ->
_pipe = fun() -> case (erlang:element(2, Iterator))() of
stop ->
stop;
{continue, E, Next} ->
{continue, E, fun() -> do_intersperse(Next, Elem) end}
end end,
{iterator, _pipe}.
-spec do_any(fun(() -> action(BII)), fun((BII) -> boolean())) -> boolean().
do_any(Continuation, Predicate) ->
case Continuation() of
stop ->
false;
{continue, E, Next} ->
Predicate(E) orelse do_any(Next, Predicate)
end.
-spec any(iterator(BIK), fun((BIK) -> boolean())) -> boolean().
any(Iterator, Predicate) ->
_pipe = erlang:element(2, Iterator),
do_any(_pipe, Predicate).
-spec do_all(fun(() -> action(BIM)), fun((BIM) -> boolean())) -> boolean().
do_all(Continuation, Predicate) ->
case Continuation() of
stop ->
true;
{continue, E, Next} ->
Predicate(E) andalso do_all(Next, Predicate)
end.
-spec all(iterator(BIO), fun((BIO) -> boolean())) -> boolean().
all(Iterator, Predicate) ->
_pipe = erlang:element(2, Iterator),
do_all(_pipe, Predicate).
-spec update_group_with(BIQ) -> fun((gleam@option:option(list(BIQ))) -> list(BIQ)).
update_group_with(El) ->
fun(Maybe_group) -> case Maybe_group of
{some, Group} ->
[El | Group];
none ->
[El]
end end.
-spec group_updater(fun((BIU) -> BIV)) -> fun((gleam@map:map_(BIV, list(BIU)), BIU) -> gleam@map:map_(BIV, list(BIU))).
group_updater(F) ->
fun(Groups, Elem) ->
_pipe = Groups,
gleam@map:update(_pipe, F(Elem), update_group_with(Elem))
end.
-spec group(iterator(BJC), fun((BJC) -> BJE)) -> gleam@map:map_(BJE, list(BJC)).
group(Iterator, Key) ->
_pipe = Iterator,
_pipe@1 = fold(_pipe, gleam@map:new(), group_updater(Key)),
gleam@map:map_values(
_pipe@1,
fun(_, Group) -> gleam@list:reverse(Group) end
).
-spec reduce(iterator(BJI), fun((BJI, BJI) -> BJI)) -> {ok, BJI} | {error, nil}.
reduce(Iterator, F) ->
case (erlang:element(2, Iterator))() of
stop ->
{error, nil};
{continue, E, Next} ->
_pipe = do_fold(Next, F, E),
{ok, _pipe}
end.
-spec last(iterator(BJM)) -> {ok, BJM} | {error, nil}.
last(Iterator) ->
_pipe = Iterator,
reduce(_pipe, fun(_, Elem) -> Elem end).
-spec empty() -> iterator(any()).
empty() ->
{iterator, fun stop/0}.
-spec once(fun(() -> BJS)) -> iterator(BJS).
once(F) ->
_pipe = fun() -> {continue, F(), fun stop/0} end,
{iterator, _pipe}.
-spec single(BJU) -> iterator(BJU).
single(Elem) ->
once(fun() -> Elem end).
-spec do_interleave(fun(() -> action(BJW)), fun(() -> action(BJW))) -> action(BJW).
do_interleave(Current, Next) ->
case Current() of
stop ->
Next();
{continue, E, Next_other} ->
{continue, E, fun() -> do_interleave(Next, Next_other) end}
end.
-spec interleave(iterator(BKA), iterator(BKA)) -> iterator(BKA).
interleave(Left, Right) ->
_pipe = fun() ->
do_interleave(erlang:element(2, Left), erlang:element(2, Right))
end,
{iterator, _pipe}.
-spec do_fold_until(
fun(() -> action(BKE)),
fun((BKG, BKE) -> gleam@list:continue_or_stop(BKG)),
BKG
) -> BKG.
do_fold_until(Continuation, F, Accumulator) ->
case Continuation() of
stop ->
Accumulator;
{continue, Elem, Next} ->
case F(Accumulator, Elem) of
{continue, Accumulator@1} ->
do_fold_until(Next, F, Accumulator@1);
{stop, Accumulator@2} ->
Accumulator@2
end
end.
-spec fold_until(
iterator(BKI),
BKK,
fun((BKK, BKI) -> gleam@list:continue_or_stop(BKK))
) -> BKK.
fold_until(Iterator, Initial, F) ->
_pipe = erlang:element(2, Iterator),
do_fold_until(_pipe, F, Initial).
-spec do_try_fold(
fun(() -> action(BKM)),
fun((BKO, BKM) -> {ok, BKO} | {error, BKP}),
BKO
) -> {ok, BKO} | {error, BKP}.
do_try_fold(Continuation, F, Accumulator) ->
case Continuation() of
stop ->
{ok, Accumulator};
{continue, Elem, Next} ->
case F(Accumulator, Elem) of
{error, _try} -> {error, _try};
{ok, Accumulator@1} ->
do_try_fold(Next, F, Accumulator@1)
end
end.
-spec try_fold(iterator(BKU), BKW, fun((BKW, BKU) -> {ok, BKW} | {error, BKX})) -> {ok,
BKW} |
{error, BKX}.
try_fold(Iterator, Initial, F) ->
_pipe = erlang:element(2, Iterator),
do_try_fold(_pipe, F, Initial).
-spec first(iterator(BLC)) -> {ok, BLC} | {error, nil}.
first(Iterator) ->
case (erlang:element(2, Iterator))() of
stop ->
{error, nil};
{continue, E, _@1} ->
{ok, E}
end.
-spec at(iterator(BLG), integer()) -> {ok, BLG} | {error, nil}.
at(Iterator, Index) ->
_pipe = Iterator,
_pipe@1 = drop(_pipe, Index),
first(_pipe@1).