39348
|
1 |
(* ========================================================================= *)
|
|
2 |
(* A POSSIBLY-INFINITE STREAM DATATYPE FOR ML *)
|
39444
|
3 |
(* Copyright (c) 2001 Joe Hurd, distributed under the BSD License *)
|
39348
|
4 |
(* ========================================================================= *)
|
|
5 |
|
|
6 |
signature Stream =
|
|
7 |
sig
|
|
8 |
|
|
9 |
(* ------------------------------------------------------------------------- *)
|
|
10 |
(* The stream type. *)
|
|
11 |
(* ------------------------------------------------------------------------- *)
|
|
12 |
|
|
13 |
datatype 'a stream = Nil | Cons of 'a * (unit -> 'a stream)
|
|
14 |
|
|
15 |
(* If you're wondering how to create an infinite stream: *)
|
|
16 |
(* val stream4 = let fun s4 () = Stream.Cons (4,s4) in s4 () end; *)
|
|
17 |
|
|
18 |
(* ------------------------------------------------------------------------- *)
|
|
19 |
(* Stream constructors. *)
|
|
20 |
(* ------------------------------------------------------------------------- *)
|
|
21 |
|
|
22 |
val repeat : 'a -> 'a stream
|
|
23 |
|
|
24 |
val count : int -> int stream
|
|
25 |
|
|
26 |
val funpows : ('a -> 'a) -> 'a -> 'a stream
|
|
27 |
|
|
28 |
(* ------------------------------------------------------------------------- *)
|
|
29 |
(* Stream versions of standard list operations: these should all terminate. *)
|
|
30 |
(* ------------------------------------------------------------------------- *)
|
|
31 |
|
|
32 |
val cons : 'a -> (unit -> 'a stream) -> 'a stream
|
|
33 |
|
|
34 |
val null : 'a stream -> bool
|
|
35 |
|
|
36 |
val hd : 'a stream -> 'a (* raises Empty *)
|
|
37 |
|
|
38 |
val tl : 'a stream -> 'a stream (* raises Empty *)
|
|
39 |
|
|
40 |
val hdTl : 'a stream -> 'a * 'a stream (* raises Empty *)
|
|
41 |
|
|
42 |
val singleton : 'a -> 'a stream
|
|
43 |
|
|
44 |
val append : 'a stream -> (unit -> 'a stream) -> 'a stream
|
|
45 |
|
|
46 |
val map : ('a -> 'b) -> 'a stream -> 'b stream
|
|
47 |
|
|
48 |
val maps :
|
|
49 |
('a -> 's -> 'b * 's) -> ('s -> 'b stream) -> 's -> 'a stream -> 'b stream
|
|
50 |
|
|
51 |
val zipwith : ('a -> 'b -> 'c) -> 'a stream -> 'b stream -> 'c stream
|
|
52 |
|
|
53 |
val zip : 'a stream -> 'b stream -> ('a * 'b) stream
|
|
54 |
|
|
55 |
val take : int -> 'a stream -> 'a stream (* raises Subscript *)
|
|
56 |
|
|
57 |
val drop : int -> 'a stream -> 'a stream (* raises Subscript *)
|
|
58 |
|
|
59 |
(* ------------------------------------------------------------------------- *)
|
|
60 |
(* Stream versions of standard list operations: these might not terminate. *)
|
|
61 |
(* ------------------------------------------------------------------------- *)
|
|
62 |
|
|
63 |
val length : 'a stream -> int
|
|
64 |
|
|
65 |
val exists : ('a -> bool) -> 'a stream -> bool
|
|
66 |
|
|
67 |
val all : ('a -> bool) -> 'a stream -> bool
|
|
68 |
|
|
69 |
val filter : ('a -> bool) -> 'a stream -> 'a stream
|
|
70 |
|
|
71 |
val foldl : ('a * 's -> 's) -> 's -> 'a stream -> 's
|
|
72 |
|
|
73 |
val concat : 'a stream stream -> 'a stream
|
|
74 |
|
|
75 |
val mapPartial : ('a -> 'b option) -> 'a stream -> 'b stream
|
|
76 |
|
|
77 |
val mapsPartial :
|
|
78 |
('a -> 's -> 'b option * 's) -> ('s -> 'b stream) -> 's ->
|
|
79 |
'a stream -> 'b stream
|
|
80 |
|
|
81 |
val mapConcat : ('a -> 'b stream) -> 'a stream -> 'b stream
|
|
82 |
|
|
83 |
val mapsConcat :
|
|
84 |
('a -> 's -> 'b stream * 's) -> ('s -> 'b stream) -> 's ->
|
|
85 |
'a stream -> 'b stream
|
|
86 |
|
|
87 |
(* ------------------------------------------------------------------------- *)
|
|
88 |
(* Stream operations. *)
|
|
89 |
(* ------------------------------------------------------------------------- *)
|
|
90 |
|
|
91 |
val memoize : 'a stream -> 'a stream
|
|
92 |
|
|
93 |
val listConcat : 'a list stream -> 'a stream
|
|
94 |
|
|
95 |
val concatList : 'a stream list -> 'a stream
|
|
96 |
|
|
97 |
val toList : 'a stream -> 'a list
|
|
98 |
|
|
99 |
val fromList : 'a list -> 'a stream
|
|
100 |
|
|
101 |
val toString : char stream -> string
|
|
102 |
|
|
103 |
val fromString : string -> char stream
|
|
104 |
|
|
105 |
val toTextFile : {filename : string} -> string stream -> unit
|
|
106 |
|
|
107 |
val fromTextFile : {filename : string} -> string stream (* line by line *)
|
|
108 |
|
|
109 |
end
|