File: test_lazy.ml

package info (click to toggle)
js-of-ocaml 5.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 32,020 kB
  • sloc: ml: 91,250; javascript: 57,289; ansic: 315; makefile: 271; lisp: 23; sh: 6; perl: 4
file content (49 lines) | stat: -rw-r--r-- 1,153 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(* TEST *)

open Effect
open Effect.Deep

type _ t += Stop : unit t

let f count =
  let r = ref 0 in
  for i = 1 to count do
    incr r;
    if i = count / 2 then perform Stop
  done;
  !r

let _ =
  let l = lazy (f 1_000) in
  let v1 =
    try_with Lazy.force l
    { effc = fun (type a) (e : a t) ->
        match e with
        | Stop -> Some (fun (k : (a, _) continuation) -> continue k ())
        | _ -> None }
  in
  Printf.printf "%d\n" v1;
  let l2 = lazy (f 2_000) in
  let v2 =
    try_with Lazy.force l2
    { effc = fun (type a) (e : a t) ->
        match e with
        | Stop -> Some (fun (k : (a, _) continuation) ->
            let d = Domain.spawn(fun () -> continue k ()) in
            Domain.join d)
        | _ -> None }
  in
  Printf.printf "%d\n" v2;
  let l3 = lazy (f 3_000) in
  let _ =
    try_with Lazy.force l3
    { effc = fun (type a) (e : a t) ->
        match e with
        | Stop -> Some (fun _ ->
            try
              let d = Domain.spawn(fun () -> Lazy.force l3) in
              Domain.join d
            with CamlinternalLazy.Undefined -> Printf.printf "Undefined\n"; 0)
        | _ -> None }
  in
  ()