Skip to content

Instantly share code, notes, and snippets.

@dantswain
Created September 30, 2012 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dantswain/3807110 to your computer and use it in GitHub Desktop.
Save dantswain/3807110 to your computer and use it in GitHub Desktop.
Erlang first match functions (doesn't evaluate past first match)
% From http://stackoverflow.com/questions/12657202/
-export([first/3, first1/3]).
% My solution
first([E | Rest], Condition, Default) ->
case Condition(E) of
true -> E;
false -> first(Rest, Condition, Default)
end;
first([], _Cond, Default) -> Default.
% Odobenus Rosmarus solution, wrapped up a little
first1(L, Condition, Default) ->
case lists:dropwhile(fun(E) -> not Condition(E) end, L) of
[] -> Default;
[F | _] -> F
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment