Current section

Files

Jump to
gleam_stdlib gen src gleam@iterator.erl
Raw

gen/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]).
-export_type([action/1, iterator/1, step/2, chunk/2, sized_chunk/1]).
-type action(BZB) :: stop | {continue, BZB, fun(() -> action(BZB))}.
-opaque iterator(BZC) :: {iterator, fun(() -> action(BZC))}.
-type step(BZD, BZE) :: {next, BZD, BZE} | done.
-type chunk(BZF, BZG) :: {another_by,
list(BZF),
BZG,
BZF,
fun(() -> action(BZF))} |
{last_by, list(BZF)}.
-type sized_chunk(BZH) :: {another, list(BZH), fun(() -> action(BZH))} |
{last, list(BZH)} |
none.
-spec stop() -> action(any()).
stop() ->
stop.
-spec do_unfold(BZM, fun((BZM) -> step(BZN, BZM))) -> fun(() -> action(BZN)).
do_unfold(Initial, F) ->
fun() -> case F(Initial) of
{next, X, Acc} ->
{continue, X, do_unfold(Acc, F)};
done ->
stop
end end.
-spec unfold(BZR, fun((BZR) -> step(BZS, BZR))) -> iterator(BZS).
unfold(Initial, F) ->
{iterator, do_unfold(Initial, F)}.
-spec repeatedly(fun(() -> BZW)) -> iterator(BZW).
repeatedly(F) ->
unfold(nil, fun(_) -> {next, F(), nil} end).
-spec repeat(BZY) -> iterator(BZY).
repeat(X) ->
repeatedly(fun() -> X end).
-spec from_list(list(CAA)) -> iterator(CAA).
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(CAD)), fun((CAD, CAF) -> CAF), CAF) -> CAF.
do_fold(Continuation, F, Accumulator) ->
case Continuation() of
{continue, Elem, Next} ->
do_fold(Next, F, F(Elem, Accumulator));
stop ->
Accumulator
end.
-spec fold(iterator(CAG), CAI, fun((CAG, CAI) -> CAI)) -> CAI.
fold(Iterator, Initial, F) ->
do_fold(erlang:element(2, Iterator), F, Initial).
-spec run(iterator(any())) -> nil.
run(Iterator) ->
fold(Iterator, nil, fun(_, _) -> nil end).
-spec to_list(iterator(CAL)) -> list(CAL).
to_list(Iterator) ->
gleam@list:reverse(fold(Iterator, [], fun(E, Acc) -> [E | Acc] end)).
-spec step(iterator(CAO)) -> step(CAO, iterator(CAO)).
step(Iterator) ->
case (erlang:element(2, Iterator))() of
stop ->
done;
{continue, E, A} ->
{next, E, {iterator, A}}
end.
-spec do_take(fun(() -> action(CAT)), integer()) -> fun(() -> action(CAT)).
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(CAW), integer()) -> iterator(CAW).
take(Iterator, Desired) ->
{iterator, do_take(erlang:element(2, Iterator), Desired)}.
-spec do_drop(fun(() -> action(CAZ)), integer()) -> action(CAZ).
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(CBC), integer()) -> iterator(CBC).
drop(Iterator, Desired) ->
{iterator, fun() -> do_drop(erlang:element(2, Iterator), Desired) end}.
-spec do_map(fun(() -> action(CBF)), fun((CBF) -> CBH)) -> fun(() -> action(CBH)).
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(CBJ), fun((CBJ) -> CBL)) -> iterator(CBL).
map(Iterator, F) ->
{iterator, do_map(erlang:element(2, Iterator), F)}.
-spec do_append(fun(() -> action(CBN)), fun(() -> action(CBN))) -> action(CBN).
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(CBR), iterator(CBR)) -> iterator(CBR).
append(First, Second) ->
{iterator,
fun() -> do_append(erlang:element(2, First), erlang:element(2, Second)) end}.
-spec do_flatten(fun(() -> action(iterator(CBV)))) -> action(CBV).
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(CBZ))) -> iterator(CBZ).
flatten(Iterator) ->
{iterator, fun() -> do_flatten(erlang:element(2, Iterator)) end}.
-spec flat_map(iterator(CCD), fun((CCD) -> iterator(CCF))) -> iterator(CCF).
flat_map(Iterator, F) ->
flatten(map(Iterator, F)).
-spec do_filter(fun(() -> action(CCI)), fun((CCI) -> boolean())) -> action(CCI).
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(CCL), fun((CCL) -> boolean())) -> iterator(CCL).
filter(Iterator, Predicate) ->
{iterator, fun() -> do_filter(erlang:element(2, Iterator), Predicate) end}.
-spec cycle(iterator(CCO)) -> iterator(CCO).
cycle(Iterator) ->
flatten(repeat(Iterator)).
-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(CCS)), fun((CCS) -> boolean())) -> {ok, CCS} |
{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(CCW), fun((CCW) -> boolean())) -> {ok, CCW} | {error, nil}.
find(Haystack, Is_desired) ->
do_find(erlang:element(2, Haystack), Is_desired).
-spec do_index(fun(() -> action(CDA)), integer()) -> fun(() -> action({integer(),
CDA})).
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(CDD)) -> iterator({integer(), CDD}).
index(Iterator) ->
{iterator, do_index(erlang:element(2, Iterator), 0)}.
-spec iterate(CDG, fun((CDG) -> CDG)) -> iterator(CDG).
iterate(Initial, F) ->
unfold(Initial, fun(Element) -> {next, Element, F(Element)} end).
-spec do_take_while(fun(() -> action(CDI)), fun((CDI) -> boolean())) -> fun(() -> action(CDI)).
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(CDL), fun((CDL) -> boolean())) -> iterator(CDL).
take_while(Iterator, Predicate) ->
{iterator, do_take_while(erlang:element(2, Iterator), Predicate)}.
-spec do_drop_while(fun(() -> action(CDO)), fun((CDO) -> boolean())) -> action(CDO).
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(CDR), fun((CDR) -> boolean())) -> iterator(CDR).
drop_while(Iterator, Predicate) ->
{iterator,
fun() -> do_drop_while(erlang:element(2, Iterator), Predicate) end}.
-spec do_scan(fun(() -> action(CDU)), fun((CDU, CDW) -> CDW), CDW) -> fun(() -> action(CDW)).
do_scan(Continuation, F, Accumulator) ->
fun() -> case Continuation() of
stop ->
stop;
{continue, El, Next} ->
Accumulated = F(El, Accumulator),
{continue, Accumulated, do_scan(Next, F, Accumulated)}
end end.
-spec scan(iterator(CDY), CEA, fun((CDY, CEA) -> CEA)) -> iterator(CEA).
scan(Iterator, Initial, F) ->
{iterator, do_scan(erlang:element(2, Iterator), F, Initial)}.
-spec do_zip(fun(() -> action(CEC)), fun(() -> action(CEE))) -> fun(() -> action({CEC,
CEE})).
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(CEH), iterator(CEJ)) -> iterator({CEH, CEJ}).
zip(Left, Right) ->
{iterator, do_zip(erlang:element(2, Left), erlang:element(2, Right))}.
-spec next_chunk(fun(() -> action(CEP)), fun((CEP) -> CER), CER, list(CEP)) -> chunk(CEP, CER).
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(CEV)), fun((CEV) -> CEX), CEX, CEV) -> action(list(CEV)).
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(CFA), fun((CFA) -> any())) -> iterator(list(CFA)).
chunk(Iterator, F) ->
{iterator, fun() -> case (erlang:element(2, Iterator))() of
stop ->
stop;
{continue, E, Next} ->
do_chunk(Next, F, F(E), E)
end end}.
-spec next_sized_chunk(fun(() -> action(CFI)), integer(), list(CFI)) -> sized_chunk(CFI).
next_sized_chunk(Continuation, Left, Current_chunk) ->
case Continuation() of
stop ->
case Current_chunk of
[] ->
none;
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(CFM)), integer()) -> fun(() -> action(list(CFM))).
do_sized_chunk(Continuation, Count) ->
fun() -> case next_sized_chunk(Continuation, Count, []) of
none ->
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(CFQ), integer()) -> iterator(list(CFQ)).
sized_chunk(Iterator, Count) ->
{iterator, do_sized_chunk(erlang:element(2, Iterator), Count)}.
-spec do_intersperse(fun(() -> action(CFU)), CFU) -> action(CFU).
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(CFX), CFX) -> iterator(CFX).
intersperse(Iterator, Elem) ->
{iterator, fun() -> case (erlang:element(2, Iterator))() of
stop ->
stop;
{continue, E, Next} ->
{continue, E, fun() -> do_intersperse(Next, Elem) end}
end end}.
-spec do_any(fun(() -> action(CGA)), fun((CGA) -> 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(CGC), fun((CGC) -> boolean())) -> boolean().
any(Iterator, Predicate) ->
do_any(erlang:element(2, Iterator), Predicate).
-spec do_all(fun(() -> action(CGE)), fun((CGE) -> 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(CGG), fun((CGG) -> boolean())) -> boolean().
all(Iterator, Predicate) ->
do_all(erlang:element(2, Iterator), Predicate).
-spec update_group_with(CGI) -> fun(({ok, list(CGI)} | {error, nil}) -> list(CGI)).
update_group_with(El) ->
fun(Maybe_group) -> case Maybe_group of
{ok, Group} ->
[El | Group];
{error, nil} ->
[El]
end end.
-spec group_updater(fun((CGN) -> CGO)) -> fun((CGN, gleam@map:map_(CGO, list(CGN))) -> gleam@map:map_(CGO, list(CGN))).
group_updater(F) ->
fun(Elem, Groups) ->
gleam@map:update(Groups, F(Elem), update_group_with(Elem))
end.
-spec group(iterator(CGV), fun((CGV) -> CGX)) -> gleam@map:map_(CGX, list(CGV)).
group(Iterator, Key) ->
gleam@map:map_values(
fold(Iterator, gleam@map:new(), group_updater(Key)),
fun(_, Group) -> gleam@list:reverse(Group) end
).
-spec reduce(iterator(CHB), fun((CHB, CHB) -> CHB)) -> {ok, CHB} | {error, nil}.
reduce(Iterator, F) ->
case (erlang:element(2, Iterator))() of
stop ->
{error, nil};
{continue, E, Next} ->
{ok, do_fold(Next, F, E)}
end.
-spec last(iterator(CHF)) -> {ok, CHF} | {error, nil}.
last(Iterator) ->
reduce(Iterator, fun(Elem, _) -> Elem end).
-spec empty() -> iterator(any()).
empty() ->
{iterator, fun stop/0}.
-spec once(fun(() -> CHL)) -> iterator(CHL).
once(F) ->
{iterator, fun() -> {continue, F(), fun stop/0} end}.
-spec single(CHN) -> iterator(CHN).
single(Elem) ->
once(fun() -> Elem end).
-spec do_interleave(fun(() -> action(CHP)), fun(() -> action(CHP))) -> action(CHP).
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(CHT), iterator(CHT)) -> iterator(CHT).
interleave(Left, Right) ->
{iterator,
fun() ->
do_interleave(erlang:element(2, Left), erlang:element(2, Right))
end}.