Tip of the day

Richard Carlsson richardc@REDACTED
Thu Aug 12 19:05:25 CEST 2004


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