[erlang-questions] case and pattern matching

Bengt Kleberg <>
Tue Jul 10 12:40:38 CEST 2007


On 2007-07-10 12:08, Joel Reymont wrote:
> Folks,
> 
> What do you do when two clauses in your case statement need to share  
> the code? Do you create an anonymous function and reuse it?
> 
> I really wish there was a way to combine patterns but I can't seem to  
> find any. Consider the following bit of Mnesia code:
> 
> init_indecies(Tab, Storage, PosList) ->
>      case Storage of
> 	unknown ->
> 	    ignore;
> 	s3_copies ->
> 	    ignore;
> 	disc_only_copies ->
> 	    init_disc_index(Tab, PosList);
> 	ram_copies ->
> 	    make_ram_index(Tab, PosList);
> 	disc_copies ->
> 	    make_ram_index(Tab, PosList)
>      end.
> 
> What I would like to do is join unknown and s3_copies or maybe  
> ram_copies and disc_copies in a single pattern match like
> 
> uknown | s3_copies -> ...
> 
> Is this possible?

perhaps this is what you want:

case Storage of
unknown -> ignore;
%% ...
Copies when (Copies =:= ram_copies) orelse (Copies =:= disc_copies) -> 
make_ram_index(Tab, PosList)
end.


bengt
-- 
Those were the days...
    EPO guidelines 1978: "If the contribution to the known art resides
    solely in a computer program then the subject matter is not
    patentable in whatever manner it may be presented in the claims."



More information about the erlang-questions mailing list