aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/future.ml
diff options
context:
space:
mode:
authorGravatar Enrico Tassi <Enrico.Tassi@inria.fr>2013-12-24 18:08:05 +0100
committerGravatar Enrico Tassi <Enrico.Tassi@inria.fr>2013-12-24 18:23:41 +0100
commit29969434c2b5625273e742d01cd7662c9db47d11 (patch)
treed84fb690dd14d25b50f2e8b98bd7901704513eaa /lib/future.ml
parentc59a8d40e80f3d00081e26739a5aa5eba01269e0 (diff)
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).
Diffstat (limited to 'lib/future.ml')
-rw-r--r--lib/future.ml17
1 files changed, 10 insertions, 7 deletions
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