[erlang-questions] Avoiding case nesting
mayamatakeshi
mayamatakeshi@REDACTED
Mon Sep 28 13:50:21 CEST 2009
On Mon, Sep 28, 2009 at 7:38 PM, Gleb Peregud <gleber.p@REDACTED> wrote:
> On Mon, Sep 28, 2009 at 12:23, mayamatakeshi <mayamatakeshi@REDACTED>
> wrote:
> > So, is there a way to avoid this cascading of case expressions?
>
> One could do this:
>
> Prefixes = [{"prefix1", fun do_this/1},
> {"prefix2", fun do_that/1},
> {"prefix3", fun do_something/1}],
> catch lists:foldl(fun({Prefix, Fun}, Tail) ->
> case checkprefix(Prefix, Tail) of
> {ok, NewTail} ->
> NewTail;
> _ ->
> throw(done)
> end
> end, String, Prefixes).
>
This works.
I'm a little concerned about the run time cost of throwing exceptions to
force early processing termination (this will be called a lot) and this
really seems somewhat abusing, but I will not discard this without checking
performance.
Thanks.
More information about the erlang-questions
mailing list