Created
September 30, 2012 15:17
-
-
Save dantswain/3807110 to your computer and use it in GitHub Desktop.
Erlang first match functions (doesn't evaluate past first match)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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