altered ordering on the fetch and find functions, version patch bumped

Ordering altered to match existing list functions, Version patch
bumped because because API is not yet final

Signed-off-by: Eric Merritt <ericbmerritt@gmail.com>
This commit is contained in:
Martin J. Logan 2011-03-12 22:10:12 -06:00 committed by Eric Merritt
parent b4981ff4b5
commit 3c9e60c5a2
2 changed files with 44 additions and 50 deletions

View file

@ -1,7 +1,7 @@
%% -*- mode: Erlang; fill-column: 75; comment-column: 50; -*- %% -*- mode: Erlang; fill-column: 75; comment-column: 50; -*-
{application, erlware_commons, {application, erlware_commons,
[{description, "Additional standard library for Erlang"}, [{description, "Additional standard library for Erlang"},
{vsn, "0.1.0"}, {vsn, "0.2.0"},
{modules, [ {modules, [
ec_lists, ec_lists,
ec_string, ec_string,

View file

@ -20,27 +20,24 @@
%% term()} and processing is aborted, if the function returns false, %% term()} and processing is aborted, if the function returns false,
%% processing continues until the end of the list. If the end is found %% processing continues until the end of the list. If the end is found
%% and the function never returns true the atom error is returned. %% and the function never returns true the atom error is returned.
-spec find([term()], fun()) -> {ok, term()} | error. -spec find(fun(), list()) -> {ok, term()} | error.
find([Head | Tail], Fun) find(Fun, [Head|Tail]) when is_function(Fun) ->
when is_function(Fun) ->
case Fun(Head) of case Fun(Head) of
true -> true ->
{ok, Head}; {ok, Head};
false -> false ->
find(Tail, Fun) find(Fun, Tail)
end; end;
find([], _Fun) -> find(_Fun, []) ->
error. error.
%% @doc Fetch a value from the list. If the function returns true the %% @doc Fetch a value from the list. If the function returns true the
%% value is returend. If processing reaches the end of the list and %% value is returend. If processing reaches the end of the list and
%% the function has never returned true an exception not_found is %% the function has never returned true an exception not_found is
%% thrown. %% thrown.
-spec fetch(list(), fun()) -> term(). -spec fetch(fun(), list()) -> term().
fetch(List, Fun) fetch(Fun, List) when is_list(List), is_function(Fun) ->
when is_list(List), case find(Fun, List) of
is_function(Fun) ->
case find(List, Fun) of
{ok, Head} -> {ok, Head} ->
Head; Head;
error -> error ->
@ -56,38 +53,38 @@ fetch(List, Fun)
find1_test() -> find1_test() ->
TestData = [1, 2, 3, 4, 5, 6], TestData = [1, 2, 3, 4, 5, 6],
Result = find(TestData, Result = find(fun(5) ->
fun(5) ->
true; true;
(_) -> (_) ->
false false
end), end,
TestData),
?assertMatch({ok, 5}, Result), ?assertMatch({ok, 5}, Result),
Result2 = find(TestData, Result2 = find(fun(37) ->
fun(37) ->
true; true;
(_) -> (_) ->
false false
end), end,
TestData),
?assertMatch(error, Result2). ?assertMatch(error, Result2).
find2_test() -> find2_test() ->
TestData = ["one", "two", "three", "four", "five", "six"], TestData = ["one", "two", "three", "four", "five", "six"],
Result = find(TestData, Result = find(fun("five") ->
fun("five") ->
true; true;
(_) -> (_) ->
false false
end), end,
TestData),
?assertMatch({ok, "five"}, Result), ?assertMatch({ok, "five"}, Result),
Result2 = find(TestData, Result2 = find(fun(super_duper) ->
fun(super_duper) ->
true; true;
(_) -> (_) ->
false false
end), end,
TestData),
?assertMatch(error, Result2). ?assertMatch(error, Result2).
@ -95,84 +92,81 @@ find2_test() ->
find3_test() -> find3_test() ->
TestData = [{"one", 1}, {"two", 2}, {"three", 3}, {"four", 5}, {"five", 5}, TestData = [{"one", 1}, {"two", 2}, {"three", 3}, {"four", 5}, {"five", 5},
{"six", 6}], {"six", 6}],
Result = find(TestData, Result = find(fun({"one", 1}) ->
fun({"one", 1}) ->
true; true;
(_) -> (_) ->
false false
end), end,
TestData),
?assertMatch({ok, {"one", 1}}, Result), ?assertMatch({ok, {"one", 1}}, Result),
Result2 = find(TestData, Result2 = find(fun([fo, bar, baz]) ->
fun([fo, bar, baz]) ->
true; true;
({"onehundred", 100}) -> ({"onehundred", 100}) ->
true; true;
(_) -> (_) ->
false false
end), end,
TestData),
?assertMatch(error, Result2). ?assertMatch(error, Result2).
fetch1_test() -> fetch1_test() ->
TestData = [1, 2, 3, 4, 5, 6], TestData = [1, 2, 3, 4, 5, 6],
Result = fetch(TestData, Result = fetch(fun(5) ->
fun(5) ->
true; true;
(_) -> (_) ->
false false
end), end,
TestData),
?assertMatch(5, Result), ?assertMatch(5, Result),
?assertThrow(not_found, ?assertThrow(not_found,
fetch(TestData, fetch(fun(37) ->
fun(37) ->
true; true;
(_) -> (_) ->
false false
end)). end,
TestData)).
fetch2_test() -> fetch2_test() ->
TestData = ["one", "two", "three", "four", "five", "six"], TestData = ["one", "two", "three", "four", "five", "six"],
Result = fetch(TestData, Result = fetch(fun("five") ->
fun("five") ->
true; true;
(_) -> (_) ->
false false
end), end,
TestData),
?assertMatch("five", Result), ?assertMatch("five", Result),
?assertThrow(not_found, ?assertThrow(not_found,
fetch(TestData, fetch(fun(super_duper) ->
fun(super_duper) ->
true; true;
(_) -> (_) ->
false false
end)). end,
TestData)).
fetch3_test() -> fetch3_test() ->
TestData = [{"one", 1}, {"two", 2}, {"three", 3}, {"four", 5}, {"five", 5}, TestData = [{"one", 1}, {"two", 2}, {"three", 3}, {"four", 5}, {"five", 5},
{"six", 6}], {"six", 6}],
Result = fetch(TestData, Result = fetch(fun({"one", 1}) ->
fun({"one", 1}) ->
true; true;
(_) -> (_) ->
false false
end), end,
TestData),
?assertMatch({"one", 1}, Result), ?assertMatch({"one", 1}, Result),
?assertThrow(not_found, ?assertThrow(not_found,
fetch(TestData, fetch(fun([fo, bar, baz]) ->
fun([fo, bar, baz]) ->
true; true;
({"onehundred", 100}) -> ({"onehundred", 100}) ->
true; true;
(_) -> (_) ->
false false
end)). end,
TestData)).
-endif. -endif.