# HG changeset patch # User Matthias Görgens # Date 1268698901 0 # Node ID e7bfcac6a2bd93de39914af8339263a29905c9d1 # Parent dfc5d7edf140b39c0443409f332a11e7d876f0dd stdext/Either: A module for type ('a, 'b) t = Left of 'a | Right of 'b and helper functions. diff -r dfc5d7edf140 -r e7bfcac6a2bd stdext/Makefile --- a/stdext/Makefile +++ b/stdext/Makefile @@ -22,7 +22,7 @@ STDEXT_OBJS = fun listext filenameext stringext arrayext hashtblext pervasiveext threadext ring \ qring fring opt bigbuffer unixext range vIO trie config date encodings fe fecomms \ - forkhelpers gzip sha1sum zerocheck base64 backtrace tar mapext os + forkhelpers gzip sha1sum zerocheck base64 backtrace tar mapext os either INTF = $(foreach obj, $(STDEXT_OBJS),$(obj).cmi) LIBS = stdext.cma stdext.cmxa diff -r dfc5d7edf140 -r e7bfcac6a2bd stdext/either.ml --- /dev/null +++ b/stdext/either.ml @@ -0,0 +1,24 @@ +open Pervasiveext + +type ('a,'b) t = Left of 'a | Right of 'b + +let left x = Left x +let right x = Right x +let is_left = function + | Left _ -> true + | Right _ -> false +let is_right x = not ++ is_left $ x +let to_option = function + | Right x -> Some x + | Left _ -> None + +let cat_right l = Opt.cat_some ++ List.map to_option $ l + +let join = function + | Right (Right x) -> Right x + | Left x -> Left (Left x) + | Right (Left x) -> Left (Right x) + +let swap = function + | Right x -> Left x + | Left x -> Right x diff -r dfc5d7edf140 -r e7bfcac6a2bd stdext/either.mli --- /dev/null +++ b/stdext/either.mli @@ -0,0 +1,18 @@ +(* Inspired by Haskell's Either, as a way to enhance option + with information about what went wrong. + + Right is commonly used for success + Left is commonly used for failure. +*) + +type ('a,'b) t = Left of 'a | Right of 'b +val left : 'a -> ('a, 'b) t +val right: 'b -> ('a, 'b) t +val is_left: ('a, 'b) t -> bool +val is_right: ('a, 'b) t -> bool + +val cat_right: ('a, 'b) t list -> 'b list +(* Brings Right values closer to the surface. *) +val join: ('a, ('b, 'c) t) t -> (('a, 'b) t, 'c) t + +val swap : ('a, 'b) t -> ('b, 'a) t