Current section
Files
Jump to
Current section
Files
src/ior.erl
-module(ior).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([fold/4, bimap/3, map/2, left_map/2, flat_map/3, put_left/2, put_right/2, is_left/1, is_right/1, is_both/1, get_left/1, get_right/1, combine/4, only_left/1, only_right/1, only_both/1, as_options/1, swap/1, exists/2, for_all/2, get_or_else/2]).
-export_type([ior/2]).
-type ior(FJS, FJT) :: {left, FJS} | {right, FJT} | {both, FJS, FJT}.
-spec fold(
ior(FJU, FJV),
fun((FJU) -> FJY),
fun((FJV) -> FJY),
fun((FJU, FJV) -> FJY)
) -> FJY.
fold(Ior, Case_left, Case_right, Case_both) ->
case Ior of
{left, Val} ->
Case_left(Val);
{right, Val@1} ->
Case_right(Val@1);
{both, Left, Right} ->
Case_both(Left, Right)
end.
-spec bimap(ior(FJZ, FKA), fun((FJZ) -> FKD), fun((FKA) -> FKE)) -> ior(FKD, FKE).
bimap(Ior, Left_map, Right_map) ->
case Ior of
{left, Val} ->
{left, Left_map(Val)};
{right, Val@1} ->
{right, Right_map(Val@1)};
{both, Left, Right} ->
{both, Left_map(Left), Right_map(Right)}
end.
-spec map(ior(FKH, FKI), fun((FKI) -> FKL)) -> ior(FKH, FKL).
map(Ior, F) ->
case Ior of
{left, Val} ->
{left, Val};
{right, Val@1} ->
{right, F(Val@1)};
{both, Left, Right} ->
{both, Left, F(Right)}
end.
-spec left_map(ior(FKO, FKP), fun((FKO) -> FKS)) -> ior(FKS, FKP).
left_map(Ior, F) ->
case Ior of
{left, Val} ->
{left, F(Val)};
{right, Val@1} ->
{right, Val@1};
{both, Left, Right} ->
{both, F(Left), Right}
end.
-spec flat_map(
ior(FKV, FKW),
fun((FKV, FKV) -> FKV),
fun((FKW) -> ior(FKV, FKZ))
) -> ior(FKV, FKZ).
flat_map(Ior, Combine, F) ->
case Ior of
{left, Val} ->
{left, Val};
{right, Val@1} ->
F(Val@1);
{both, Left, Right} ->
_pipe = F(Right),
left_map(_pipe, fun(_capture) -> Combine(Left, _capture) end)
end.
-spec put_left(ior(any(), FLN), FLQ) -> ior(FLQ, FLN).
put_left(Ior, Left) ->
case Ior of
{left, _} ->
{left, Left};
{right, Val} ->
{both, Left, Val};
{both, _, Right} ->
{both, Left, Right}
end.
-spec put_right(ior(FLT, any()), FLX) -> ior(FLT, FLX).
put_right(Ior, Right) ->
case Ior of
{left, Value} ->
{both, Value, Right};
{right, _} ->
{right, Right};
{both, Left, _} ->
{both, Left, Right}
end.
-spec is_left(ior(any(), any())) -> boolean().
is_left(Ior) ->
case Ior of
{left, _} ->
true;
_ ->
false
end.
-spec is_right(ior(any(), any())) -> boolean().
is_right(Ior) ->
case Ior of
{right, _} ->
true;
_ ->
false
end.
-spec is_both(ior(any(), any())) -> boolean().
is_both(Ior) ->
case Ior of
{both, _, _} ->
true;
_ ->
false
end.
-spec get_left(ior(FMM, any())) -> gleam@option:option(FMM).
get_left(Ior) ->
case Ior of
{left, Val} ->
{some, Val};
{right, _} ->
none;
{both, Left, _} ->
{some, Left}
end.
-spec get_right(ior(any(), FMS)) -> gleam@option:option(FMS).
get_right(Ior) ->
case Ior of
{left, _} ->
none;
{right, Val} ->
{some, Val};
{both, _, Right} ->
{some, Right}
end.
-spec combine(
ior(FLE, FLF),
ior(FLE, FLF),
fun((FLE, FLE) -> FLE),
fun((FLF, FLF) -> FLF)
) -> ior(FLE, FLF).
combine(Ior, Ior2, Combine_left, Combine_right) ->
Left_map = case begin
_pipe = Ior2,
get_left(_pipe)
end of
{some, Val} ->
fun(_capture) -> Combine_left(_capture, Val) end;
none ->
fun(Val@1) -> Val@1 end
end,
Right_map = case begin
_pipe@1 = Ior2,
get_right(_pipe@1)
end of
{some, Val@2} ->
fun(_capture@1) -> Combine_right(_capture@1, Val@2) end;
none ->
fun(Val@3) -> Val@3 end
end,
_pipe@2 = Ior,
bimap(_pipe@2, Left_map, Right_map).
-spec only_left(ior(FMW, any())) -> gleam@option:option(FMW).
only_left(Ior) ->
case Ior of
{left, Val} ->
{some, Val};
_ ->
none
end.
-spec only_right(ior(any(), FNC)) -> gleam@option:option(FNC).
only_right(Ior) ->
case Ior of
{right, Val} ->
{some, Val};
_ ->
none
end.
-spec only_both(ior(FNG, FNH)) -> gleam@option:option({FNG, FNH}).
only_both(Ior) ->
case Ior of
{both, Left, Right} ->
{some, {Left, Right}};
_ ->
none
end.
-spec as_options(ior(FNL, FNM)) -> {gleam@option:option(FNL),
gleam@option:option(FNM)}.
as_options(Ior) ->
case Ior of
{left, Val} ->
{{some, Val}, none};
{right, Val@1} ->
{none, {some, Val@1}};
{both, Left, Right} ->
{{some, Left}, {some, Right}}
end.
-spec swap(ior(FNR, FNS)) -> ior(FNS, FNR).
swap(Ior) ->
case Ior of
{left, Val} ->
{right, Val};
{right, Val@1} ->
{left, Val@1};
{both, Left, Right} ->
{both, Right, Left}
end.
-spec exists(ior(any(), FNY), fun((FNY) -> boolean())) -> boolean().
exists(Ior, Predicate) ->
case begin
_pipe = Ior,
get_right(_pipe)
end of
{some, Val} ->
Predicate(Val);
none ->
false
end.
-spec for_all(ior(any(), FOC), fun((FOC) -> boolean())) -> boolean().
for_all(Ior, Predicate) ->
case begin
_pipe = Ior,
get_right(_pipe)
end of
{some, Val} ->
Predicate(Val);
none ->
true
end.
-spec get_or_else(ior(any(), FOG), FOG) -> FOG.
get_or_else(Ior, Default) ->
case begin
_pipe = Ior,
get_right(_pipe)
end of
{some, Val} ->
Val;
none ->
Default
end.