mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
terser interface for Behavior (removed mk_ prefixes)
This commit is contained in:
parent
7eaf28490e
commit
59fb4a0d47
3 changed files with 53 additions and 48 deletions
36
behavior.ml
36
behavior.ml
|
|
@ -65,42 +65,44 @@ let strategy_random ?(proba_fail=0.05) l =
|
||||||
let t = a.(Random.int (Array.length a)) in
|
let t = a.(Random.int (Array.length a)) in
|
||||||
Some t
|
Some t
|
||||||
|
|
||||||
let mk_succeed = Succeed
|
let succeed = Succeed
|
||||||
|
|
||||||
let mk_fail = Fail
|
let fail = Fail
|
||||||
|
|
||||||
let mk_test e = Test e
|
let test e = Test e
|
||||||
|
|
||||||
let mk_test_s s = TestFun (fun () -> React.S.value s)
|
let test_fun f = TestFun f
|
||||||
|
|
||||||
let mk_test_fun f = TestFun f
|
let test_signal s = TestFun (fun () -> React.S.value s)
|
||||||
|
|
||||||
let mk_wait e = Wait e
|
let wait e = Wait e
|
||||||
|
|
||||||
let mk_timeout f = Timeout f
|
let timeout f = Sequence (false, [Timeout f; Fail])
|
||||||
|
|
||||||
let mk_do act = Do act
|
let delay f = Sequence (false, [Timeout f; Succeed])
|
||||||
|
|
||||||
let mk_do_ok act = Do (fun () -> act (); true)
|
let do_ act = Do act
|
||||||
|
|
||||||
let mk_if s then_ else_ = If (s, then_, else_)
|
let do_succeed act = Do (fun () -> act (); true)
|
||||||
|
|
||||||
let mk_sequence ?(loop=false) l =
|
let if_ s then_ else_ = If (s, then_, else_)
|
||||||
|
|
||||||
|
let sequence ?(loop=false) l =
|
||||||
assert (l <> []);
|
assert (l <> []);
|
||||||
Sequence (loop, l)
|
Sequence (loop, l)
|
||||||
|
|
||||||
let mk_select ?(strat=strategy_inorder) l =
|
let select ?(strat=strategy_inorder) l =
|
||||||
assert (l <> []);
|
assert (l <> []);
|
||||||
Select (strat, l)
|
Select (strat, l)
|
||||||
|
|
||||||
let mk_or_else t1 t2 =
|
let or_else t1 t2 =
|
||||||
mk_select ~strat:strategy_inorder [t1; t2]
|
select ~strat:strategy_inorder [t1; t2]
|
||||||
|
|
||||||
let mk_parallel ?(strat=PSForall) l =
|
let parallel ?(strat=PSForall) l =
|
||||||
assert (l <> []);
|
assert (l <> []);
|
||||||
Parallel (strat, l)
|
Parallel (strat, l)
|
||||||
|
|
||||||
let mk_closure f =
|
let closure f =
|
||||||
Closure f
|
Closure f
|
||||||
|
|
||||||
(** {2 Lightweight futures} *)
|
(** {2 Lightweight futures} *)
|
||||||
|
|
@ -247,7 +249,7 @@ let run ?delay tree =
|
||||||
| None -> failwith "Behavior.run: not delay function provided"
|
| None -> failwith "Behavior.run: not delay function provided"
|
||||||
| Some delay ->
|
| Some delay ->
|
||||||
let timeout = delay howlong in
|
let timeout = delay howlong in
|
||||||
Fut.next (E.stamp timeout false)
|
Fut.next (E.stamp timeout true)
|
||||||
end
|
end
|
||||||
| Do act ->
|
| Do act ->
|
||||||
let b = act () in
|
let b = act () in
|
||||||
|
|
|
||||||
39
behavior.mli
39
behavior.mli
|
|
@ -76,50 +76,53 @@ val strategy_random : ?proba_fail:float -> select_strategy
|
||||||
(** Randomly chooses a subtree. May fail at each point with
|
(** Randomly chooses a subtree. May fail at each point with
|
||||||
a probability of [proba_fail]. *)
|
a probability of [proba_fail]. *)
|
||||||
|
|
||||||
val mk_succeed : tree
|
val succeed : tree
|
||||||
(** Behavior that always succeeds *)
|
(** Behavior that always succeeds *)
|
||||||
|
|
||||||
val mk_fail : tree
|
val fail : tree
|
||||||
(** Behavior that always fails *)
|
(** Behavior that always fails *)
|
||||||
|
|
||||||
val mk_test : bool React.event -> tree
|
val test : bool React.event -> tree
|
||||||
(** Fails or succeeds based on the next occurrence of the event *)
|
(** Fails or succeeds based on the next occurrence of the event *)
|
||||||
|
|
||||||
val mk_test_s : bool React.signal -> tree
|
val test_fun : (unit -> bool) -> tree
|
||||||
(** Fails or succeeds based on the current signal value *)
|
|
||||||
|
|
||||||
val mk_test_fun : (unit -> bool) -> tree
|
|
||||||
(** Tests that the result of calling this function is true *)
|
(** Tests that the result of calling this function is true *)
|
||||||
|
|
||||||
val mk_wait : unit React.event -> tree
|
val test_signal : bool React.signal -> tree
|
||||||
|
(** Fails or succeeds based on the current signal value *)
|
||||||
|
|
||||||
|
val wait : unit React.event -> tree
|
||||||
(** Wait for the event to trigger, then succeed *)
|
(** Wait for the event to trigger, then succeed *)
|
||||||
|
|
||||||
val mk_timeout : float -> tree
|
val timeout : float -> tree
|
||||||
(** Fails after the given amount of seconds *)
|
(** Fails after the given amount of seconds *)
|
||||||
|
|
||||||
val mk_do : (unit -> bool) -> tree
|
val delay : float -> tree
|
||||||
|
(** Wait for the given amount of seconds, then succeed *)
|
||||||
|
|
||||||
|
val do_ : (unit -> bool) -> tree
|
||||||
(** Perform an action, then succeed iff it returned true *)
|
(** Perform an action, then succeed iff it returned true *)
|
||||||
|
|
||||||
val mk_do_ok : (unit -> unit) -> tree
|
val do_succeed : (unit -> unit) -> tree
|
||||||
(** Perform an action and succeed (unless it raises an exception) *)
|
(** Perform an action and succeed (unless it raises an exception) *)
|
||||||
|
|
||||||
val mk_if : bool React.signal -> tree -> tree -> tree
|
val if_ : bool React.signal -> tree -> tree -> tree
|
||||||
(** Conditional choice, based on the current value of the signal *)
|
(** Conditional choice, based on the current value of the signal *)
|
||||||
|
|
||||||
val mk_sequence : ?loop:bool -> tree list -> tree
|
val sequence : ?loop:bool -> tree list -> tree
|
||||||
(** Sequence of sub-trees to run *)
|
(** Sequence of sub-trees to run *)
|
||||||
|
|
||||||
val mk_select : ?strat:select_strategy -> tree list -> tree
|
val select : ?strat:select_strategy -> tree list -> tree
|
||||||
(** Choice among the subtrees. The strategy defines in which order subtrees
|
(** Choice among the subtrees. The strategy defines in which order subtrees
|
||||||
are tried. *)
|
are tried. *)
|
||||||
|
|
||||||
val mk_or_else : tree -> tree -> tree
|
val or_else : tree -> tree -> tree
|
||||||
(** Binary choice, favoring the left one *)
|
(** Binary choice, favoring the left one *)
|
||||||
|
|
||||||
val mk_parallel : ?strat:parallel_strategy -> tree list -> tree
|
val parallel : ?strat:parallel_strategy -> tree list -> tree
|
||||||
(** Run subtrees in parallel *)
|
(** Run subtrees in parallel (default strat: PSForall) *)
|
||||||
|
|
||||||
val mk_closure : (unit -> tree) -> tree
|
val closure : (unit -> tree) -> tree
|
||||||
(** Produce a tree dynamically, at each call. *)
|
(** Produce a tree dynamically, at each call. *)
|
||||||
|
|
||||||
(** {2 Lightweight futures} *)
|
(** {2 Lightweight futures} *)
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ module B = Behavior
|
||||||
|
|
||||||
let test_do () =
|
let test_do () =
|
||||||
let r = ref false in
|
let r = ref false in
|
||||||
let t = B.mk_do_ok (fun () -> r := true) in
|
let t = B.do_succeed (fun () -> r := true) in
|
||||||
let res = B.run t in
|
let res = B.run t in
|
||||||
OUnit.assert_equal true !r;
|
OUnit.assert_equal true !r;
|
||||||
OUnit.assert_equal (Some true) (React.S.value (B.Fut.wait res));
|
OUnit.assert_equal (Some true) (React.S.value (B.Fut.wait res));
|
||||||
|
|
@ -15,11 +15,11 @@ let test_seq () =
|
||||||
let l = ref [] in
|
let l = ref [] in
|
||||||
(* add int to [l] *)
|
(* add int to [l] *)
|
||||||
let add x = l := x :: !l in
|
let add x = l := x :: !l in
|
||||||
let t = B.mk_sequence
|
let t = B.sequence
|
||||||
[ B.mk_do (fun () -> add 3; true);
|
[ B.do_ (fun () -> add 3; true);
|
||||||
B.mk_do (fun () -> add 2; true);
|
B.do_ (fun () -> add 2; true);
|
||||||
B.mk_test_fun (fun () -> List.length !l = 2);
|
B.test_fun (fun () -> List.length !l = 2);
|
||||||
B.mk_do (fun () -> add 1; true);
|
B.do_ (fun () -> add 1; true);
|
||||||
] in
|
] in
|
||||||
let res = B.run t in
|
let res = B.run t in
|
||||||
OUnit.assert_equal [1;2;3] !l;
|
OUnit.assert_equal [1;2;3] !l;
|
||||||
|
|
@ -28,7 +28,7 @@ let test_seq () =
|
||||||
|
|
||||||
let test_wait () =
|
let test_wait () =
|
||||||
let e, send_e = React.E.create () in
|
let e, send_e = React.E.create () in
|
||||||
let t = B.mk_sequence [B.mk_wait e; B.mk_succeed] in
|
let t = B.sequence [B.wait e; B.succeed] in
|
||||||
let signal = B.Fut.wait (B.run t) in
|
let signal = B.Fut.wait (B.run t) in
|
||||||
OUnit.assert_equal None (React.S.value signal);
|
OUnit.assert_equal None (React.S.value signal);
|
||||||
send_e ();
|
send_e ();
|
||||||
|
|
@ -39,9 +39,9 @@ let test_parallel () =
|
||||||
let e, send_e = React.E.create () in
|
let e, send_e = React.E.create () in
|
||||||
(* forall fails *)
|
(* forall fails *)
|
||||||
let t =
|
let t =
|
||||||
B.mk_parallel ~strat:B.PSForall
|
B.parallel ~strat:B.PSForall
|
||||||
[ B.mk_sequence [B.mk_wait e; B.mk_succeed];
|
[ B.sequence [B.wait e; B.succeed];
|
||||||
B.mk_fail
|
B.fail
|
||||||
] in
|
] in
|
||||||
let signal = B.Fut.wait (B.run t) in
|
let signal = B.Fut.wait (B.run t) in
|
||||||
OUnit.assert_equal (Some false) (React.S.value signal);
|
OUnit.assert_equal (Some false) (React.S.value signal);
|
||||||
|
|
@ -49,9 +49,9 @@ let test_parallel () =
|
||||||
OUnit.assert_equal (Some false) (React.S.value signal);
|
OUnit.assert_equal (Some false) (React.S.value signal);
|
||||||
(* exists succeeds *)
|
(* exists succeeds *)
|
||||||
let t =
|
let t =
|
||||||
B.mk_parallel ~strat:B.PSExists
|
B.parallel ~strat:B.PSExists
|
||||||
[ B.mk_sequence [B.mk_wait e; B.mk_succeed];
|
[ B.sequence [B.wait e; B.succeed];
|
||||||
B.mk_fail
|
B.fail
|
||||||
] in
|
] in
|
||||||
let signal = B.Fut.wait (B.run t) in
|
let signal = B.Fut.wait (B.run t) in
|
||||||
OUnit.assert_equal None (React.S.value signal);
|
OUnit.assert_equal None (React.S.value signal);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue