Current section
Files
Jump to
Current section
Files
src/squirrel@internal@eval_extra.erl
-module(squirrel@internal@eval_extra).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([run_all/2, try_fold/3, try_map/2, try_index_map/2]).
-spec run_all(list(eval:eval(SLF, SLG, SLH)), SLH) -> list({ok, SLF} |
{error, SLG}).
run_all(List, Context) ->
Acc = {[], Context},
{Results@1, _} = (gleam@list:fold(
List,
Acc,
fun(_use0, Script) ->
{Results, Context@1} = _use0,
{Context@2, Result} = eval:step(Script, Context@1),
{[Result | Results], Context@2}
end
)),
Results@1.
-spec try_fold(list(SMD), SMF, fun((SMF, SMD) -> eval:eval(SMF, SML, SMM))) -> eval:eval(SMF, SML, SMM).
try_fold(List, Acc, Fun) ->
case List of
[] ->
eval:return(Acc);
[First | Rest] ->
eval:'try'(
Fun(Acc, First),
fun(Acc@1) -> try_fold(Rest, Acc@1, Fun) end
)
end.
-spec try_map(list(SKR), fun((SKR) -> eval:eval(SKT, SOV, SOW))) -> eval:eval(list(SKT), SOV, SOW).
try_map(List, Fun) ->
_pipe = try_fold(
List,
[],
fun(Acc, Item) ->
eval:'try'(
Fun(Item),
fun(Mapped_item) -> eval:return([Mapped_item | Acc]) end
)
end
),
eval:map(_pipe, fun lists:reverse/1).
-spec do_try_index_fold(
integer(),
list(SND),
SNF,
fun((SNF, SND, integer()) -> eval:eval(SNF, SNL, SNM))
) -> eval:eval(SNF, SNL, SNM).
do_try_index_fold(Index, List, Acc, Fun) ->
case List of
[] ->
eval:return(Acc);
[First | Rest] ->
eval:'try'(
Fun(Acc, First, Index),
fun(Acc@1) -> do_try_index_fold(Index + 1, Rest, Acc@1, Fun) end
)
end.
-spec try_index_fold(
list(SMQ),
SMS,
fun((SMS, SMQ, integer()) -> eval:eval(SMS, SMT, SMU))
) -> eval:eval(SMS, SMT, SMU).
try_index_fold(List, Acc, Fun) ->
do_try_index_fold(0, List, Acc, Fun).
-spec try_index_map(
list(SLP),
fun((SLP, integer()) -> eval:eval(SLR, SQE, SQF))
) -> eval:eval(list(SLR), SQE, SQF).
try_index_map(List, Fun) ->
_pipe = try_index_fold(
List,
[],
fun(Acc, Item, I) ->
eval:'try'(
Fun(Item, I),
fun(Mapped_item) -> eval:return([Mapped_item | Acc]) end
)
end
),
eval:map(_pipe, fun lists:reverse/1).