Its a recursive function iterating over a list. The blank list pattern
terminates the search at the end of the list. If you get to the end of
the list without the function returning a value then the item was not
found. The same for an empty list. The function has to return {ok,
Value} for the search to succeed. In the case where you reach the end
of the list, or the list is empty the function would have never done
that.Rereading the documentation that seems apparent, maybe I am missing something? On Mon, Sep 26, 2011 at 10:07 PM, Jordan Wilberding <[email protected]> wrote: > Comment below: > > On Mon, Sep 26, 2011 at 1:42 PM, Eric Merritt <[email protected]> > wrote: >> >> --- >> src/ec_lists.erl | 91 >> ++++++++++++++++++++++++++++++++++++++++++++++++----- >> 1 files changed, 82 insertions(+), 9 deletions(-) >> >> diff --git a/src/ec_lists.erl b/src/ec_lists.erl >> index 6f76dcc..b80b700 100644 >> --- a/src/ec_lists.erl >> +++ b/src/ec_lists.erl >> @@ -9,12 +9,31 @@ >> >> %% API >> -export([find/2, >> - fetch/2]). >> + fetch/2, >> + search/2]). >> >> %%%=================================================================== >> %%% API >> %%%=================================================================== >> >> +%% @doc Search each value in the list with the specified >> +%% function. When the function returns a value of {ok, term()} the >> +%% search function stops and returns a tuple of {ok, term(), term()}, >> +%% where the second value is the term returned from the function and >> +%% the third value is the element passed to the function. The purpose >> +%% of this is to allow a list to be searched where some internal state >> +%% is important while the input element is not. >> +-spec search(fun(), list()) -> {ok, Result::term(), Element::term()}. >> +search(Fun, [H|T]) -> >> + case Fun(H) of >> + {ok, Value} -> >> + {ok, Value, H}; >> + not_found -> >> + search(Fun, T) >> + end; >> +search(_, []) -> >> + not_found. > > I think this implementation is incorrect from the description. I'd argue, > given the documentation above, someone could realistically think that this > function would be valid for a function and an empty list. While I can't > think of a good example, I don't know why we need to pattern match on the > blank list and assume it is not_found. Could be looking for a blank list for > all we know. > At the very least, we need to specify in the docs a search on a blank list > is always not_found. > Thanks! > JW > > -- > You received this message because you are subscribed to the Google Groups > "erlware-dev" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/erlware-dev?hl=en. > -- You received this message because you are subscribed to the Google Groups "erlware-dev" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/erlware-dev?hl=en.
