[MLton-commit] r4856

Vesa Karvonen vesak at mlton.org
Tue Nov 21 23:19:23 PST 2006


Added span, takeWhile, and dropWhile.
----------------------------------------------------------------------

U   mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml
U   mltonlib/trunk/com/ssh/extended-basis/unstable/public/list.sig

----------------------------------------------------------------------

Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml	2006-11-21 11:48:26 UTC (rev 4855)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml	2006-11-22 07:19:22 UTC (rev 4856)
@@ -83,4 +83,17 @@
    fun contains l x = exists (fn y => x = y) l
    fun maximum cmp = foldl1 (Cmp.max cmp o Pair.swap)
    fun minimum cmp = foldl1 (Cmp.min cmp o Pair.swap)
+   local
+      fun mk combine init pred xs = let
+         fun lp (ts, []) = (ts, [])
+           | lp (ts, x::xs) =
+             if pred x then lp (combine (x, ts), xs) else (ts, x::xs)
+      in
+         lp (init, xs)
+      end
+   in
+      fun span ? = Pair.map (rev, Fn.id) o mk op :: [] ?
+      fun takeWhile ? = rev o #1 o mk op :: [] ?
+      fun dropWhile ? = #2 o mk ignore () ?
+   end
 end

Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/public/list.sig
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/public/list.sig	2006-11-21 11:48:26 UTC (rev 4855)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/public/list.sig	2006-11-22 07:19:22 UTC (rev 4856)
@@ -25,14 +25,6 @@
     * {Empty} if the list is empty.
     *)
 
-   val split : 'a t * Int.t -> 'a t Sq.t
-   (**
-    * {split (l, i)} returns a pair f the first {i} and last {length l -
-    * i} elements of the list {l}.  Raises {Subscript} if {i < 0 orelse
-    * length l < i}.  Specifically, {split (l, n) = (take (l, n), drop (l,
-    * n))}.
-    *)
-
    (** == Transformations == *)
 
    val intersperse : 'a -> 'a t UnOp.t
@@ -91,6 +83,28 @@
    val unfoldr : ('a -> ('b * 'a) Option.t) -> 'a -> 'b list
    val unfoldr' : ('a -> ('b * 'a) Option.t) -> 'a -> 'b list * 'a
 
+   (** == Extracting sublists == *)
+
+   val split : 'a t * Int.t -> 'a t Sq.t
+   (**
+    * {split (l, i)} returns a pair f the first {i} and last {length l -
+    * i} elements of the list {l}.  Raises {Subscript} if {i < 0 orelse
+    * length l < i}.  Specifically, {split (l, n) = (take (l, n), drop (l,
+    * n))}.
+    *)
+
+   val takeWhile : 'a UnPr.t -> 'a t UnOp.t
+   (**
+    * {takeWhile p xs} returns the longest prefix of {xs} of elements that
+    * satisfy {p}.
+    *)
+
+   val dropWhile : 'a UnPr.t -> 'a t UnOp.t
+   (** {dropWhile p xs} returns the suffix remaining after {takeWhile p xs}. *)
+
+   val span : 'a UnPr.t -> 'a t -> 'a t Sq.t
+   (** {span p xs = (takeWhile p xs, dropWhile p xs)}. *)
+
    (** == Set Operations == *)
 
    val contains : ''a t -> ''a UnPr.t




More information about the MLton-commit mailing list