From 29969434c2b5625273e742d01cd7662c9db47d11 Mon Sep 17 00:00:00 2001 From: Enrico Tassi Date: Tue, 24 Dec 2013 18:08:05 +0100 Subject: Future: optional greedy chaining If a Future.computation is already a value v or an exception and is chained in a greedy way with a function f, then f v is executed immediately (or the exception is raised). --- lib/future.ml | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'lib/future.ml') diff --git a/lib/future.ml b/lib/future.ml index e9a2be989..8c40f0edb 100644 --- a/lib/future.ml +++ b/lib/future.ml @@ -37,8 +37,7 @@ type 'a comp = a mutex and a condition to make this possible *) | Closure of (unit -> 'a) | Val of 'a * Dyn.t option - | Exn of exn - (* Invariant: this exception is always "fixed" as in fix_exn *) + | Exn of exn (* Invariant: this exception is always "fixed" as in fix_exn *) type 'a comput = | Ongoing of (fix_exn * 'a comp ref) Ephemeron.key @@ -151,19 +150,23 @@ let transactify f x = let purify_future f x = if is_over x then f x else purify f x let compute x = purify_future (compute ~pure:false) x let force x = purify_future (force ~pure:false) x +let chain ?(greedy=false) ~pure x f = + let y = chain ~pure x f in + if is_over x && greedy then ignore(force y); + y let join kx = let v = force kx in kx := Finished v; v -let split2 x = - chain ~pure:true x (fun x -> fst x), - chain ~pure:true x (fun x -> snd x) +let split2 ?greedy x = + chain ?greedy ~pure:true x (fun x -> fst x), + chain ?greedy ~pure:true x (fun x -> snd x) -let map2 f x l = +let map2 ?greedy f x l = CList.map_i (fun i y -> - let xi = chain ~pure:true x (fun x -> + let xi = chain ?greedy ~pure:true x (fun x -> try List.nth x i with Failure _ | Invalid_argument _ -> Errors.anomaly (Pp.str "Future.map2 length mismatch")) in -- cgit v1.2.3