[erlang-questions] How do I elegantly check many conditions?

Adam Kelly cthulahoops@REDACTED
Fri Mar 20 15:33:33 CET 2009


2009/3/20 ryeguy <ryeguy1@REDACTED>:
> Now I don't want to have a 5 level deep if or case statement, but what
> other options do I have? Splitting it into separate functions sounds
> like a good idea, but then I just have to check the return value of
> the functions in some sort of conditional and it's back to the
> original problem.

I've added this function to my libraries:

run_checks([]) -> ok;
run_checks([H|T]) when is_function(H, 0) ->
	case H() of
		ok    -> run_checks(T);
		Error -> Error
	end.

at which point the problem is simply

case run_checks([
              fun() ->check_email(Email) end,
              fun() ->check_username(Username) end,
              ... ]) of
      ok -> ...;
      {error, Reason} -> ...
end.

Adam.



More information about the erlang-questions mailing list