[MLton-commit] r5536

Vesa Karvonen vesak at mlton.org
Sat Apr 21 15:07:32 PDT 2007


Added inits and tails.
----------------------------------------------------------------------

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

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

Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/detail/sequence/list.sml
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/detail/sequence/list.sml	2007-04-21 19:17:45 UTC (rev 5535)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/detail/sequence/list.sml	2007-04-21 22:07:32 UTC (rev 5536)
@@ -96,20 +96,18 @@
       fun takeWhile ? = rev o #1 o mk op :: [] ?
       fun dropWhile ? = #2 o mk ignore () ?
    end
+   fun process f s = fn [] => s | x::xs => Fn.uncurry (process f) (f (x, s, xs))
    fun uniqueByEq eq =
     fn [] => true
      | x::xs => not (exists (Fn.curry eq x) xs) andalso uniqueByEq eq xs
-   fun divideByEq eq = let
-      fun lp accum =
-       fn [] => rev accum
-        | x::xs => let
-             val (xclass, remainder) = partition (Fn.curry eq x) xs
-          in
-             lp ((x::xclass)::accum) remainder
-          end
-   in
-      lp []
-   end
+   fun tails l = rev ([]::process (fn (x, a, xs) => ((x::xs)::a, xs)) [] l)
+   fun inits l = []::process (fn (x, a, xs) => (rev (x::xs)::a, xs)) [] (rev l)
+   fun divideByEq eq =
+       rev o process (fn (y, cs, xs) => let
+                            val (ys, xs) = partition (Fn.curry eq y) xs
+                         in
+                            ((y::ys)::cs, xs)
+                         end) []
    fun nubByEq eq =
        rev o foldl (fn (x, ys) =>
                        if exists (Fn.curry eq x) ys then ys else x::ys) []

Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/public/sequence/list.sig
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/public/sequence/list.sig	2007-04-21 19:17:45 UTC (rev 5535)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/public/sequence/list.sig	2007-04-21 22:07:32 UTC (rev 5536)
@@ -25,6 +25,16 @@
     * {Empty} if the list is empty.
     *)
 
+   val inits : 'a t -> 'a t t
+   (**
+    * Returns all initial segments of the given list, shortest first.
+    *
+    * Warning: The time and space complexity of this function is {O(n*n)}.
+    *)
+
+   val tails : 'a t -> 'a t t
+   (** Returns all final segments of the given list, longest first. *)
+
    (** == Transformations == *)
 
    val intersperse : 'a -> 'a t UnOp.t
@@ -78,10 +88,10 @@
 
    (** == Unfolding == *)
 
-   val unfoldl : ('a -> ('b * 'a) Option.t) -> 'a -> 'b list
-   val unfoldl' : ('a -> ('b * 'a) Option.t) -> 'a -> 'b list * 'a
-   val unfoldr : ('a -> ('b * 'a) Option.t) -> 'a -> 'b list
-   val unfoldr' : ('a -> ('b * 'a) Option.t) -> 'a -> 'b list * 'a
+   val unfoldl : ('a -> ('b * 'a) Option.t) -> 'a -> 'b t
+   val unfoldl' : ('a -> ('b * 'a) Option.t) -> 'a -> 'b t * 'a
+   val unfoldr : ('a -> ('b * 'a) Option.t) -> 'a -> 'b t
+   val unfoldr' : ('a -> ('b * 'a) Option.t) -> 'a -> 'b t * 'a
 
    (** == Extracting sublists == *)
 
@@ -138,11 +148,10 @@
     * contains elements in the equivalence class induced by {eq}.  
     *)
 
-   val nubByEq : 'a BinPr.t -> 'a t -> 'a t
+   val nubByEq : 'a BinPr.t -> 'a t UnOp.t
    (** 
     * {nubByEq eq xs} removes duplicates in {xs} based upon the 
     * equivalence class specified by {eq}.  It preserves the ordering of 
     * the elements in {xs}.
     *)
-
 end




More information about the MLton-commit mailing list