-module(stream). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/stream.gleam"). -export([has_next/1, next/1, generate_until/3, each/2, generate/2, map/2, from_list/1, empty/0, copy/1, fold/3, to_list/1, until/2, filter/2, drop_while/2, drop/2, flatten/1, flat_map/2]). -export_type([message/1, state/1, stream/1]). -type message(FLT) :: {get_next, gleam@erlang@process:subject(gleam@option:option(FLT))} | {has_next, gleam@erlang@process:subject(boolean())}. -type state(FLU) :: finished | {un_initialized, fun(() -> FLU)} | {generating, FLU}. -opaque stream(FLV) :: {true_generator, gleam@erlang@process:subject(message(FLV)), gleam@erlang@process:pid_(), fun((state(FLV), message(FLV)) -> gleam@otp@actor:next(state(FLV), message(FLV))), fun(() -> FLV)} | empty_generator. -file("src/stream.gleam", 29). -spec save_call( gleam@erlang@process:subject(message(FLW)), fun((gleam@erlang@process:subject(FLZ)) -> message(FLW)) ) -> {ok, FLZ} | {error, nil}. save_call(Subject, Message) -> Reply_to = gleam@erlang@process:new_subject(), gleam@erlang@process:send(Subject, Message(Reply_to)), gleam@erlang@process:'receive'(Reply_to, 1000). -file("src/stream.gleam", 38). -spec try_if_alive( stream(FMD), FMF, fun((gleam@erlang@process:subject(message(FMD)), gleam@erlang@process:pid_()) -> {ok, FMF} | {error, nil}) ) -> FMF. try_if_alive(Generator, Fallback, If_alive) -> case Generator of empty_generator -> Fallback; {true_generator, _, Pid, _, _} -> gleam@bool:guard( begin _pipe = erlang:is_process_alive(Pid), gleam@bool:negate(_pipe) end, Fallback, fun() -> _pipe@1 = If_alive( erlang:element(2, Generator), erlang:element(3, Generator) ), gleam@result:unwrap(_pipe@1, Fallback) end ) end. -file("src/stream.gleam", 52). -spec has_next(stream(any())) -> boolean(). has_next(Generator) -> try_if_alive( Generator, false, fun(Subject, _) -> save_call(Subject, fun(Field@0) -> {has_next, Field@0} end) end ). -file("src/stream.gleam", 57). -spec next(stream(FMM)) -> gleam@option:option(FMM). next(Generator) -> try_if_alive( Generator, none, fun(Subject, _) -> save_call(Subject, fun(Field@0) -> {get_next, Field@0} end) end ). -file("src/stream.gleam", 62). -spec handle_message( fun((FMP) -> FMP), fun((FMP) -> boolean()), state(FMP), message(FMP) ) -> gleam@otp@actor:next(state(FMP), any()). handle_message(Generator, Until, State, Message) -> New_state = case {State, Message} of {finished, {get_next, Return_to}} -> gleam@erlang@process:send(Return_to, none), finished; {{generating, Cur}, {get_next, Return_to@1}} -> Next = Generator(Cur), gleam@erlang@process:send(Return_to@1, {some, Next}), case Until(Next) of true -> finished; false -> {generating, Next} end; {{un_initialized, Init}, {get_next, Return_to@2}} -> First = Init(), gleam@erlang@process:send(Return_to@2, {some, First}), case Until(First) of true -> finished; false -> {generating, First} end; {finished, {has_next, Return_to@3}} -> gleam@erlang@process:send(Return_to@3, false), finished; {{un_initialized, X}, {has_next, Return_to@4}} -> gleam@erlang@process:send(Return_to@4, true), {un_initialized, X}; {{generating, X@1}, {has_next, Return_to@5}} -> gleam@erlang@process:send(Return_to@5, true), {generating, X@1} end, case New_state of finished -> gleam@otp@actor:stop(); _ -> gleam@otp@actor:continue(New_state) end. -file("src/stream.gleam", 110). -spec create( fun(() -> FMT), fun((state(FMT), message(FMT)) -> gleam@otp@actor:next(state(FMT), message(FMT))) ) -> stream(FMT). create(Seed, Handle) -> My_actor@1 = case begin _pipe = gleam@otp@actor:new({un_initialized, Seed}), _pipe@1 = gleam@otp@actor:on_message(_pipe, Handle), gleam@otp@actor:start(_pipe@1) end of {ok, My_actor} -> My_actor; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"stream"/utf8>>, function => <<"create"/utf8>>, line => 114, value => _assert_fail, start => 2655, 'end' => 2766, pattern_start => 2666, pattern_end => 2678}) end, {true_generator, erlang:element(3, My_actor@1), erlang:element(2, My_actor@1), Handle, Seed}. -file("src/stream.gleam", 128). -spec id(FNB) -> fun(() -> FNB). id(X) -> fun() -> X end. -file("src/stream.gleam", 132). -spec generate_until(fun(() -> FNC), fun((FNC) -> FNC), fun((FNC) -> boolean())) -> stream(FNC). generate_until(Seed, Generate, Until) -> create( Seed, fun(State, Message) -> handle_message(Generate, Until, State, Message) end ). -file("src/stream.gleam", 141). -spec each(stream(FNE), fun((FNE) -> nil)) -> nil. each(From, Do) -> case next(From) of none -> nil; {some, Val} -> Do(Val), each(From, Do) end. -file("src/stream.gleam", 153). -spec generate(fun(() -> FNG), fun((FNG) -> FNG)) -> stream(FNG). generate(Seed, By) -> create( Seed, fun(State, Message) -> handle_message(By, fun(_) -> true end, State, Message) end ). -file("src/stream.gleam", 183). -spec assert_some(gleam@option:option(FNL)) -> FNL. assert_some(Item) -> Item@2 = case Item of {some, Item@1} -> Item@1; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"stream"/utf8>>, function => <<"assert_some"/utf8>>, line => 184, value => _assert_fail, start => 4043, 'end' => 4071, pattern_start => 4054, pattern_end => 4064}) end, Item@2. -file("src/stream.gleam", 188). -spec map(stream(FNN), fun((FNN) -> FNP)) -> stream(FNP). map(From, By) -> case has_next(From) of false -> empty_generator; true -> generate_until( begin _pipe = next(From), _pipe@1 = assert_some(_pipe), _pipe@2 = By(_pipe@1), id(_pipe@2) end, fun(_) -> _pipe@3 = next(From), _pipe@4 = assert_some(_pipe@3), By(_pipe@4) end, fun(_) -> _pipe@5 = has_next(From), gleam@bool:negate(_pipe@5) end ) end. -file("src/stream.gleam", 158). -spec from_list(list(FNI)) -> stream(FNI). from_list(Values) -> Gen = case Values of [] -> empty_generator; [First | Res] -> generate_until( id({Res, First}), fun(X) -> {Values@1, _} = X, {Next@1, Res@2} = case Values@1 of [Next | Res@1] -> {Next, Res@1}; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"stream"/utf8>>, function => <<"from_list"/utf8>>, line => 166, value => _assert_fail, start => 3739, 'end' => 3772, pattern_start => 3750, pattern_end => 3763}) end, {Res@2, Next@1} end, fun(X@1) -> {Values@2, _} = X@1, case Values@2 of [_ | _] -> false; _ -> true end end ) end, map( Gen, fun(_use0) -> {_, Cur} = _use0, Cur end ). -file("src/stream.gleam", 201). -spec empty() -> stream(any()). empty() -> empty_generator. -file("src/stream.gleam", 210). -spec copy(stream(FNW)) -> stream(FNW). copy(Original) -> case Original of empty_generator -> empty_generator; {true_generator, _, _, Handle, Seed} -> create(Seed, Handle) end. -file("src/stream.gleam", 248). -spec fold(stream(FOF), FOH, fun((FOH, FOF) -> FOH)) -> FOH. fold(Stream, Initial, Fun) -> _pipe = Stream, _pipe@1 = next(_pipe), _pipe@2 = gleam@option:map( _pipe@1, fun(_capture) -> Fun(Initial, _capture) end ), _pipe@3 = gleam@option:map( _pipe@2, fun(_capture@1) -> fold(Stream, _capture@1, Fun) end ), gleam@option:unwrap(_pipe@3, Initial). -file("src/stream.gleam", 260). -spec to_list(stream(FOI)) -> list(FOI). to_list(From) -> _pipe = From, _pipe@1 = fold(_pipe, [], fun gleam@list:prepend/2), lists:reverse(_pipe@1). -file("src/stream.gleam", 264). -spec map_unwrap(gleam@option:option(FOL), fun(() -> FON), fun((FOL) -> FON)) -> FON. map_unwrap(Option, Or, By) -> _pipe = Option, _pipe@1 = gleam@option:map(_pipe, By), gleam@option:lazy_unwrap(_pipe@1, Or). -file("src/stream.gleam", 205). -spec until(stream(FNT), fun((FNT) -> boolean())) -> stream(FNT). until(From, Until) -> map_unwrap( next(From), id(empty_generator), fun(First) -> generate_until(id(First), fun(_) -> _pipe = next(From), assert_some(_pipe) end, Until) end ). -file("src/stream.gleam", 219). -spec next_match(stream(FNZ), fun((FNZ) -> boolean())) -> gleam@option:option(FNZ). next_match(From, Predicate) -> map_unwrap(next(From), id(none), fun(X) -> case Predicate(X) of true -> {some, X}; false -> next_match(From, Predicate) end end). -file("src/stream.gleam", 227). -spec filter(stream(FOC), fun((FOC) -> boolean())) -> stream(FOC). filter(From, Predicate) -> Next_val = fun() -> next_match(From, Predicate) end, Intermediate = begin map_unwrap( Next_val(), id(empty_generator), fun(First) -> generate_until( fun() -> {First, Next_val()} end, fun(A) -> Cur@1 = case A of {_, {some, Cur}} -> Cur; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"stream"/utf8>>, function => <<"filter"/utf8>>, line => 234, value => _assert_fail, start => 5331, 'end' => 5361, pattern_start => 5342, pattern_end => 5357}) end, {Cur@1, Next_val()} end, fun(A@1) -> {_, Next} = A@1, gleam@option:is_none(Next) end ) end ) end, map( Intermediate, fun(_use0) -> {Cur@2, _} = _use0, Cur@2 end ). -file("src/stream.gleam", 270). -spec indexed(stream(FOO)) -> stream({FOO, integer()}). indexed(Stream) -> map_unwrap( next(Stream), fun empty/0, fun(First) -> generate_until( fun() -> {First, 0} end, fun(_use0) -> {_, Last_index} = _use0, Val@1 = case next(Stream) of {some, Val} -> Val; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"stream"/utf8>>, function => <<"indexed"/utf8>>, line => 275, value => _assert_fail, start => 6216, 'end' => 6251, pattern_start => 6227, pattern_end => 6236}) end, {Val@1, Last_index + 1} end, fun(_) -> _pipe = has_next(Stream), gleam@bool:negate(_pipe) end ) end ). -file("src/stream.gleam", 279). -spec drop_while(stream(FOR), fun((FOR) -> boolean())) -> stream(FOR). drop_while(Stream, Predicate) -> map_unwrap(next_match(Stream, fun(X) -> _pipe = X, _pipe@1 = Predicate(_pipe), gleam@bool:negate(_pipe@1) end), fun empty/0, fun(First) -> generate_until( id(First), fun(_) -> Val@1 = case next(Stream) of {some, Val} -> Val; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"stream"/utf8>>, function => <<"drop_while"/utf8>>, line => 290, value => _assert_fail, start => 6593, 'end' => 6628, pattern_start => 6604, pattern_end => 6613}) end, Val@1 end, fun(_) -> _pipe@2 = has_next(Stream), gleam@bool:negate(_pipe@2) end ) end). -file("src/stream.gleam", 294). -spec drop(stream(FOU), integer()) -> stream(FOU). drop(Stream, N) -> Intermediate = begin drop_while( indexed(Stream), fun(_use0) -> {_, Index} = _use0, Index < N end ) end, map( Intermediate, fun(_use0@1) -> {Val, _} = _use0@1, Val end ). -file("src/stream.gleam", 310). -spec flatten(stream(stream(integer()))) -> stream(integer()). flatten(Streams) -> Next_stream = fun() -> next_match(Streams, fun has_next/1) end, map_unwrap( Next_stream(), fun empty/0, fun(First_generator) -> Intermediate = generate_until( fun() -> First_val@1 = case next(First_generator) of {some, First_val} -> First_val; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"stream"/utf8>>, function => <<"flatten"/utf8>>, line => 317, value => _assert_fail, start => 7218, 'end' => 7268, pattern_start => 7229, pattern_end => 7244}) end, {First_val@1, First_generator, Next_stream()} end, fun(X) -> {_, Cur_stream, Next_maybe} = X, map_unwrap( next(Cur_stream), fun() -> Cur_stream@2 = case Next_maybe of {some, Cur_stream@1} -> Cur_stream@1; _assert_fail@1 -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"stream"/utf8>>, function => <<"flatten"/utf8>>, line => 326, value => _assert_fail@1, start => 7509, 'end' => 7549, pattern_start => 7520, pattern_end => 7536}) end, Val@1 = case next(Cur_stream@2) of {some, Val} -> Val; _assert_fail@2 -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"stream"/utf8>>, function => <<"flatten"/utf8>>, line => 327, value => _assert_fail@2, start => 7558, 'end' => 7597, pattern_start => 7569, pattern_end => 7578}) end, {Val@1, Cur_stream@2, Next_stream()} end, fun(Val@2) -> {Val@2, Cur_stream, Next_maybe} end ) end, fun(X@1) -> {_, Cur_stream@3, Next_maybe@1} = X@1, Has_some = has_next(Cur_stream@3) orelse begin _pipe = Next_maybe@1, _pipe@1 = gleam@option:map(_pipe, fun has_next/1), gleam@option:unwrap(_pipe@1, false) end, _pipe@2 = Has_some, gleam@bool:negate(_pipe@2) end ), map( Intermediate, fun(_use0) -> {Val@3, _, _} = _use0, Val@3 end ) end ). -file("src/stream.gleam", 303). -spec flat_map(stream(integer()), fun((integer()) -> stream(integer()))) -> stream(integer()). flat_map(Stream, Fun) -> _pipe = Stream, _pipe@1 = map(_pipe, Fun), flatten(_pipe@1).