github ↗
docs menu

Module Rune.Total

Write-only sums.

#type ('v, 's, +'d) t

The type for totals of ('v, 's, 'd) Nx.t values. 'd is covariant, so a total made at a library's top level serves every device set: let t : (float, Nx.float64_elt, 'd) Rune.Total.t = Rune.Total.make (). A collect's init fixes the set of the additions it receives.

#val make : unit -> ('v, 's, 'd) t

make () is a fresh total, distinct from every other.

#val add : ('v, 's, 'd) t -> ('v, 's, 'd) Nx.t -> unit

add t v adds v to the innermost open collect of t, and does nothing if none is open.

Raises Invalid_argument naming both sets if v lies on another set than the init of the collect it reaches, or naming both shapes if v's shape is not init's.

#val collect : ('v, 's, 'd) t -> init:('v, 's, 'd) Nx.t -> (unit -> 'r) -> 'r * ('v, 's, 'd) Nx.t

collect t ~init f is (f (), total), total being init plus each addition to t that reaches it, in the order they reach it, and init itself if none does. If f raises, collect raises the same exception, with the backtrace of its raise.