Skip to content

Commit 9f95743

Browse files
committed
Join and First
1 parent b03b4dd commit 9f95743

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

ioSystem.ml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
(** Some OCaml primitives for the extraction. *)
22
open Big_int
33

4+
module Sum = struct
5+
type ('a, 'b) t =
6+
| Left of 'a
7+
| Right of 'b
8+
9+
let destruct (s : ('a, 'b) t) (c_x : 'a -> 'c) (c_y : 'b -> 'c) : 'c =
10+
match s with
11+
| Left x -> c_x x
12+
| Right y -> c_y y
13+
end
14+
415
(** Interface to the OCaml strings. *)
516
module String = struct
617
(** Export an OCaml string. *)
@@ -24,6 +35,24 @@ end
2435
let argv : string list =
2536
Array.to_list Sys.argv
2637

38+
(** Join. *)
39+
let join (x : 'a Lwt.t) (y : 'b Lwt.t) : ('a * 'b) Lwt.t =
40+
let r_x = ref None in
41+
let r_y = ref None in
42+
Lwt.bind (Lwt.join [
43+
Lwt.bind x (fun x -> r_x := Some x; Lwt.return ());
44+
Lwt.bind y (fun y -> r_y := Some y; Lwt.return ())])
45+
(fun (_ : unit) ->
46+
match (!r_x, !r_y) with
47+
| (Some x, Some y) -> Lwt.return (x, y)
48+
| _ -> Lwt.fail_with "The join expected two answers.")
49+
50+
(** First. *)
51+
let first (x : 'a Lwt.t) (y : 'b Lwt.t) : ('a, 'b) Sum.t Lwt.t =
52+
Lwt.choose [
53+
Lwt.bind x (fun x -> Lwt.return @@ Sum.Left x);
54+
Lwt.bind y (fun y -> Lwt.return @@ Sum.Right y)]
55+
2756
(** List the files of a directory. *)
2857
let list_files (directory : string) : string list option Lwt.t =
2958
Lwt.catch (fun _ ->

0 commit comments

Comments
 (0)