forked from lambdaclass/erlang-katana
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from inaka/ktn_lists.delete_first
added ktn_lists:delete_first/2
- Loading branch information
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]). |