[m-rev.] for review: Add list.any_true/2.

Peter Wang novalazy at gmail.com
Tue Oct 9 16:53:42 AEDT 2018


Hi,

Anyone else ever want this? An alternative name would be some_true/2

----

Add list.any_true/2.

list.any_true/2 is a trivial predicate but a goal

    any_true(Pred, List)

would usually be clearer to the reader than any of these alternatives:

    find_first_match(Pred, List, _)
    not all_false(Pred, List)
    some [X] ( member(X, Xs), Pred(X) )

library/list.m:
    Add list.any_true/2.

NEWS:
    Announce the addition.

diff --git a/NEWS b/NEWS
index 59c4edbd1..7958674bf 100644
--- a/NEWS
+++ b/NEWS
@@ -398,20 +398,21 @@ Changes to the Mercury standard library:
 
 * The following predicates and functions have been added to the int module:
 
   - all_true_in_range/3
   - nabs/1
 
 * We have added a predicate named is_dummy_context to the term module.
 
 * The following predicates and functions have been added to the list module:
 
+   - any_true/2
    - reverse_prepend/2
    - reverse_prepend/3
    - take_while/4
    - take_while/3
    - take_while/2
    - drop_while/3
    - drop_while/2
 
 * The takewhile/4 predicate has been deprecated in the list module,
   take_while/4 can be used instead.
diff --git a/library/list.m b/library/list.m
index 9f75d6e93..4d227a866 100644
--- a/library/list.m
+++ b/library/list.m
@@ -671,20 +671,26 @@
 %
 %---------------------------------------------------------------------------%
 
     % find_first_match(Pred, List, FirstMatch) takes a closure with one
     % input argument. It returns the first element X of the list (if any)
     % for which Pred(X) is true.
     %
 :- pred find_first_match(pred(X)::in(pred(in) is semidet), list(X)::in,
     X::out) is semidet.
 
+    % any_true(Pred, List):
+    % Succeeds iff Pred succeeds for at least one element of List.
+    % Same as find_first_match(Pred, List, _FirstMatch).
+    %
+:- pred any_true(pred(X)::in(pred(in) is semidet), list(X)::in) is semidet.
+
     % all_true(Pred, List) takes a closure with one input argument.
     % If Pred succeeds for every member of List, all_true succeeds.
     % If Pred fails for any member of List, all_true fails.
     %
 :- pred all_true(pred(X)::in(pred(in) is semidet), list(X)::in) is semidet.
 
     % all_false(Pred, List) takes a closure with one input argument.
     % If Pred fails for every member of List, all_false succeeds.
     % If Pred succeeds for any member of List, all_false fails.
     %
@@ -2843,20 +2849,23 @@ list_to_doc_2([X | Xs]) = Doc :-
 %---------------------------------------------------------------------------%
 %---------------------------------------------------------------------------%
 
 find_first_match(P, [H | T], FirstMatch) :-
     ( if P(H) then
         FirstMatch = H
     else
         find_first_match(P, T, FirstMatch)
     ).
 
+any_true(P, L) :-
+    find_first_match(P, L, _FirstMatch).
+
 all_true(_P, []).
 all_true(P, [X | Xs]) :-
     P(X),
     all_true(P, Xs).
 
 all_false(_P, []).
 all_false(P, [X | Xs]) :-
     not P(X),
     all_false(P, Xs).
 
-- 
2.19.1



More information about the reviews mailing list