Skip to content

Commit e013a3d

Browse files
committed
some additional functionality needed by shwestrick/parse-sml
1 parent faeeb59 commit e013a3d

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

doc/CommandLineArgs.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ For reals, this uses `Real.fromString`.
2525

2626
For bools, the value must be either `true` or `false`.
2727

28+
29+
```sml
30+
val parseStrings: string -> string list
31+
```
32+
33+
`parseStrings key` looks for every `-<key> <value>` in the command-line
34+
arguments and returns a list all such `<value>` found.
35+
36+
For example, if `-arg a -arg b -arg c -arg d` is passed at the command line,
37+
then `parseStrings "arg"` returns `["a", "b", "c", "d"]`.
38+
2839
```sml
2940
val parseFlag: string -> bool
3041
```

doc/Seq.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ Converts a list into a sequence.
123123
Linear work and span.
124124

125125

126+
```sml
127+
val fromRevList: 'a list -> 'a seq
128+
```
129+
130+
Reverse a list and convert it into a sequence.
131+
Linear work and span.
132+
133+
126134
```sml
127135
val toList: 'a seq -> 'a list
128136
```

lib/github.com/mpllang/mpllib/ArraySequence.sml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ struct
9898
tabulate (fn i => nth s (length s - 1 - i)) (length s)
9999

100100

101+
(** TODO: make faster *)
102+
fun fromRevList list = rev (fromList list)
103+
104+
101105
fun append (s, t) =
102106
let
103107
val (ns, nt) = (length s, length t)
@@ -133,16 +137,16 @@ struct
133137

134138

135139
fun iteratePrefixes f b s =
136-
let
140+
let
137141
val prefixes = alloc (length s)
138-
fun g ((i, b), a) =
139-
let
142+
fun g ((i, b), a) =
143+
let
140144
val _ = A.update (prefixes, i, b)
141145
in
142146
(i+1, f (b, a))
143147
end
144148
val (_, r) = iterate g (0, b) s
145-
in
149+
in
146150
(AS.full prefixes, r)
147151
end
148152

@@ -173,7 +177,7 @@ struct
173177
(* Assumes that the predicate p is pure *)
174178
AS.full (SeqBasis.filter GRAN (0, length s) (nth s) (p o nth s))
175179

176-
fun filterSafe p s =
180+
fun filterSafe p s =
177181
(* Does not assume that the predicate p is pure *)
178182
AS.full (SeqBasis.tabFilter GRAN (0, length s) (fn i => if p (nth s i) then SOME (nth s i) else NONE))
179183

lib/github.com/mpllang/mpllib/CommandLineArgs.sml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ sig
77
val parseReal: string -> real -> real
88
val parseBool: string -> bool -> bool
99

10+
(** Look for every instance of -K V and return seq of the Vs.
11+
* For example, if this is given on the commandline:
12+
* -arg a -arg b -arg c -arg d
13+
* then
14+
* parseStrings "arg" ==> ["a", "b", "c", "d"]
15+
*)
16+
val parseStrings: string -> string list
17+
1018
(* parseFlag K returns true if --K given on command-line *)
1119
val parseFlag: string -> bool
1220

@@ -51,6 +59,17 @@ struct
5159
| SOME [] => die ("Missing argument of \"-" ^ key ^ "\" ")
5260
| SOME (s :: _) => s
5361

62+
fun parseStrings key =
63+
let
64+
fun loop args =
65+
case search ("-" ^ key) args of
66+
NONE => []
67+
| SOME [] => die ("Missing argument of \"-" ^ key ^ "\"")
68+
| SOME (v :: args') => v :: loop args'
69+
in
70+
loop (CommandLine.arguments ())
71+
end
72+
5473
fun parseInt key default =
5574
case search ("-" ^ key) (CommandLine.arguments ()) of
5675
NONE => default

lib/github.com/mpllang/mpllib/Util.sml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ sig
4343
* for lo <= i < hi, iteratively do b = f (b, i) *)
4444
val loop: (int * int) -> 'a -> ('a * int -> 'a) -> 'a
4545

46+
val all: (int * int) -> (int -> bool) -> bool
47+
val exists: (int * int) -> (int -> bool) -> bool
48+
4649
val copyListIntoArray: 'a list -> 'a array -> int -> int
4750

4851
val revMap: ('a -> 'b) -> 'a list -> 'b list
4952

5053
val intToString: int -> string
54+
55+
val equalLists: ('a * 'a -> bool) -> 'a list * 'a list -> bool
56+
5157
end =
5258
struct
5359

@@ -138,6 +144,22 @@ struct
138144
ForkJoin.parfor 4096 (0, ArraySlice.length s)
139145
(fn i => f (i, ArraySlice.sub (s, i)))
140146

147+
fun all (lo, hi) f =
148+
let
149+
fun allFrom i =
150+
(i >= hi) orelse (f i andalso allFrom (i+1))
151+
in
152+
allFrom lo
153+
end
154+
155+
fun exists (lo, hi) f =
156+
let
157+
fun existsFrom i =
158+
i < hi andalso (f i orelse existsFrom (i+1))
159+
in
160+
existsFrom lo
161+
end
162+
141163
fun copyListIntoArray xs arr i =
142164
case xs of
143165
[] => i
@@ -323,4 +345,10 @@ struct
323345
toInt v
324346
end)
325347

348+
349+
fun equalLists eq ([], []) = true
350+
| equalLists eq (x :: xs, y :: ys) =
351+
eq (x, y) andalso equalLists eq (xs, ys)
352+
| equalLists _ _ = false
353+
326354
end

0 commit comments

Comments
 (0)