package batteries
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=e4b70d1a716f0aaba36f419f618d0a2e
sha512=a31f1f8cf2c7c3c6c757f3bfae98ff61bb32bab6a1f1e215937df42bcfa447aad41a37edb28d7bcecb88b3838ed8bd57142bcf8e2d28e09bb538055fd8a3b72d
doc/batteries.unthreaded/BatRef/index.html
Module BatRef
Operations on references.
References are mutable values, i.e. "variables" which may actually change value during their life-time, as variables in imperative languages. References can be understood as 1-cell arrays and are typically used to implement imperative algorithms in OCaml.
References are useful but don't abuse them.
type 'a t = 'a refThe type of references.
val ref : 'a -> 'a refReturn a fresh reference containing the given value.
val (!) : 'a ref -> 'a!r returns the current contents of reference r. Equivalent to fun r -> r.contents.
val (:=) : 'a ref -> 'a -> unitr := a stores the value of a in reference r. Equivalent to fun r v -> r.contents <- v.
val set : 'a ref -> 'a -> unitAs :=
val get : 'a ref -> 'aAs !
val pre : 'a ref -> ('a -> 'a) -> 'aPerform an operation on a reference and return the new value of that reference.
For instance, if x is a reference to 1, pre x ( ( + ) 1) returns 2 and sets x to 2.
val post : 'a ref -> ('a -> 'a) -> 'aPerform an operation on a reference and return the previous value of that reference.
For instance, if x is a reference to 1, post x ( ( + ) 1) returns 1 and sets x to 2.
val post_incr : int ref -> intIncrement an integer, return the old value.
Comparable to C or Java's i++.
val post_decr : int ref -> intDecrement an integer, return the old value.
Comparable to C or Java 's i--.
val pre_incr : int ref -> intIncrement an integer, return the new value.
Comparable to C or Java's ++i.
val pre_decr : int ref -> intIncrement an integer, return the new value.
Comparable to C or Java's --i.
val protect : 'a ref -> 'a -> (unit -> 'b) -> 'bAssign a reference temporarily.
protect r v body sets the value of r to v and executes body. Once body has been executed, whether termination happens as a consequence of regular evaluation or exception, the previous value of r is restored.
val toggle : bool ref -> unitInvert the boolean stored in the reference
val oset : 'a option ref -> 'a -> unitSet the given option ref to Some x
val oget_exn : 'a option ref -> 'aGet a value from an option ref;
Boilerplate code
val print :
('b BatInnerIO.output -> 'a -> unit) ->
'b BatInnerIO.output ->
'a t ->
unitGiven a printing function for the value in the ref, produce a printing function for the ref.
Example: IO.to_string (Ref.print Int.print) (ref 20) = "20"
val compare : 'a BatOrd.comp -> 'a ref BatOrd.compGiven a comparison function, produce a comparison function for refs of that type.
Example: let a = ref 10 and b = ref 20 in Ref.compare Int.compare a b = -1
val ord : 'a BatOrd.ord -> 'a ref BatOrd.ordGiven an ordering function, produce an ordering function for refs of that type.
Example: let a = ref 10 and b = ref 20 in Ref.ord Int.ord a b = Ord.Lt