[m-rev.] for review: Add take_while and drop_while to the list module

Paul Bone paul at bone.id.au
Thu Apr 21 11:11:02 AEST 2016


For review by anyone

---

Add take_while and drop_while to the list module

Add new predicates and functions take_while and drop_while to the list
module.

library/list.m:
    As above.

NEWS:
    Announce this change.
---
 NEWS           |  4 ++++
 library/list.m | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/NEWS b/NEWS
index c269b21..faa772a 100644
--- a/NEWS
+++ b/NEWS
@@ -280,6 +280,10 @@ Changes to the Mercury standard library:
 
    - reverse_prepend/2
    - reverse_prepend/3
+   - take_while/3
+   - take_while/2
+   - drop_while/3
+   - drop_while/2
 
 * The following predicate and function in the builtin module have been
   deprecated and will be removed in a future release:
diff --git a/library/list.m b/library/list.m
index 9565434..b7e51f7 100644
--- a/library/list.m
+++ b/library/list.m
@@ -251,6 +251,15 @@
 :- pred take_upto(int::in, list(T)::in, list(T)::out) is det.
 :- func take_upto(int, list(T)) = list(T).
 
+    % take_while(Pred, List, Start):
+    %
+    % 'Start' is the first elements of 'List' that satisfy 'Pred'.
+    %
+:- pred take_while(pred(T)::in(pred(in) is semidet), list(T)::in,
+    list(T)::out) is det.
+:- func take_while(pred(T), list(T)) = list(T).
+:- mode take_while(pred(in) is semidet, in) = out is det.
+
     % drop(Len, List, End):
     %
     % `End' is the remainder of `List' after removing the first `Len' elements.
@@ -267,6 +276,16 @@
     %
 :- pred det_drop(int::in, list(T)::in, list(T)::out) is det.
 
+    % drop_while(Pred, List, End):
+    %
+    % 'End' is the remaining elements of 'List' after dropping those that
+    % satisfy 'Pred'.
+    %
+:- pred drop_while(pred(T)::in(pred(in) is semidet), list(T)::in,
+    list(T)::out) is det.
+:- func drop_while(pred(T), list(T)) = list(T).
+:- mode drop_while(pred(in) is semidet, in) = out is det.
+
     % insert(Elem, List0, List):
     %
     % `List' is the result of inserting `Elem' somewhere in `List0'.
@@ -2339,6 +2358,18 @@ list.take_upto(N, Xs, InitialXs) :-
         InitialXs = Xs
     ).
 
+take_while(_, [], []).
+take_while(P, [X | Xs], Start) :-
+    ( if P(X) then
+        take_while(P, Xs, Start0),
+        Start = [X | Start0]
+    else
+        Start = []
+    ).
+
+take_while(P, Xs) = Start :-
+    take_while(P, Xs, Start).
+
 list.drop(N, Xs, FinalXs) :-
     ( if N > 0 then
         Xs = [_ | Tail],
@@ -2360,6 +2391,17 @@ list.det_drop(N, Xs, FinalXs) :-
         FinalXs = Xs
     ).
 
+drop_while(_, [], []).
+drop_while(P, [X | Xs], End) :-
+    ( if P(X) then
+        drop_while(P, Xs, End)
+    else
+        End = [X | Xs]
+    ).
+
+drop_while(P, Xs) = End :-
+    drop_while(P, Xs, End).
+
 %---------------------------------------------------------------------------%
 
 list.duplicate(N, X) = Xs :-
-- 
2.8.0.rc3



More information about the reviews mailing list