[erlang-questions] Improving Erlang!

Hynek Vychodil hynek@REDACTED
Wed Dec 1 23:30:54 CET 2010


2010/12/1 Gilberio Carmenates García <co7eb@REDACTED>:
> Hi all!
>
>
>
> Things like this could be nice in Erlang
>
>
>
> start(Type, Args)->
>
> ChkArg = fun(Arg)->
>
> case lists:keyfind(Arg, 1, StartArgs) of
>
> {Arg, true}->
>
> true;
>
> _->
>
> false
>
> end
>
> end,
>
>                if
>
>                               ChkArg(install)->
>
>                                               ?MODULE:install();
>
>                               ChkArg(debug)->
>
>                                               appmon:start();
>
>                               true->
>
>                                               %% Starts the application.
>
>
> evo_debug_module:print([{debug, true}], "~n~nApplication: \"~p\" is
> starting...~n", [?MODULE]),
>
>
> evo_debug_module:print([{debug, true}], "~nType: ~p   StartArgs: ~p~n",
> [Type, StartArgs]),
>
>                                               R =
> evo_main_supervisor:start_link(StartArgs),
>
>
> evo_debug_module:print([{debug, true}], "~nApplication: \"~p\" is ready.~n",
> [?MODULE]),
>
>                                               R
>
>                end.

If I understand what your code do (and I'm not sure that it is
supposed to do) I would write it in this way:

start(Type, Args)->
   ChkArg = fun(Arg)-> not lists:member({Arg, true}, Args) end,
   case lists:dropwhile(ChkArg, [install, debug]) of
        [install|_] -> ?MODULE:install();
        [debug|_] -> appmon:start();
        [] -> %% Starts the application.
            evo_debug_module:print([{debug, true}], "~n~nApplication:
\"~p\" is starting...~n", [?MODULE]),
            evo_debug_module:print([{debug, true}], "~nType: ~p
StartArgs: ~p~n", [Type, StartArgs]),
            R = evo_main_supervisor:start_link(StartArgs),
            evo_debug_module:print([{debug, true}], "~nApplication:
\"~p\" is ready.~n", [?MODULE]),
            R
   end.

I think that lists:dropwhile(fun(X) -> not lists:member(X, Args) end,
ToDo) is pretty idiomatic way for this purpose.

>
> But that is impossible since the ‘if’ clause only work with guards, so since
> we can not define our own guards that and many other limitations can come
> up.
>
>
>
> Other nice example of the Erlang limitations could be
>
>
>
> get(Name)->
>
> MatchHead = #t_char{name='$1', _='_'},
>
>                Guard = {'user_define_compare/2', '$1', Name},       %%
> instead of {'==', '$1', Name}
>
>                Result = '$_',
>
>                mnesia:dirty_select(t_char,[{MatchHead, [Guard],
> [Result]}]).
>
>
>
> Or
>
>
>
> … when are_approximately_equals(Name, Name2) ->…             %% if I want to
> Search a name in a list, that I don’t know exactly what name is… could be in
> the list Iván Carmenates García and I search by Ivan Garcia… if the names
> are in the database then using just mnesia:select/3 or match I can do that
> in just almost one step, since I used my own user defined compare that
> validates the approximately match between the real full name and the part of
> the name I introduced.
>
>
>
>
>
>
>
> Regards,
>
>
>
>
>
> Ivan.
>
>
>
> =======================================================================
> Este mensaje ha sido enviado mediante el servicio de correo electrónico que ofrece la Federación de Radioaficionados de Cuba a sus miembros para respaldar el cumplimiento de los objetivos de la organización y su política informativa. La persona que envía este correo asume el compromiso de  usar el servicio a tales fines y cumplir con las regulaciones establecidas.
>



-- 
--Hynek (Pichi) Vychodil

Analyze your data in minutes. Share your insights instantly. Thrill
your boss.  Be a data hero!
Try GoodData now for free: www.gooddata.com


More information about the erlang-questions mailing list