Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Erlang first match functions (doesn't evaluate past first match)
% From
-export([first/3, first1/3]).
% My solution
first([E | Rest], Condition, Default) ->
case Condition(E) of
true -> E;
false -> first(Rest, Condition, Default)
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
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.