Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ktn_lists:delete_first/2 #23

Merged
merged 1 commit into from
Feb 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 22 additions & 0 deletions src/ktn_lists.erl
Original file line number Diff line number Diff line change
@@ -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.
54 changes: 54 additions & 0 deletions test/ktn_lists_SUITE.erl
Original file line number Diff line number Diff line change
@@ -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]).