author | wenzelm |
Sat, 17 Oct 2009 14:43:18 +0200 | |
changeset 32960 | 69916a850301 |
parent 19095 | 9497f7b174be |
child 40627 | becf5d5187cc |
permissions | -rw-r--r-- |
19089 | 1 |
signature SCANNER_SEQ = |
2 |
sig |
|
3 |
type 'a seq |
|
4 |
||
5 |
val pull : 'a seq -> ('a * 'a seq) option |
|
6 |
end |
|
7 |
||
8 |
signature EXTENDED_SCANNER_SEQ = |
|
9 |
sig |
|
10 |
||
11 |
include SCANNER_SEQ |
|
12 |
||
13 |
val null : 'a seq -> bool |
|
14 |
val length : 'a seq -> int |
|
15 |
val take_at_most : 'a seq -> int -> 'a list |
|
16 |
||
17 |
end |
|
18 |
||
19 |
functor ExtendScannerSeq (structure Seq : SCANNER_SEQ) : EXTENDED_SCANNER_SEQ = |
|
20 |
struct |
|
21 |
||
22 |
type 'a seq = 'a Seq.seq |
|
23 |
||
24 |
val pull = Seq.pull |
|
25 |
||
26 |
fun null s = is_none (pull s) |
|
27 |
||
28 |
fun take_at_most s n = |
|
29 |
if n <= 0 then [] else |
|
30 |
case pull s of |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
19095
diff
changeset
|
31 |
NONE => [] |
19089 | 32 |
| SOME (x,s') => x::(take_at_most s' (n-1)) |
33 |
||
34 |
fun length' s n = |
|
35 |
case pull s of |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
19095
diff
changeset
|
36 |
NONE => n |
19089 | 37 |
| SOME (_, s') => length' s' (n+1) |
38 |
||
39 |
fun length s = length' s 0 |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
19095
diff
changeset
|
40 |
|
19089 | 41 |
end |
42 |
||
43 |
signature VECTOR_SCANNER_SEQ = |
|
44 |
sig |
|
45 |
include EXTENDED_SCANNER_SEQ |
|
46 |
||
47 |
val fromString : string -> string seq |
|
48 |
val fromVector : 'a Vector.vector -> 'a seq |
|
49 |
end |
|
50 |
||
51 |
structure VectorScannerSeq :> VECTOR_SCANNER_SEQ = |
|
52 |
struct |
|
53 |
||
54 |
structure Incubator : SCANNER_SEQ = |
|
55 |
struct |
|
56 |
||
57 |
type 'a seq = 'a Vector.vector * int * int |
|
58 |
fun pull (v, len, i) = if i >= len then NONE else SOME (Vector.sub (v, i), (v, len, i+1)) |
|
59 |
||
60 |
end |
|
61 |
||
62 |
structure Extended = ExtendScannerSeq (structure Seq = Incubator) |
|
63 |
||
64 |
open Extended |
|
65 |
||
66 |
fun fromVector v = (v, Vector.length v, 0) |
|
67 |
fun vector_from_string s = Vector.tabulate (String.size s, fn i => String.str (String.sub (s, i))) |
|
68 |
fun fromString s = fromVector (vector_from_string s) |
|
69 |
||
70 |
end |
|
71 |
||
72 |
signature LIST_SCANNER_SEQ = |
|
73 |
sig |
|
74 |
include EXTENDED_SCANNER_SEQ |
|
75 |
||
76 |
val fromString : string -> string seq |
|
77 |
val fromList : 'a list -> 'a seq |
|
78 |
end |
|
79 |
||
80 |
structure ListScannerSeq :> LIST_SCANNER_SEQ = |
|
81 |
struct |
|
82 |
||
83 |
structure Incubator : SCANNER_SEQ = |
|
84 |
struct |
|
85 |
||
86 |
type 'a seq = 'a list |
|
87 |
fun pull [] = NONE |
|
88 |
| pull (x::xs) = SOME (x, xs) |
|
89 |
||
90 |
end |
|
91 |
||
92 |
structure Extended = ExtendScannerSeq (structure Seq = Incubator) |
|
93 |
||
94 |
open Extended |
|
95 |
||
96 |
val fromList = I |
|
19095 | 97 |
val fromString = explode |
19089 | 98 |
|
99 |
end |