forked from atomvm/AtomVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes atomvm#1159. Carbon copy from https://github.com/erlang/otp/blob/0b1bab25dbabb6c19319f3373c5a6dfa74667210/lib/stdlib/src/lists.erl#L1252 Signed-off-by: Peter M <[email protected]>
- Loading branch information
Showing
3 changed files
with
41 additions
and
0 deletions.
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
% | ||
% Copyright 2017-2023 Fred Dushin <[email protected]> | ||
% split/2 function Copyright Ericsson AB 1996-2023. | ||
% keytake/3 function Copyright Ericsson AB 1996-2024. | ||
% | ||
% Licensed under the Apache License, Version 2.0 (the "License"); | ||
% you may not use this file except in compliance with the License. | ||
|
@@ -40,6 +41,7 @@ | |
keyfind/3, | ||
keymember/3, | ||
keyreplace/4, | ||
keytake/3, | ||
foldl/3, | ||
foldr/3, | ||
all/2, | ||
|
@@ -304,6 +306,30 @@ keyreplace(K, I, [H | T], L, NewTuple, NewList) when is_tuple(H) andalso is_tupl | |
keyreplace(K, I, [H | T], L, NewTuple, NewList) -> | ||
keyreplace(K, I, T, L, NewTuple, [H | NewList]). | ||
|
||
%%----------------------------------------------------------------------------- | ||
%% @param Key the key to match | ||
%% @param N the position in the tuple to compare (1..tuple_size) | ||
%% @param TupleList1 the list of tuples from which to find the element | ||
%% @returns `{value, Tuple, TupleList2}` if such a tuple is found, otherwise `false`. `TupleList2` is a copy of `TupleList1` where the first occurrence of `Tuple` has been removed. | ||
%% @doc Searches the list of tuples `TupleList1` for a tuple whose `N`th element | ||
%% compares equal to `Key`. | ||
%% @end | ||
%%----------------------------------------------------------------------------- | ||
-spec keytake(Key, N, TupleList1) -> {value, Tuple, TupleList2} | false when | ||
Key :: term(), | ||
N :: pos_integer(), | ||
TupleList1 :: [tuple()], | ||
TupleList2 :: [tuple()], | ||
Tuple :: tuple(). | ||
keytake(Key, N, L) when is_integer(N), N > 0 -> | ||
keytake(Key, N, L, []). | ||
keytake(Key, N, [H | T], L) when element(N, H) == Key -> | ||
{value, H, lists:reverse(L, T)}; | ||
keytake(Key, N, [H | T], L) -> | ||
keytake(Key, N, T, [H | L]); | ||
keytake(_K, _N, [], _L) -> | ||
false. | ||
|
||
%%----------------------------------------------------------------------------- | ||
%% @param Fun the function to apply | ||
%% @param Acc0 the initial accumulator | ||
|
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