Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.