diff --git a/Makefile b/Makefile index e503a3d..10c1ed7 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ include erlang.mk COMPILE_FIRST = ktn_recipe -CT_SUITES = ktn_maps ktn_recipe ktn_numbers ktn_binary +CT_SUITES = ktn_maps ktn_recipe ktn_numbers ktn_binary ktn_lists shell: app erl -pa ebin -pa deps/*/ebin -s sync diff --git a/src/ktn_lists.erl b/src/ktn_lists.erl new file mode 100644 index 0000000..b1d4c03 --- /dev/null +++ b/src/ktn_lists.erl @@ -0,0 +1,22 @@ +-module(ktn_lists). + +-export([ + delete_first/2 + ]). + +%% @doc Returns a copy of List deleting the first Element where Fun(Element) +%% returns true, if there is such an element. +%% @end +-spec delete_first(fun((term()) -> boolean()), list()) -> list(). +delete_first(Fun, List) -> + delete_first(Fun, List, []). + +delete_first(Fun, [], Acc) when is_function(Fun, 1) -> + lists:reverse(Acc); +delete_first(Fun, [Head | Tail], Acc) -> + case Fun(Head) of + false -> + delete_first(Fun, Tail, [Head | Acc]); + true -> + lists:concat([lists:reverse(Acc), Tail]) + end. \ No newline at end of file diff --git a/test/ktn_lists_SUITE.erl b/test/ktn_lists_SUITE.erl new file mode 100644 index 0000000..f79c70f --- /dev/null +++ b/test/ktn_lists_SUITE.erl @@ -0,0 +1,54 @@ +-module(ktn_lists_SUITE). + +-export([ + all/0, + init_per_suite/1, + end_per_suite/1 + ]). + +-export([ + delete_first/1 + ]). + +-define(EXCLUDED_FUNS, + [ + module_info, + all, + test, + init_per_suite, + end_per_suite + ]). + +-type config() :: [{atom(), term()}]. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% Common test +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +-spec all() -> [atom()]. +all() -> + Exports = ?MODULE:module_info(exports), + [F || {F, _} <- Exports, not lists:member(F, ?EXCLUDED_FUNS)]. + +-spec init_per_suite(config()) -> config(). +init_per_suite(Config) -> + Config. + +-spec end_per_suite(config()) -> config(). +end_per_suite(Config) -> + Config. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% Test Cases +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +-spec delete_first(config()) -> ok. +delete_first(_Config) -> + Fun = fun(N) -> 0 == N rem 2 end, + + [] = ktn_lists:delete_first(Fun, []), + [] = ktn_lists:delete_first(Fun, [4]), + [4] = ktn_lists:delete_first(Fun, [4, 4]), + [1, 3] = ktn_lists:delete_first(Fun, [1, 3]), + [1, 3] = ktn_lists:delete_first(Fun, [1, 4, 3]), + [1, 3, 4] = ktn_lists:delete_first(Fun, [1, 4, 3, 4]).