Current section

Files

Jump to
gleam_stdlib src gleam@iterator.erl
Raw

src/gleam@iterator.erl

-module(gleam@iterator).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([unfold/2, repeatedly/1, repeat/1, from_list/1, transform/3, fold/3, run/1, to_list/1, step/1, take/2, drop/2, map/2, map2/3, append/2, flatten/1, concat/1, flat_map/2, filter/2, filter_map/2, cycle/1, find/2, find_map/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, range/2, single/1, interleave/2, fold_until/3, try_fold/3, first/1, at/2, length/1, each/2, yield/2]).
-export_type([action/1, iterator/1, step/2, chunk/2, sized_chunk/1]).
-type action(BXF) :: stop | {continue, BXF, fun(() -> action(BXF))}.
-opaque iterator(BXG) :: {iterator, fun(() -> action(BXG))}.
-type step(BXH, BXI) :: {next, BXH, BXI} | done.
-type chunk(BXJ, BXK) :: {another_by,
list(BXJ),
BXK,
BXJ,
fun(() -> action(BXJ))} |
{last_by, list(BXJ)}.
-type sized_chunk(BXL) :: {another, list(BXL), fun(() -> action(BXL))} |
{last, list(BXL)} |
no_more.
-spec stop() -> action(any()).
stop() ->
stop.
-spec do_unfold(BXO, fun((BXO) -> step(BXP, BXO))) -> fun(() -> action(BXP)).
do_unfold(Initial, F) ->
fun() -> case F(Initial) of
{next, X, Acc} ->
{continue, X, do_unfold(Acc, F)};
done ->
stop
end end.
-spec unfold(BXT, fun((BXT) -> step(BXU, BXT))) -> iterator(BXU).
unfold(Initial, F) ->
_pipe = Initial,
_pipe@1 = do_unfold(_pipe, F),
{iterator, _pipe@1}.
-spec repeatedly(fun(() -> BXY)) -> iterator(BXY).
repeatedly(F) ->
unfold(nil, fun(_) -> {next, F(), nil} end).
-spec repeat(BYA) -> iterator(BYA).
repeat(X) ->
repeatedly(fun() -> X end).
-spec from_list(list(BYC)) -> iterator(BYC).
from_list(List) ->
Yield = fun(Acc) -> case Acc of
[] ->
done;
[Head | Tail] ->
{next, Head, Tail}
end end,
unfold(List, Yield).
-spec do_transform(
fun(() -> action(BYF)),
BYH,
fun((BYH, BYF) -> step(BYI, BYH))
) -> fun(() -> action(BYI)).
do_transform(Continuation, State, F) ->
fun() -> case Continuation() of
stop ->
stop;
{continue, El, Next} ->
case F(State, El) of
done ->
stop;
{next, Yield, Next_state} ->
{continue, Yield, do_transform(Next, Next_state, F)}
end
end end.
-spec transform(iterator(BYM), BYO, fun((BYO, BYM) -> step(BYP, BYO))) -> iterator(BYP).
transform(Iterator, Initial, F) ->
_pipe = do_transform(erlang:element(2, Iterator), Initial, F),
{iterator, _pipe}.
-spec do_fold(fun(() -> action(BYT)), fun((BYV, BYT) -> BYV), BYV) -> BYV.
do_fold(Continuation, F, Accumulator) ->
case Continuation() of
{continue, Elem, Next} ->
do_fold(Next, F, F(Accumulator, Elem));
stop ->
Accumulator
end.
-spec fold(iterator(BYW), BYY, fun((BYY, BYW) -> BYY)) -> BYY.
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(BZB)) -> list(BZB).
to_list(Iterator) ->
_pipe = Iterator,
_pipe@1 = fold(_pipe, [], fun(Acc, E) -> [E | Acc] end),
lists:reverse(_pipe@1).
-spec step(iterator(BZE)) -> step(BZE, iterator(BZE)).
step(Iterator) ->
case (erlang:element(2, Iterator))() of
stop ->
done;
{continue, E, A} ->
{next, E, {iterator, A}}
end.
-spec do_take(fun(() -> action(BZJ)), integer()) -> fun(() -> action(BZJ)).
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(BZM), integer()) -> iterator(BZM).
take(Iterator, Desired) ->
_pipe = erlang:element(2, Iterator),
_pipe@1 = do_take(_pipe, Desired),
{iterator, _pipe@1}.
-spec do_drop(fun(() -> action(BZP)), integer()) -> action(BZP).
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(BZS), integer()) -> iterator(BZS).
drop(Iterator, Desired) ->
_pipe = fun() -> do_drop(erlang:element(2, Iterator), Desired) end,
{iterator, _pipe}.
-spec do_map(fun(() -> action(BZV)), fun((BZV) -> BZX)) -> fun(() -> action(BZX)).
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(BZZ), fun((BZZ) -> CAB)) -> iterator(CAB).
map(Iterator, F) ->
_pipe = erlang:element(2, Iterator),
_pipe@1 = do_map(_pipe, F),
{iterator, _pipe@1}.
-spec do_map2(
fun(() -> action(CAD)),
fun(() -> action(CAF)),
fun((CAD, CAF) -> CAH)
) -> fun(() -> action(CAH)).
do_map2(Continuation1, Continuation2, Fun) ->
fun() -> case Continuation1() of
stop ->
stop;
{continue, A, Next_a} ->
case Continuation2() of
stop ->
stop;
{continue, B, Next_b} ->
{continue, Fun(A, B), do_map2(Next_a, Next_b, Fun)}
end
end end.
-spec map2(iterator(CAJ), iterator(CAL), fun((CAJ, CAL) -> CAN)) -> iterator(CAN).
map2(Iterator1, Iterator2, Fun) ->
_pipe = do_map2(
erlang:element(2, Iterator1),
erlang:element(2, Iterator2),
Fun
),
{iterator, _pipe}.
-spec do_append(fun(() -> action(CAP)), fun(() -> action(CAP))) -> action(CAP).
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(CAT), iterator(CAT)) -> iterator(CAT).
append(First, Second) ->
_pipe = fun() ->
do_append(erlang:element(2, First), erlang:element(2, Second))
end,
{iterator, _pipe}.
-spec do_flatten(fun(() -> action(iterator(CAX)))) -> action(CAX).
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(CBB))) -> iterator(CBB).
flatten(Iterator) ->
_pipe = fun() -> do_flatten(erlang:element(2, Iterator)) end,
{iterator, _pipe}.
-spec concat(list(iterator(CBF))) -> iterator(CBF).
concat(Iterators) ->
flatten(from_list(Iterators)).
-spec flat_map(iterator(CBJ), fun((CBJ) -> iterator(CBL))) -> iterator(CBL).
flat_map(Iterator, F) ->
_pipe = Iterator,
_pipe@1 = map(_pipe, F),
flatten(_pipe@1).
-spec do_filter(fun(() -> action(CBO)), fun((CBO) -> boolean())) -> action(CBO).
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(CBR), fun((CBR) -> boolean())) -> iterator(CBR).
filter(Iterator, Predicate) ->
_pipe = fun() -> do_filter(erlang:element(2, Iterator), Predicate) end,
{iterator, _pipe}.
-spec do_filter_map(
fun(() -> action(CBU)),
fun((CBU) -> {ok, CBW} | {error, any()})
) -> action(CBW).
do_filter_map(Continuation, F) ->
case Continuation() of
stop ->
stop;
{continue, E, Next} ->
case F(E) of
{ok, E@1} ->
{continue, E@1, fun() -> do_filter_map(Next, F) end};
{error, _} ->
do_filter_map(Next, F)
end
end.
-spec filter_map(iterator(CCB), fun((CCB) -> {ok, CCD} | {error, any()})) -> iterator(CCD).
filter_map(Iterator, F) ->
_pipe = fun() -> do_filter_map(erlang:element(2, Iterator), F) end,
{iterator, _pipe}.
-spec cycle(iterator(CCI)) -> iterator(CCI).
cycle(Iterator) ->
_pipe = repeat(Iterator),
flatten(_pipe).
-spec do_find(fun(() -> action(CCM)), fun((CCM) -> boolean())) -> {ok, CCM} |
{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(CCQ), fun((CCQ) -> boolean())) -> {ok, CCQ} | {error, nil}.
find(Haystack, Is_desired) ->
_pipe = erlang:element(2, Haystack),
do_find(_pipe, Is_desired).
-spec do_find_map(
fun(() -> action(CCU)),
fun((CCU) -> {ok, CCW} | {error, any()})
) -> {ok, CCW} | {error, nil}.
do_find_map(Continuation, F) ->
case Continuation() of
stop ->
{error, nil};
{continue, E, Next} ->
case F(E) of
{ok, E@1} ->
{ok, E@1};
{error, _} ->
do_find_map(Next, F)
end
end.
-spec find_map(iterator(CDC), fun((CDC) -> {ok, CDE} | {error, any()})) -> {ok,
CDE} |
{error, nil}.
find_map(Haystack, Is_desired) ->
_pipe = erlang:element(2, Haystack),
do_find_map(_pipe, Is_desired).
-spec do_index(fun(() -> action(CDK)), integer()) -> fun(() -> action({CDK,
integer()})).
do_index(Continuation, Next) ->
fun() -> case Continuation() of
stop ->
stop;
{continue, E, Continuation@1} ->
{continue, {E, Next}, do_index(Continuation@1, Next + 1)}
end end.
-spec index(iterator(CDN)) -> iterator({CDN, integer()}).
index(Iterator) ->
_pipe = erlang:element(2, Iterator),
_pipe@1 = do_index(_pipe, 0),
{iterator, _pipe@1}.
-spec iterate(CDQ, fun((CDQ) -> CDQ)) -> iterator(CDQ).
iterate(Initial, F) ->
unfold(Initial, fun(Element) -> {next, Element, F(Element)} end).
-spec do_take_while(fun(() -> action(CDS)), fun((CDS) -> boolean())) -> fun(() -> action(CDS)).
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(CDV), fun((CDV) -> boolean())) -> iterator(CDV).
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(CDY)), fun((CDY) -> boolean())) -> action(CDY).
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(CEB), fun((CEB) -> boolean())) -> iterator(CEB).
drop_while(Iterator, Predicate) ->
_pipe = fun() -> do_drop_while(erlang:element(2, Iterator), Predicate) end,
{iterator, _pipe}.
-spec do_scan(fun(() -> action(CEE)), fun((CEG, CEE) -> CEG), CEG) -> fun(() -> action(CEG)).
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(CEI), CEK, fun((CEK, CEI) -> CEK)) -> iterator(CEK).
scan(Iterator, Initial, F) ->
_pipe = erlang:element(2, Iterator),
_pipe@1 = do_scan(_pipe, F, Initial),
{iterator, _pipe@1}.
-spec do_zip(fun(() -> action(CEM)), fun(() -> action(CEO))) -> fun(() -> action({CEM,
CEO})).
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(CER), iterator(CET)) -> iterator({CER, CET}).
zip(Left, Right) ->
_pipe = do_zip(erlang:element(2, Left), erlang:element(2, Right)),
{iterator, _pipe}.
-spec next_chunk(fun(() -> action(CEW)), fun((CEW) -> CEY), CEY, list(CEW)) -> chunk(CEW, CEY).
next_chunk(Continuation, F, Previous_key, Current_chunk) ->
case Continuation() of
stop ->
{last_by, lists: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, lists:reverse(Current_chunk), Key, E, Next}
end
end.
-spec do_chunk(fun(() -> action(CFC)), fun((CFC) -> CFE), CFE, CFC) -> action(list(CFC)).
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(CFH), fun((CFH) -> any())) -> iterator(list(CFH)).
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(CFM)), integer(), list(CFM)) -> sized_chunk(CFM).
next_sized_chunk(Continuation, Left, Current_chunk) ->
case Continuation() of
stop ->
case Current_chunk of
[] ->
no_more;
Remaining ->
{last, lists:reverse(Remaining)}
end;
{continue, E, Next} ->
Chunk = [E | Current_chunk],
case Left > 1 of
false ->
{another, lists:reverse(Chunk), Next};
true ->
next_sized_chunk(Next, Left - 1, Chunk)
end
end.
-spec do_sized_chunk(fun(() -> action(CFQ)), integer()) -> fun(() -> action(list(CFQ))).
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(CFU), integer()) -> iterator(list(CFU)).
sized_chunk(Iterator, Count) ->
_pipe = erlang:element(2, Iterator),
_pipe@1 = do_sized_chunk(_pipe, Count),
{iterator, _pipe@1}.
-spec do_intersperse(fun(() -> action(CFY)), CFY) -> action(CFY).
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(CGB), CGB) -> iterator(CGB).
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(CGE)), fun((CGE) -> boolean())) -> boolean().
do_any(Continuation, Predicate) ->
case Continuation() of
stop ->
false;
{continue, E, Next} ->
case Predicate(E) of
true ->
true;
false ->
do_any(Next, Predicate)
end
end.
-spec any(iterator(CGG), fun((CGG) -> boolean())) -> boolean().
any(Iterator, Predicate) ->
_pipe = erlang:element(2, Iterator),
do_any(_pipe, Predicate).
-spec do_all(fun(() -> action(CGI)), fun((CGI) -> boolean())) -> boolean().
do_all(Continuation, Predicate) ->
case Continuation() of
stop ->
true;
{continue, E, Next} ->
case Predicate(E) of
true ->
do_all(Next, Predicate);
false ->
false
end
end.
-spec all(iterator(CGK), fun((CGK) -> boolean())) -> boolean().
all(Iterator, Predicate) ->
_pipe = erlang:element(2, Iterator),
do_all(_pipe, Predicate).
-spec update_group_with(CGM) -> fun((gleam@option:option(list(CGM))) -> list(CGM)).
update_group_with(El) ->
fun(Maybe_group) -> case Maybe_group of
{some, Group} ->
[El | Group];
none ->
[El]
end end.
-spec group_updater(fun((CGQ) -> CGR)) -> fun((gleam@dict:dict(CGR, list(CGQ)), CGQ) -> gleam@dict:dict(CGR, list(CGQ))).
group_updater(F) ->
fun(Groups, Elem) -> _pipe = Groups,
gleam@dict:upsert(_pipe, F(Elem), update_group_with(Elem)) end.
-spec group(iterator(CGY), fun((CGY) -> CHA)) -> gleam@dict:dict(CHA, list(CGY)).
group(Iterator, Key) ->
_pipe = Iterator,
_pipe@1 = fold(_pipe, gleam@dict:new(), group_updater(Key)),
gleam@dict:map_values(_pipe@1, fun(_, Group) -> lists:reverse(Group) end).
-spec reduce(iterator(CHE), fun((CHE, CHE) -> CHE)) -> {ok, CHE} | {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(CHI)) -> {ok, CHI} | {error, nil}.
last(Iterator) ->
_pipe = Iterator,
reduce(_pipe, fun(_, Elem) -> Elem end).
-spec empty() -> iterator(any()).
empty() ->
{iterator, fun stop/0}.
-spec once(fun(() -> CHO)) -> iterator(CHO).
once(F) ->
_pipe = fun() -> {continue, F(), fun stop/0} end,
{iterator, _pipe}.
-spec range(integer(), integer()) -> iterator(integer()).
range(Start, Stop) ->
case gleam@int:compare(Start, Stop) of
eq ->
once(fun() -> Start end);
gt ->
unfold(Start, fun(Current) -> case Current < Stop of
false ->
{next, Current, Current - 1};
true ->
done
end end);
lt ->
unfold(Start, fun(Current@1) -> case Current@1 > Stop of
false ->
{next, Current@1, Current@1 + 1};
true ->
done
end end)
end.
-spec single(CHQ) -> iterator(CHQ).
single(Elem) ->
once(fun() -> Elem end).
-spec do_interleave(fun(() -> action(CHS)), fun(() -> action(CHS))) -> action(CHS).
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(CHW), iterator(CHW)) -> iterator(CHW).
interleave(Left, Right) ->
_pipe = fun() ->
do_interleave(erlang:element(2, Left), erlang:element(2, Right))
end,
{iterator, _pipe}.
-spec do_fold_until(
fun(() -> action(CIA)),
fun((CIC, CIA) -> gleam@list:continue_or_stop(CIC)),
CIC
) -> CIC.
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(CIE),
CIG,
fun((CIG, CIE) -> gleam@list:continue_or_stop(CIG))
) -> CIG.
fold_until(Iterator, Initial, F) ->
_pipe = erlang:element(2, Iterator),
do_fold_until(_pipe, F, Initial).
-spec do_try_fold(
fun(() -> action(CII)),
fun((CIK, CII) -> {ok, CIK} | {error, CIL}),
CIK
) -> {ok, CIK} | {error, CIL}.
do_try_fold(Continuation, F, Accumulator) ->
case Continuation() of
stop ->
{ok, Accumulator};
{continue, Elem, Next} ->
gleam@result:'try'(
F(Accumulator, Elem),
fun(Accumulator@1) -> do_try_fold(Next, F, Accumulator@1) end
)
end.
-spec try_fold(iterator(CIQ), CIS, fun((CIS, CIQ) -> {ok, CIS} | {error, CIT})) -> {ok,
CIS} |
{error, CIT}.
try_fold(Iterator, Initial, F) ->
_pipe = erlang:element(2, Iterator),
do_try_fold(_pipe, F, Initial).
-spec first(iterator(CIY)) -> {ok, CIY} | {error, nil}.
first(Iterator) ->
case (erlang:element(2, Iterator))() of
stop ->
{error, nil};
{continue, E, _} ->
{ok, E}
end.
-spec at(iterator(CJC), integer()) -> {ok, CJC} | {error, nil}.
at(Iterator, Index) ->
_pipe = Iterator,
_pipe@1 = drop(_pipe, Index),
first(_pipe@1).
-spec do_length(fun(() -> action(any())), integer()) -> integer().
do_length(Continuation, Length) ->
case Continuation() of
stop ->
Length;
{continue, _, Next} ->
do_length(Next, Length + 1)
end.
-spec length(iterator(any())) -> integer().
length(Iterator) ->
_pipe = erlang:element(2, Iterator),
do_length(_pipe, 0).
-spec each(iterator(CJK), fun((CJK) -> any())) -> nil.
each(Iterator, F) ->
_pipe = Iterator,
_pipe@1 = map(_pipe, F),
run(_pipe@1).
-spec yield(CJN, fun(() -> iterator(CJN))) -> iterator(CJN).
yield(Element, Next) ->
{iterator,
fun() ->
{continue, Element, fun() -> (erlang:element(2, Next()))() end}
end}.