small erlang question.

Richard Carlsson richardc@REDACTED
Fri Jan 16 13:39:32 CET 2004


On Fri, 16 Jan 2004, WILLIAMS Dominic wrote:

> But here's my favourite:
>
> Actions = [{Str1,fun(Txt) -> ... end},
>            {Str2,fun(Txt) -> ... end},
>            ...],
> [Fun(Text) || {Str,Fun} <- Actions, string:str(Text,Str)>0].

This is the nicest approach, I think, but the above would check
all the strings even if the first one matches.
You could use a library function in 'lists' instead:

 Cases = [{Str1, fun handle1/1},
          {Str2, fun handle2/1},
          ...],
 case lists:dropwhile(fun({S,_})-> string:str(Text,S)==0 end, Cases) of
   [{S,F}|_] -> F(Text);
   [] -> exit(not_found)
 end

	/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