File 3641-Add-lists-enumerate-1-and-lists-enumerate-2.patch of Package erlang

From 0aef479c4fb9eb31f7c3e9edac601f52288426fd Mon Sep 17 00:00:00 2001
From: radrow <radrowicki@gmail.com>
Date: Wed, 2 Jun 2021 14:06:44 +0200
Subject: [PATCH] Add lists:enumerate/1 and lists:enumerate/2

This commit introduces `enumerate/1` and `enumerate/2` functions to
the `lists` module in standard library. Simple unit tests and
documentation for the introduced functions are included.

Fix test

Equalities in docs

Better binary test

Change the definition example

change the definition example back to mapfoldl

Remove unnecessary parens

Co-authored-by: Lukas Larsson <garazdawi@gmail.com>

Fix test result
---
 lib/stdlib/doc/src/lists.xml    | 42 +++++++++++++++++++++++++++++++++
 lib/stdlib/src/lists.erl        | 20 +++++++++++++++-
 lib/stdlib/test/lists_SUITE.erl | 20 ++++++++++++++--
 3 files changed, 79 insertions(+), 3 deletions(-)

diff --git a/lib/stdlib/doc/src/lists.xml b/lib/stdlib/doc/src/lists.xml
index 5477f13d1e..ad60829e72 100644
--- a/lib/stdlib/doc/src/lists.xml
+++ b/lib/stdlib/doc/src/lists.xml
@@ -174,6 +174,48 @@
       </desc>
     </func>
 
+    <func>
+      <name name="enumerate" arity="1"/>
+      <fsummary>Annotates elements with their index.</fsummary>
+      <desc>
+        <p>Returns <c><anno>List1</anno></c> with each element
+        <c>H</c> replaced by a tuple of form <c>{I, H}</c> where
+        <c>I</c> is the position of <c>H</c> in
+        <c><anno>List1</anno></c>. The enumeration starts with 1 and
+        increases by 1 in each step.</p>
+        <p>That is, <c>enumerate/1</c> behaves as if it had been defined as follows:</p>
+        <code type="erl">
+enumerate(List) ->
+  {List1, _ } = lists:mapfoldl(fun(T, Acc) -> {{Acc, T}, Acc+1} end, 1, List),
+  List1.</code>
+        <p><em>Example:</em></p>
+        <pre>
+> <input>lists:enumerate([a,b,c]).</input>
+[{1,a},{2,b},{3,c}]</pre>
+      </desc>
+    </func>
+    
+    <func>
+      <name name="enumerate" arity="2"/>
+      <fsummary>Annotates elements with their index.</fsummary>
+      <desc>
+        <p>Returns <c><anno>List1</anno></c> with each element
+        <c>H</c> replaced by a tuple of form <c>{I, H}</c> where
+        <c>I</c> is the position of <c>H</c> in
+        <c><anno>List1</anno></c>. The enumeration starts with
+        <c><anno>Index</anno></c> and increases by 1 in each step.</p>
+        <p>That is, <c>enumerate/2</c> behaves as if it had been defined as follows:</p>
+        <code type="erl">
+enumerate(I, List) ->
+  {List1, _ } = lists:mapfoldl(fun(T, Acc) -> {{Acc, T}, Acc+1} end, I, List),
+  List1.</code>
+        <p><em>Example:</em></p>
+        <pre>
+> <input>lists:enumerate(10, [a,b,c]).</input>
+[{10,a},{11,b},{12,c}]</pre>
+      </desc>
+    </func>
+    
     <func>
       <name name="filter" arity="2"/>
       <fsummary>Select elements that satisfy a predicate.</fsummary>
diff --git a/lib/stdlib/src/lists.erl b/lib/stdlib/src/lists.erl
index f44ed726ca..b82732e0ca 100644
--- a/lib/stdlib/src/lists.erl
+++ b/lib/stdlib/src/lists.erl
@@ -32,7 +32,7 @@
 	 concat/1, flatten/1, flatten/2, flatlength/1,
 	 keydelete/3, keyreplace/4, keytake/3, keystore/4,
 	 keysort/2, keymerge/3, rkeymerge/3, rukeymerge/3, 
