Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / char-pred.sml
1 (* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8 fun contains s =
9 let
10 val a = Array.array(numChars, false)
11 val n = String.size s
12 fun loop i =
13 if i >= n then ()
14 else (Array.update(a, ord(String.sub(s, i)), true)
15 ; loop(i + 1))
16 in loop 0
17 ; fn c => Array.sub(a, ord c)
18 end
19
20 fun notContains s = not o (contains s)
21
22 fun memoize (f: char -> 'a): char -> 'a =
23 let val a = Array.tabulate(numChars, f o chr)
24 in fn c => Array.sub(a, ord c)
25 end
26
27 local
28 val not = fn f => memoize(not o f)
29 infix or andd
30 fun f or g = memoize(fn c => f c orelse g c)
31 fun f andd g = memoize(fn c => f c andalso g c)