File 2781-Add-all-2-and-any-2-to-the-queue-module.patch of Package erlang
From cf3eae431c1d60f703f7c56e43fd5048d0d2ffc7 Mon Sep 17 00:00:00 2001
From: Maria-12648430 <maria-12648430@gmx.net>
Date: Mon, 16 Nov 2020 12:50:56 +0100
Subject: [PATCH 1/5] Add all/2 and any/2 to the queue module
---
lib/stdlib/doc/src/queue.xml | 24 ++++++++++++++++++++++++
lib/stdlib/src/queue.erl | 24 +++++++++++++++++++++++-
lib/stdlib/test/queue_SUITE.erl | 12 ++++++++++++
3 files changed, 59 insertions(+), 1 deletion(-)
diff --git a/lib/stdlib/doc/src/queue.xml b/lib/stdlib/doc/src/queue.xml
index 98971b1f4e..297ea34ace 100644
--- a/lib/stdlib/doc/src/queue.xml
+++ b/lib/stdlib/doc/src/queue.xml
@@ -53,6 +53,8 @@
assuming knowledge of the format is running on thin ice.</p>
<p>All operations have an amortized O(1) running time, except
+ <seemfa marker="#all/2"><c>all/2</c></seemfa>,
+ <seemfa marker="#any/2"><c>any/2</c></seemfa>,
<seemfa marker="#filter/2"><c>filter/2</c></seemfa>,
<seemfa marker="#filtermap/2"><c>filtermap/2</c></seemfa>,
<seemfa marker="#fold/3"><c>fold/3</c></seemfa>,
@@ -115,6 +117,28 @@
<fsdescription>
<title>Original API</title>
</fsdescription>
+ <func>
+ <name name="all" arity="2" since=""/>
+ <fsummary>Return <c>true</c> if all items in a queue satisfy
+ <c>Pred</c>.</fsummary>
+ <desc>
+ <p>Returns <c>true</c> if <c><anno>Pred</anno>(<anno>Item</anno>)</c>
+ returns <c>true</c> for all items <c><anno>Item</anno></c> in
+ <c><anno>Q</anno></c>, otherwise <c>false</c>.</p>
+ </desc>
+ </func>
+
+ <func>
+ <name name="any" arity="2" since=""/>
+ <fsummary>Return <c>true</c> if any of the items in a queue satisfy
+ <c>Pred</c>.</fsummary>
+ <desc>
+ <p>Returns <c>true</c> if <c><anno>Pred</anno>(<anno>Item</anno>)</c>
+ returns <c>true</c> for at least one item <c><anno>Item</anno></c>
+ in <c><anno>Q</anno></c>, otherwise <c>false</c>.</p>
+ </desc>
+ </func>
+
<func>
<name name="filter" arity="2" since=""/>
<fsummary>Filter a queue.</fsummary>
diff --git a/lib/stdlib/src/queue.erl b/lib/stdlib/src/queue.erl
index 06c8ae51df..e58f0404f0 100644
--- a/lib/stdlib/src/queue.erl
+++ b/lib/stdlib/src/queue.erl
@@ -27,7 +27,7 @@
-export([get/1,get_r/1,peek/1,peek_r/1,drop/1,drop_r/1]).
%% Higher level API
--export([reverse/1,join/2,split/2,filter/2,filtermap/2,fold/3]).
+-export([reverse/1,join/2,split/2,filter/2,filtermap/2,fold/3,any/2,all/2]).
%% Okasaki API from klacke
-export([cons/2,head/1,tail/1,
@@ -453,6 +453,28 @@ fold(Fun, Acc0, {R, F}) when is_function(Fun, 2), is_list(R), is_list(F) ->
fold(Fun, Acc0, Q) ->
erlang:error(badarg, [Fun, Acc0, Q]).
+%% Check if any item satisfies the predicate, traverse in queue order.
+%%
+%% O(len(Q)) worst case
+-spec any(Pred, Q :: queue(Item)) -> boolean() when
+ Pred :: fun((Item) -> boolean()).
+any(Pred, {R, F}) when is_function(Pred, 1), is_list(R), is_list(F) ->
+ lists:any(Pred, F) orelse
+ lists:any(Pred, R);
+any(Pred, Q) ->
+ erlang:error(badarg, [Pred, Q]).
+
+%% Check if all items satisfy the predicate, traverse in queue order.
+%%
+%% O(len(Q)) worst case
+-spec all(Pred, Q :: queue(Item)) -> boolean() when
+ Pred :: fun((Item) -> boolean()).
+all(Pred, {R, F}) when is_function(Pred, 1), is_list(R), is_list(F) ->
+ lists:all(Pred, F) andalso
+ lists:all(Pred, R);
+all(Pred, Q) ->
+ erlang:error(badarg, [Pred, Q]).
+
%%--------------------------------------------------------------------------
%% Okasaki API inspired by an Erlang user contribution "deque.erl"
%% by Claes Wikstrom <klacke@kaja.klacke.net> 1999.
diff --git a/lib/stdlib/test/queue_SUITE.erl b/lib/stdlib/test/queue_SUITE.erl
index f2bad3c3b8..28a3dd7e9d 100644
--- a/lib/stdlib/test/queue_SUITE.erl
+++ b/lib/stdlib/test/queue_SUITE.erl
@@ -388,6 +388,18 @@ do_op_test(F) ->
FoldExp2 = [X*X || X <- L1],
FoldAct2 = queue:fold(fun(X,A) -> [X*X|A] end, [], FoldQ),
FoldExp2 = lists:reverse(FoldAct2),
+ false = queue:any(fun(X) -> X>9 end, queue:from_list([])),
+ AnyQ = queue:from_list([1,2,3]),
+ true = queue:any(fun(X) -> X>1 end, AnyQ),
+ false = queue:any(fun(X) -> X<1 end, AnyQ),
+ true = queue:any(fun(X) -> X<3 end, AnyQ),
+ false = queue:any(fun(X) -> X>3 end, AnyQ),
+ true = queue:all(fun(X) -> X>9 end, queue:from_list([])),
+ AllQ = queue:from_list([1,2,3]),
+ true = queue:all(fun(X) -> X>=1 end, AllQ),
+ false = queue:all(fun(X) -> X>1 end, AllQ),
+ true = queue:all(fun(X) -> X=<3 end, AllQ),
+ false = queue:all(fun(X) -> X<3 end, AllQ),
%%
ok.
--
2.26.2