File: wc-scanStream.sml

package info (click to toggle)
mlton 20100608-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 34,980 kB
  • ctags: 69,089
  • sloc: ansic: 18,421; lisp: 2,879; makefile: 1,570; sh: 1,325; pascal: 256; asm: 97
file content (42 lines) | stat: -rw-r--r-- 1,402 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(* Written by Stephen Weeks (sweeks@sweeks.com). *)

structure Main =
   struct
      fun doit n =
         let
            open TextIO
            val f = OS.FileSys.tmpName ()
            val out = openOut f
            val _ =
               output (out,
                       String.implode
                       (List.tabulate (1000000, fn i =>
                                       if i mod 10 = 0 then #"\n" else #"a")))
            val _ = closeOut out
            fun wc f =
               let
                  val ins = openIn f
               in TextIO.scanStream
                  (fn reader => fn s =>
                   let
                      fun loop (s, ns) =
                         case reader s of
                            NONE => (closeIn ins
                                     ; if ns <> 100000
                                          then raise Fail "bug"
                                       else ()
                                     ; NONE)
                          | SOME (c, s') =>
                               loop (s', if c = #"\n" then ns + 1 else ns)
                   in loop (s, 0)
                   end)
                  ins
               end
            val rec loop =
               fn 0 => ()
                | n => (wc f; loop (n - 1))
            val _ = loop n
            val _ = OS.FileSys.remove f
         in ()
         end
   end