Tip of the day

Joe Armstrong joe@REDACTED
Fri Aug 13 16:41:02 CEST 2004


Brilliant


    How come we miss the simplest of ideas?

    I have hundreds (thousands?) of lines of

    {ok, XX} =foo(...)

   ok(foo(..)) is  *much clearer* -  and it saves two  characters (well
one really, but I always put a space after the comma.

    I'm going to have to change a lot of my code now :-)

    /Joe





On Thu, 12 Aug 2004, Richard Carlsson wrote:

>
> Put the following in your user_default.erl
>
> ----cut here----
> -module(user_default).
> -compile(export_all).
>
> ok(T) when tuple(T), size(T) >= 2, element(1, T) == ok -> element(2, T);
> ok(E) -> erlang:fault({not_ok, E}).
>
> ok2(T) when tuple(T), size(T) >= 3, element(1, T) == ok -> element(3, T);
> ok2(E) -> erlang:fault({not_ok, E}).
>
> ok3(T) when tuple(T), size(T) >= 4, element(1, T) == ok -> element(4, T);
> ok3(E) -> erlang:fault({not_ok, E}).
> ----cut here----
>
> and add the lines
>
>    code:add_path("...path-to-my-user-default/ebin").
>    code:ensure_loaded(user_default).
>
> to your ~/.erlang file.
>
> Then you can use these functions in the Erlang shell, like this:
>
>  Eshell V5.4  (abort with ^G)
>  1> File = ok(file:open("myfile",[read])).
>  <0.40.0>
>  2> file:close(File).
>
> or
>
>  3> ok(erl_parse:parse_term(ok(erl_scan:string("[1,2,3].")))).
>  [1,2,3]
>  4>
>
> No more matching on {ok, ...}. (Works for tuples of any size > 1.)
>
> 	/Richard
>
>
>
> Richard Carlsson (richardc@REDACTED)   (This space intentionally left blank.)
> E-mail: Richard.Carlsson@REDACTED	WWW: http://user.it.uu.se/~richardc/
> "Having users is like optimization: the wise course is to delay it."
>   -- Paul Graham
>



More information about the erlang-questions mailing list