[MLton-commit] r4852

Vesa Karvonen vesak at mlton.org
Mon Nov 20 22:32:00 PST 2006


Added maximum, minimum, unfoldl, and unfoldr.
----------------------------------------------------------------------

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 02:52:35 UTC (rev 4851)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml	2006-11-21 06:31:41 UTC (rev 4852)
@@ -8,14 +8,19 @@
    open List
    val sub = nth
    fun init l = rev (tl (rev l))
+   fun unfoldl f x = let
+      fun lp (ys, x) = case f x of NONE => ys | SOME (y, x) => lp (y::ys, x)
+   in
+      lp ([], x)
+   end
+   fun unfoldr f = rev o unfoldl f
    fun intersperse d =
        fn [] => [] | x::xs => x::rev (foldl (fn (x, ys) => x::d::ys) [] xs)
    local
-      fun cross (f, g) (x, y) = (f x, g y)
       fun headsAndTails xss =
-          cross (rev, rev)
-                (foldl (fn (h::t, (hs, ts)) => (h::hs, t::ts) | ([], ?) => ?)
-                       ([], []) xss)
+          Pair.map (rev, rev)
+                   (foldl (fn (h::t, (hs, ts)) => (h::hs, t::ts) | ([], ?) => ?)
+                          ([], []) xss)
    in
       fun transpose xss =
           case xss of
@@ -71,4 +76,6 @@
    fun alli p = Option.isNone o findi (not o p)
    fun index ? = mapi (fn ? => ?) ?
    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)
 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 02:52:35 UTC (rev 4851)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/public/list.sig	2006-11-21 06:31:41 UTC (rev 4852)
@@ -79,6 +79,16 @@
    val existsi : (Int.t * 'a) UnPr.t -> 'a t UnPr.t
    val findi : (Int.t * 'a) UnPr.t -> 'a t -> (Int.t * 'a) Option.t
 
+   (** == Special Folds == *)
+
+   val maximum : 'a Cmp.t -> 'a t -> 'a
+   val minimum : 'a Cmp.t -> 'a t -> 'a
+
+   (** == Unfolding == *)
+
+   val unfoldl : ('a -> ('b * 'a) Option.t) -> 'a -> 'b list
+   val unfoldr : ('a -> ('b * 'a) Option.t) -> 'a -> 'b list
+
    (** == Set Operations == *)
 
    val contains : ''a t -> ''a UnPr.t




More information about the MLton-commit mailing list