Skip to content

Commit

Permalink
Merge pull request #23 from inaka/ktn_lists.delete_first
Browse files Browse the repository at this point in the history
added ktn_lists:delete_first/2
  • Loading branch information
jfacorro committed Feb 12, 2015
2 parents 6c605d6 + 0773f4b commit 1b71c63
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
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]).

0 comments on commit 1b71c63

Please sign in to comment.