Last active
April 5, 2019 07:37
-
-
Save VincentCordobes/b95ea5ca09c6ed37e5cdb9b19c30b954 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Result = struct | |
type ('a, 'b) t = Ok of 'a | Error of 'b | |
let return a = Ok a | |
let map f m = | |
match m with | |
| Ok a -> Ok (f a) | |
| Error b -> Error b | |
let map_error f m = | |
match m with | |
| Ok a -> Ok a | |
| Error b -> Error (f b) | |
let bind f m = | |
match m with | |
| Ok a -> f a | |
| Error b -> Error b | |
let ( >>= ) m f = bind f m | |
end | |
let info d = Printf.printf "%d\n" d | |
let plus a b = a + b | |
let add a b log = log a ; log b ; a + b | |
open Result | |
let _ = | |
let four = Ok 4 in | |
let three = Ok 3 in | |
let res = four | |
>>= (fun a -> three |> map (plus a)) | |
in | |
res | |
|> map print_int | |
|> map_error print_string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment