Basic Example
Resources: OMAP Incremental Jane Street GitHub Docs
Here is an example in utop…
Setup
#require "core";;
#require "incremental";;
open Core;;
module Inc = Incremental.Make ();;
module Var = Inc.Var;;Example 1
1let amount_a_v = Var.create 3. ;;
2let amount_b_v = Var.create 5. ;;
3
4let amount_a = Var.watch amount_a_v;;
5let amount_b = Var.watch amount_b_v;;
6
7let sum = Inc.map2 amount_a amount_b ~f:( +. );;
8let sum_obs = Inc.observe sum;;
9
10Inc.stabilize();;
11
12Inc.Observer.value_exn sum_obs;;- : float = 8.Example 2
1let (x_v,y_v,z_v) = Inc.Var.(create 3., create 0.5, create 1.5);;
2let (x,y,z) = Incr.Var.(watch x_v, watch y_v, watch z_v);;
3
4let sum =
5 Inc.map2
6 z
7 (Inc.map2 x y ~f:(fun x y -> x +. y ))
8 ~f:(fun z x_and_y -> z +. x_and_y);;
9
10let sum_obs = Inc.observe sum;;
11
12Inc.stabilize ();;
13
14Inc.Observer.value_exn sum_obs;;- : float = 5.Example 2 - Using Let Syntax
Additional setup for using Let_syntax…
1#require "ppx_jane";;
2open Inc.Let_syntax;;The changes from example 2 are highlighted below.
1let (x_v,y_v,z_v) = Inc.Var.(create 3., create 0.5, create 1.5);;
2let (x,y,z) = Incr.Var.(watch x_v, watch y_v, watch z_v);;
3
4let sum =
5 let%map x_and_y =
6 let%map
7 x = x and
8 y = y in
9 x +. y
10 and
11 z = z in
12 z +. x_and_y;;
13
14let sum_obs = Inc.observe sum;;
15
16Inc.stabilize ();;
17
18Inc.Observer.value_exn sum_obs;;- : float = 5.