-	 ukeysort/2, ukeymerge/3, keymap/3]).
+	 ukeysort/2, ukeymerge/3, keymap/3, enumerate/1, enumerate/2]).
 
 -export([merge/3, rmerge/3, sort/2, umerge/3, rumerge/3, usort/2]).
 
@@ -957,6 +957,24 @@ keymap(Fun, Index, [Tup|Tail]) ->
 keymap(Fun, Index, []) when is_integer(Index), Index >= 1, 
                             is_function(Fun, 1) -> [].
 
+-spec enumerate(List1) -> List2 when
+      List1 :: [T],
+      List2 :: [{Index, T}],
+      Index :: integer(),
+      T :: term().
+enumerate(List1) ->
+    enumerate(1, List1).
+
+-spec enumerate(Index, List1) -> List2 when
+      List1 :: [T],
+      List2 :: [{Index, T}],
+      Index :: integer(),
+      T :: term().
+enumerate(Index, [H|T]) when is_integer(Index) ->
+    [{Index, H}|enumerate(Index + 1, T)];
+enumerate(Index, []) when is_integer(Index) ->
+    [].
+
 %%% Suggestion from OTP-2948: sort and merge with Fun.
 
 -spec sort(Fun, List1) -> List2 when
diff --git a/lib/stdlib/test/lists_SUITE.erl b/lib/stdlib/test/lists_SUITE.erl
index 5a9d72e0e3..fb11cbba8b 100644
--- a/lib/stdlib/test/lists_SUITE.erl
+++ b/lib/stdlib/test/lists_SUITE.erl
@@ -57,7 +57,8 @@
 	 filter_partition/1, 
 	 join/1,
 	 otp_5939/1, otp_6023/1, otp_6606/1, otp_7230/1,
-	 suffix/1, subtract/1, droplast/1, hof/1]).
+	 suffix/1, subtract/1, droplast/1, hof/1,
+         enumerate/1]).
 
 %% Sort randomized lists until stopped.
 %%
@@ -121,7 +122,7 @@ groups() ->
      {zip, [parallel], [zip_unzip, zip_unzip3, zipwith, zipwith3]},
      {misc, [parallel], [reverse, member, dropwhile, takewhile,
 			 filter_partition, suffix, subtract, join,
-			 hof]}
+			 hof, enumerate]}
     ].
 
 init_per_suite(Config) ->
@@ -2653,3 +2654,19 @@ hof(Config) when is_list(Config) ->
     false = lists:all(fun(N) -> N rem 2 =:= 0 end, L),
 
     ok.
+
+%% Test lists:enumerate/1 and lists:enumerate/2
+enumerate(Config) when is_list(Config) ->
+    [] = lists:enumerate([]),
+    [] = lists:enumerate(10, []),
+    [{1,a},{2,b},{3,c}] = lists:enumerate([a,b,c]),
+    [{10,a},{11,b},{12,c}] = lists:enumerate(10, [a,b,c]),
+    {'EXIT', {function_clause, _}} = catch lists:enumerate(0),
+    {'EXIT', {function_clause, _}} = catch lists:enumerate(0, 10),
+    {'EXIT', {function_clause, _}} = catch lists:enumerate(1.0, []),
+    {'EXIT', {function_clause, _}} = catch lists:enumerate(1.0, [a,b,c]),
+    {'EXIT', {function_clause, _}} = catch lists:enumerate(<<1>>, []),
+    {'EXIT', {function_clause, _}} = catch lists:enumerate(<<1>>, [a,b,c]),
+    {'EXIT', {function_clause, _}} = catch lists:enumerate(1, <<1,2,3>>),
+
+    ok.
-- 
2.26.2

openSUSE Build Service is sponsored by