[erlang-questions] To name a function

David Mercer dmercer@REDACTED
Thu Dec 16 16:06:46 CET 2010


On Thursday, December 16, 2010, Attila Rajmund Nohl wrote:

> 2010/12/16, Morten Krogh <mk@REDACTED>:
> > As soon as you have seen both an element that satisfies the condition
> > and one that doesn't, you can return "some", making a full list
> > traversal overkill for many inputs.
> 
> Now that's a good idea:
> 
> has_property(Fun, [E | Rest]) ->
>     has_property(Fun, Fun(E), Rest).
> 
> has_property(_Fun, Prev, []) ->
>     case Prev of
> 	true -> all;
> 	false -> none
>     end;
> 
> has_property(Fun, Prev, [E | Rest]) ->
>     Curr = Fun(E),
>     case Curr of
> 	Prev -> has_property(Fun, Prev, Rest);
> 	_ -> some
>     end.

I was thinking a throw would be appropriate for the early-out:

has_property(F, L) ->
	catch lists:foldl(fun(X, Stat) -> case {F(X), Stat} of
	                                  	{true, empty} -> all;
	                                  	{true, all} -> all;
	                                  	{true, none} -> throw(some);
	                                  	{false, empty} -> none;
	                                  	{false, all} -> throw(some);
	                                  	{false, none} -> none
	                                  end
	                  end,
	                  empty, L).



More information about the erlang-questions mailing list