···1+{0 OxCaml Interactive Demo}
2+3+@x-ocaml.universe ./universe-oxcaml
4+@x-ocaml.worker ./universe-oxcaml/worker.js
5+6+This page demonstrates OxCaml language extensions running interactively
7+in the browser via [x-ocaml] and [js_top_worker].
8+9+{1 List Comprehensions}
10+11+OxCaml adds Python/Haskell-style list and array comprehensions:
12+13+{@ocaml[
14+let squares = [ x * x for x = 1 to 10 ]
15+16+let () = List.iter (fun x -> Printf.printf "%d " x) squares
17+]}
18+19+{@ocaml[
20+let evens = [ x for x = 1 to 20 when x mod 2 = 0 ]
21+22+let () = Printf.printf "Evens: %s\n"
23+ (String.concat ", " (List.map string_of_int evens))
24+]}
25+26+Nested comprehensions produce the cartesian product:
27+28+{@ocaml[
29+let pairs = [ (x, y) for x = 1 to 3 for y = 1 to 3 when x <> y ]
30+31+let () = List.iter (fun (x, y) -> Printf.printf "(%d,%d) " x y) pairs
32+]}
33+34+{1 Array Comprehensions}
35+36+Array comprehensions create arrays using the same syntax as list comprehensions:
37+38+{@ocaml[
39+let squares = [| x * x for x = 1 to 10 |]
40+41+let () = Array.iter (fun x -> Printf.printf "%d " x) squares
42+]}
43+44+{@ocaml[
45+let fibs =
46+ let a = Array.make 10 0 in
47+ a.(0) <- 1; a.(1) <- 1;
48+ for i = 2 to 9 do a.(i) <- a.(i-1) + a.(i-2) done;
49+ [| a.(i) for i = 0 to 9 |]
50+51+let () = Array.iter (fun x -> Printf.printf "%d " x) fibs
52+]}
53+54+{1 Let Mutable}
55+56+[let mutable] provides mutable local variables without heap allocation:
57+58+{@ocaml[
59+let triangle n =
60+ let mutable total = 0 in
61+ for i = 1 to n do
62+ total <- total + i
63+ done;
64+ total
65+66+let () = Printf.printf "triangle 10 = %d\n" (triangle 10)
67+]}
68+69+{@ocaml[
70+let fizzbuzz n =
71+ let mutable result = [] in
72+ for i = n downto 1 do
73+ let s = match i mod 3, i mod 5 with
74+ | 0, 0 -> "FizzBuzz"
75+ | 0, _ -> "Fizz"
76+ | _, 0 -> "Buzz"
77+ | _ -> string_of_int i
78+ in
79+ result <- s :: result
80+ done;
81+ result
82+83+let () = print_endline (String.concat " " (fizzbuzz 15))
84+]}
+36
doc/demo4_crossorigin.mld
···000000000000000000000000000000000000
···1+{0 Cross-Origin Demo}
2+3+@x-ocaml.universe http://localhost:9090/universe
4+@x-ocaml.worker http://localhost:9090/universe/worker.js
5+6+This page demonstrates {b cross-origin} loading of OCaml universes.
7+The page is served from [localhost:8080] while the worker and libraries
8+are loaded from [localhost:9090], exercising the blob: URL worker
9+creation and sync XHR + eval library loading code paths.
10+11+{1 Basic Expression}
12+13+{@ocaml[
14+1 + 2 * 3
15+]}
16+17+{@ocaml[
18+let greet name = Printf.sprintf "Hello, %s!" name
19+20+let () = print_endline (greet "Cross-Origin World")
21+]}
22+23+{1 Loading a Library}
24+25+{@ocaml[
26+#require "yojson"
27+]}
28+29+{@ocaml[
30+let json = `Assoc [
31+ ("origin", `String "cross-origin");
32+ ("port", `Int 9090)
33+]
34+35+let () = print_endline (Yojson.Safe.pretty_to_string json)
36+]}
+46
doc/demo5_multiverse.mld
···0000000000000000000000000000000000000000000000
···1+{0 Multiverse Demo}
2+3+@x-ocaml.universe http://localhost:9090/yojson
4+@x-ocaml.worker http://localhost:9090/worker.js
5+6+This page demonstrates a {b multiverse} layout where each package is
7+built and hosted independently. The universe URL points at
8+[localhost:9090/yojson], which contains only yojson's own artifacts.
9+Stdlib is discovered automatically via the ["universes"] link in
10+yojson's [findlib_index.json]:
11+12+{v
13+yojson/findlib_index.json:
14+ {"meta_files": ["lib/yojson/META"], "universes": ["../stdlib"]}
15+v}
16+17+This is how a large-scale host like [ocaml.org] would serve packages:
18+each package is a small self-contained directory, with links to its
19+dependencies.
20+21+{1 Basic Expression}
22+23+{@ocaml[
24+1 + 2 * 3
25+]}
26+27+{@ocaml[
28+let greet name = Printf.sprintf "Hello, %s!" name
29+30+let () = print_endline (greet "Multiverse World")
31+]}
32+33+{1 Loading a Library}
34+35+{@ocaml[
36+#require "yojson"
37+]}
38+39+{@ocaml[
40+let json = `Assoc [
41+ ("source", `String "multiverse");
42+ ("linked_universes", `Int 2)
43+]
44+45+let () = print_endline (Yojson.Safe.pretty_to_string json)
46+]}