diff --git a/lib/ddlog_std.dl b/lib/ddlog_std.dl index 238e5a3f2..13588ec56 100644 --- a/lib/ddlog_std.dl +++ b/lib/ddlog_std.dl @@ -181,19 +181,48 @@ function unwrap_or_default(opt: Option<'A>): 'A { option_unwrap_or_default(opt) } -function to_set(o: Option<'X>): Set<'X> = { +function to_set(o: Option<'X>): Set<'X> { match (o) { Some{x} -> set_singleton(x), None -> set_empty() } } -function to_vec(o: Option<'X>): Vec<'X> = { +function to_vec(o: Option<'X>): Vec<'X> { match (o) { Some{x} -> vec_singleton(x), None -> vec_empty() } } +/* Transforms the `Option<'T>` into a `Result<'T, 'E>`, + * mapping `Some{x}` to `Ok{v}` and `None` to `Err{e}`. */ +function ok_or(o: Option<'T>, e: 'E): Result<'T, 'E> { + match (o) { + Some{x} -> Ok{x}, + None -> Err{e} + } +} + +/* Transforms `Option<'T>` into `Result<'T, 'E>`, mapping `Some{x} + * to `Ok{v}` and `None` to `Err{e()}`. + * Function `e` is only evaluated on error. */ +function ok_or_else(o: Option<'T>, e: function(): 'E): Result<'T, 'E> { + match (o) { + Some{x} -> Ok{x}, + None -> Err{e()} + } +} + +/* Returns `None` if the option is `None`, otherwise calls `f` with the + * wrapped value and returns the result. + */ +function and_then(o: Option<'T>, f: function('T): Option<'U>): Option<'U> { + match (o) { + None -> None, + Some{x} -> f(x) + } +} + /* * Either */