123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218(*
* Option - functions for the option type
* Copyright (C) 2003 Nicolas Cannasse
* 2008 David Teller (Contributor)
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)##V>=5##modulePervasives=Stdlibtype'at='aoptionletsomex=Somexletmayf=function|None->()|Somev->fv(*$T may
let x = ref 3 in may incr (Some x); !x = 4
*)letmapf=function|None->None|Somev->Some(fv)(*$T map
map succ None = None
map succ (Some 3) = (Some 4)
*)letapply=function|None->(funx->x)|Somef->f(*$T apply
apply None 3 = 3
apply (Some succ) 3 = 4
*)letfilterf=function|Somexwhenfx->Somex|_->None(*$T filter
filter (fun _ -> true) None = None
filter (fun _ -> true) (Some 3) = Some 3
filter (fun _ -> false) (Some 3) = None
*)letdefaultv=function|None->v|Somev->v(*$T default
default 3 None = 3
default 3 (Some 4) = 4
*)letdefault_delayedl=function|None->l()|Somev->v(*$T default_delayed
default_delayed (fun () -> 3) None = 3
default_delayed (fun () -> assert false) (Some 4) = 4
*)letis_some=function|None->false|_->true(*$T is_some
not (is_some None)
is_some (Some ())
*)letis_none=function|None->true|_->false(*$T is_none
is_none None
not (is_none (Some ()))
*)letget_exnse=matchswith|None->raisee|Somev->v(*$T get_exn
try get_exn None Exit with Exit -> true
try get_exn (Some true) Exit with Exit -> false
*)letgets=get_exns(Invalid_argument"Option.get")(*$T get
try get None with Invalid_argument _ -> true
try get (Some true) with Invalid_argument _ -> false
*)letmap_defaultfv=function|None->v|Somev2->fv2(*$T map_default
map_default succ 2 None = 2
map_default succ 2 (Some 3) = 4
*)letmap_default_delayedfl=function|None->l()|Somev->fv(*$T map_default_delayed
map_default_delayed succ (fun () -> 2) None = 2
map_default_delayed succ (fun () -> assert false) (Some 3) = 4
*)letcompare?(cmp=Pervasives.compare)ab=matchawithNone->(matchbwithNone->0|Some_->-1)|Somex->(matchbwithNone->1|Somey->cmpxy)(*$T compare compare (Some 0) (Some 1) < 0
compare (Some 0) (Some 0) = 0
compare (Some 0) (Some (-1)) > 0
compare None (Some ()) < 0
compare None None = 0
compare (Some ()) None > 0
compare ~cmp:(fun _ _ -> 0) (Some (fun x -> x)) (Some (fun y -> y)) = 0
*)leteq?(eq=(=))xy=matchx,ywith|None,None->true|Somea,Someb->eqab|_->false(*$T eq
eq ~eq:(fun a b -> (a land 1) = (b land 1)) (Some 1) (Some 3)
eq (Some 3) (None) = false
eq None None = true
*)letenum=function|None->BatEnum.from(fun()->raiseBatEnum.No_more_elements)|Somee->BatEnum.singletone(*$T enum
BatList.of_enum (enum None) = []
BatList.of_enum (enum (Some 3)) = [3]
*)letof_enum=BatEnum.get(*$T of_enum
of_enum (BatList.enum []) = None
let e = BatList.enum [1; 2; 3] in of_enum e = Some 1 && BatList.of_enum e = [2; 3]
*)openBatOrdletordoxy=matchx,ywith|None,None->Eq|Somex',Somey'->ox'y'|Some_,None->Gt|None,Some_->Lt(*$T ord
ord BatInt.ord (Some 1) (Some 2) = BatOrd.Lt
ord BatInt.ord (Some 1) None = BatOrd.Gt
*)letprintprint_aout=function|None->BatInnerIO.nwriteout"None"|Somex->BatPrintf.fprintfout"Some %a"print_axletmaybe_printera_printerparenout=function|None->()|Somex->a_printerparenoutxmodule Monad=structtype'am='aoptionletreturnx=Somexletbindmf=matchmwith|None->None|Somex->fxendletbind=Monad.bind(*$T bind
bind None (fun s -> Some s) = None
bind (Some ()) (fun s -> Some s) = Some ()
*)moduleLabels=structletmay~fo=mayfoletmap~fo=mapfoletmap_default~fdo=map_defaultfdoendmoduleInfix=structlet(|?)xdef=defaultdefxlet(>>=)=Monad.bindendincludeInfix