File: opt.ml

package info (click to toggle)
xen-api-libs 0.5.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,940 kB
  • sloc: ml: 13,925; sh: 2,930; ansic: 1,699; makefile: 1,240; python: 83
file content (76 lines) | stat: -rw-r--r-- 1,636 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
(*
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)

(* Perhaps it's better to use `option' from the ocaml-extlib extension
 * to the standard library instead?  (Although it would not suffice,
 * since it's not a super-set of our `opt'.)
 * (http://code.google.com/p/ocaml-extlib/)
 *)

open Pervasiveext

module Monad = Monad.M1.Make (struct

	type 'a m = 'a option

	let bind option f =
		match option with
			| None -> None
			| Some result -> f result

	let return x = Some x

end)

let iter f = function
	| Some x -> f x
	| None -> ()

let map f = function
	| Some x -> Some(f x)
	| None -> None

let default d = function
	| Some x -> x
	| None -> d

let unbox = function
	| Some x -> x
	| None -> raise Not_found

let is_boxed = function
	| Some _ -> true
	| None -> false

let to_list = function
	| Some x -> [x]
	| None -> []

let fold_left f accu = function
	| Some x -> f accu x
	| None -> accu

let fold_right f opt accu =
	match opt with
	| Some x -> f x accu
	| None -> accu

let join = function
	| Some (Some a) -> Some a
	| _ -> None

let of_exception f =
	try Some (f ())
	with _ -> None