[MLton-commit] r6529
Vesa Karvonen
vesak at mlton.org
Sat Apr 5 17:33:01 PST 2008
Added Iter.on for adding side-effects to an iterator.
Added Iter.inTextFile for iterating over text files.
----------------------------------------------------------------------
U mltonlib/trunk/com/ssh/extended-basis/unstable/detail/control/iter.sml
U mltonlib/trunk/com/ssh/extended-basis/unstable/public/control/iter.sig
----------------------------------------------------------------------
Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/detail/control/iter.sml
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/detail/control/iter.sml 2008-04-05 20:06:42 UTC (rev 6528)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/detail/control/iter.sml 2008-04-06 01:33:00 UTC (rev 6529)
@@ -24,6 +24,8 @@
of isFirst =>
aM (fn a => (if !isFirst then isFirst := false else e x ; e a))
+ fun on i e = map (obs e) i
+
fun unfold g s f =
case g s of NONE => () | SOME (x, s) => (f x : Unit.t ; unfold g s f)
@@ -103,15 +105,25 @@
val inWord8Array = flip Word8Array.app
val inWord8Vector = flip Word8Vector.app
- fun inDir d e = let
+ fun inImperativeStream openS closeS readS a e = let
+ val s = openS a
+ fun lp () = case readS s of NONE => () | SOME x => (e x : Unit.t ; lp ())
+ in
+ after (lp, fn () => closeS s)
+ end
+
+ local
+ open BasisTextIO
+ in
+ val lines = inputLine
+ val chars = input1
+ fun inTextFile f = Fold.wrap (((), (), chars), fn ((), (), input) =>
+ inImperativeStream openIn closeIn input f)
+ end
+ val inDir = let
open BasisOS.FileSys
- val i = openDir d
- fun lp () =
- case readDir i
- of NONE => ()
- | SOME f => (e f : Unit.t ; lp ())
in
- after (lp, fn () => closeDir i)
+ inImperativeStream openDir closeDir readDir
end
val for = id
Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/public/control/iter.sig
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/public/control/iter.sig 2008-04-05 20:06:42 UTC (rev 6528)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/public/control/iter.sig 2008-04-06 01:33:00 UTC (rev 6529)
@@ -143,6 +143,9 @@
val intersperse : 'a -> 'a t UnOp.t
(** {intersperse x [<a(0), a(1), ...>] = [<a(0), x, a(1), x, ...>]} *)
+ val on : 'a t -> 'a Effect.t -> 'a t
+ (** Apply effect on each element of the iterator. *)
+
(** == Repetition == *)
val repeat : 'a -> 'a t
@@ -196,15 +199,15 @@
type ('f, 't, 'b) mod
val From : ('f,
- (('f, 't, 'b) mod, 'd, 'r) Fold.t,
+ (('x, 't, 'b) mod, 'd, 'r) Fold.t,
(('f, 't, 'b) mod, 'd, 'r) Fold.t, 'k) Fold.s1
val To : ('t,
- (('f, 't, 'b) mod, 'd, 'r) Fold.t,
+ (('f, 'x, 'b) mod, 'd, 'r) Fold.t,
(('f, 't, 'b) mod, 'd, 'r) Fold.t, 'k) Fold.s1
val By : ('b,
- (('f, 't, 'b) mod, 'd, 'r) Fold.t,
+ (('f, 't, 'x) mod, 'd, 'r) Fold.t,
(('f, 't, 'b) mod, 'd, 'r) Fold.t, 'k) Fold.s1
(** == Iterating over Integer Ranges == *)
@@ -267,10 +270,19 @@
(** == Iterators Over Standard Sequences ==
*
* Each of the {inX} iterators iterates over all the elements in the
- * given sequence of type {X}.
+ * given sequence of type {X.t}.
*)
val inList : 'a List.t -> 'a t
+ val onList : 'a List.t -> 'a List.t t
+ (**
+ *> onList [] = [<>]
+ *> onList [x(0), x(1), ..., x(n)] =
+ *> [<[x(0), x(1), ..., x(n)],
+ *> [x(1), ..., x(n)],
+ *> ...,
+ *> [x(n)]>]
+ *)
val inArray : 'a Array.t -> 'a t
val inArraySlice : 'a ArraySlice.t -> 'a t
@@ -288,6 +300,28 @@
val inWord8Vector : Word8Vector.t -> Word8.t t
val inWord8VectorSlice : Word8VectorSlice.t -> Word8.t t
+ (** == Iterators Over Input Streams == *)
+
+ val lines : BasisTextIO.instream -> String.t Option.t
+ (** This is the same as {TextIO.inputLine}. *)
+
+ val chars : BasisTextIO.instream -> Char.t Option.t
+ (** This is the same as {TextIO.input1}. *)
+
+ val inTextFile :
+ String.t ->
+ (((Unit.t, Unit.t, BasisTextIO.instream -> Char.t Option.t) mod,
+ (Unit.t, Unit.t, BasisTextIO.instream -> 'a Option.t) mod,
+ 'a t) Fold.t, 'k) CPS.t
+ (**
+ *> inTextFile file By method $
+ *
+ * Iterates over elements input from the text {file} by the given
+ * {method}.
+ *
+ * Defaults: {By chars}
+ *)
+
val inDir : String.t -> String.t t
(**
* Iterates over the files in the specified directory. This
@@ -295,14 +329,4 @@
* calling {OS.FileSys.readDir} with a directory stream opened with
* {OS.FileSys.openDir}.
*)
-
- val onList : 'a List.t -> 'a List.t t
- (**
- *> onList [] = [<>]
- *> onList [x(0), x(1), ..., x(n)] =
- *> [<[x(0), x(1), ..., x(n)],
- *> [x(1), ..., x(n)],
- *> ...,
- *> [x(n)]>]
- *)
end
More information about the MLton-commit
mailing list