Pattern matching vs. ETS lookups

Ulf Wiger <>
Mon Feb 16 23:23:28 CET 2004


On Mon, 16 Feb 2004 17:07:57 -0500, Vance Shipley <> 
wrote:

> In your handler lookup up the module and function
> from the state data.  Here are a couple ways to do it
> using a gen_server:
>
> Simple list:
>
>      init(Path) ->
>           file:consult(Path).  %% conviently returns {ok, State}
>
>      handle_info(<<FunctionID:16, Body/binary>>, State) ->
>           {M, F} = lists:keysearch(FunctionID, 2, State),
>           M:F(Body).
>
>
> General balanced trees:
>
>      init(Path) ->
>           {ok, List} = file:consult(Path),
>           {ok, gb_trees:from_ordict(List)}.
>
>      handle_info(<<FunctionID:16, Body/binary>>, State) ->
>           {M, F} = gb_trees:get(FunctionID, State},
>           M:F(Body).

Or simply a tuple of tuples:

f(<<FunID_MSB/8, FunID_LSB/8, Body/binary>>, Matrix) ->
    BodyParsingFunction =
      element(FunID_LSB, element(FunID_MSB, Matrix)),
    BodyParsingFunction(Body).

Hard to beat for speed, I think.

/Uffe
-- 
Ulf Wiger




More information about the erlang-questions mailing list