A programming convention

Thomas Arts <>
Tue Jun 11 11:34:57 CEST 2002


Why dthen not introducing a higher order function. After
all, we use a functional language ;0)

>    lookupQ(Key, Dict) ->
>       case (catch lookup(Key, Dict)) of
>          {'EXIT', Why} ->
>             {error, Why};
>          Other ->
>             {ok, Other}
>       end.

In the Erlang style:

query(F,Args) ->
  case (catch F(Args)) of
       {'EXIT',Why}
          {error,Why};
       Other ->
          {ok,Other}
  end.

in case of lookupQ(Key,Dict) you write:

query(lookup,[Key,Dict])

This makes is very easy to find the places in you code
where you use this design pattern. The Q as addition is
more add-hoc and not as clear from a program analysis
point of view.

Alternatively, in a more curried style, one could define
several functions and write:

query(lookup)(Key,Dict)

For this one needs about ten functions of the form:

query(F) -> 
  fun(X1) ->
     case (catch F(X1)) of
       {'EXIT',Why}
          {error,Why};
       Other ->
          {ok,Other}
     end
  end.


query(F) -> 
  fun(X1,X2) ->
     case (catch F(X1,X2)) of
       {'EXIT',Why}
          {error,Why};
       Other ->
          {ok,Other}
     end
  end. 

etcetera

/Thomas

---
Thomas Arts
Ericsson



More information about the erlang-questions mailing list