How to handle nested case

Michael Truog mjtruog@REDACTED
Sat Sep 18 23:23:37 CEST 2021


I have used (from 
https://github.com/CloudI/CloudI/blob/a43715b71facafc9a8ad7c7fe55c7383773c22df/src/lib/cloudi_core/src/cloudi_core_i_configuration.erl#L6022-L6039):
-type eval_value() :: number() | atom() | list(). -spec eval(L :: 
list({eval_value(), fun((eval_value()) -> {ok, any()} | {error, 
any()})})) -> tuple(). eval(L) -> eval(L, []). eval([], Output) -> 
erlang:list_to_tuple([ok | lists:reverse(Output)]); eval([{Value, F} | 
L], Output) when is_number(Value) orelse is_atom(Value) orelse 
is_list(Value) -> case F(Value) of {ok, ValueNew} -> eval(L, [ValueNew | 
Output]); {error, _} = Error -> Error end.

However, the eval function above is assuming each function in the 
2-tuple list is taking a value it is paired with as input.  The return 
tuple has the 'ok' atom to show it is a success case.  For your 
situation, you wouldn't need an input value for your function values (so 
only a list of function values would be necessary).

Best Regards,
Michael

On 9/18/21 10:14 AM, Gian Lorenzo Meocci wrote:
> Hi,
> I have a function like this:
>
> get_nas_from_conf() ->
> caseget_radius_host() of
> {ok, Server} ->
> caseget_radius_port() of
> {ok, Port} ->
> caseget_radius_secret() of
> {ok, Secret} ->
> {Server, Port, Secret};
> E-> E
> end;
> E->
> E
> end;
> E->
> E
> end.
>
> Which is the best way to write this kind of function?
>
> I'd like to have a with operator like in Elixir, to rewrite my 
> function in this way:
>
> get_nas_from_conf() ->
> with {ok, Server} <- get_radius_host(),
> {ok, Port} = get_radius_port(),
> {ok, Secret} = get_radius_secret() ->
> {Server, Port, Secret};
> catch
> {error, _Reason} = E->
> E
> end.
>
> Any suggestion?
>
> -- 
> GL
> https://www.meocci.it <https://meox.github.io>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210918/0c549afc/attachment.htm>


More information about the erlang-questions mailing list