| 1 |
(* Copyright (C) 1999-2005 Henry Cejtin, Matthew Fluet, Suresh |
| 2 |
* Jagannathan, and Stephen Weeks. |
| 3 |
* Copyright (C) 1997-2000 NEC Research Institute. |
| 4 |
* |
| 5 |
* MLton is released under a HPND-style license. |
| 6 |
* See the file MLton-LICENSE for details. |
| 7 |
*) |
| 8 |
|
| 9 |
structure Option: OPTION = |
| 10 |
struct |
| 11 |
|
| 12 |
datatype option = datatype option |
| 13 |
|
| 14 |
exception Option |
| 15 |
|
| 16 |
fun map f = |
| 17 |
fn NONE => NONE |
| 18 |
| SOME a => SOME (f a) |
| 19 |
|
| 20 |
fun app f z = (ignore (map f z); ()) |
| 21 |
|
| 22 |
fun compose (f, g) c = map f (g c) |
| 23 |
|
| 24 |
val join = |
| 25 |
fn NONE => NONE |
| 26 |
| SOME v => v |
| 27 |
|
| 28 |
fun mapPartial f = join o (map f) |
| 29 |
|
| 30 |
fun composePartial (f, g) = (mapPartial f) o g |
| 31 |
|
| 32 |
fun filter f a = if f a then SOME a else NONE |
| 33 |
|
| 34 |
fun getOpt (z, a) = |
| 35 |
case z of |
| 36 |
NONE => a |
| 37 |
| SOME v => v |
| 38 |
|
| 39 |
val isSome = |
| 40 |
fn NONE => false |
| 41 |
| SOME _ => true |
| 42 |
|
| 43 |
val valOf = |
| 44 |
fn NONE => raise Option |
| 45 |
| SOME v => v |
| 46 |
|
| 47 |
end |
| 48 |
|
| 49 |
structure OptionGlobal: OPTION_GLOBAL = Option |
| 50 |
open OptionGlobal |