package accessor

  1. Overview
  2. Docs
Module type
Class type

An Index.t is a heterogeneous stack of values intended to serve as "breadcrumbs" that show how you got to some value currently being accessed inside a composite data structure. For example, if on the way in you traversed a Map.t, one component of the index might be the key of the data being accessed.

Index.t is defined to overload list syntax. This can look a little weird, because you may not have ever seen a list with different types of elements before, such as ["foo"; 5; true; 3.14]. The idea is that you will normally only ever be pattern matching on them, and in contexts where the compiler can infer that it is an Index.t, not a list. You should be able to just pattern match with a lambda, no need for a match expression or a type annotation. This is a lot more convenient than the alternative of pattern matching on deeply nested tuples.

One thing that might not be immediately intuitive is that the first element of an Index.t is the innermost index, not the outermost. Index.t is a list-like data structure, so it is much more efficient for the head, not the tail, to be the most frequently updated, as a stack.

Here is an example demonstrating the use of an Index.t that demonstrates both the pattern matching syntax and the ordering:

  (Accessor.Map.eachi @> Accessor.Map.eachi)
  (Map.singleton "foo" (Map.singleton "bar" 1))
  ~f:(fun [inner; outer] data -> outer, inner, data)
= (Map.singleton "foo" (Map.singleton "bar" ("foo", "bar", 1)))
type 'a t =
  1. | [] : Base.unit t
  2. | :: : 'a * 'b t -> ('a * 'b) t

A stack of indices accumulated during traversal of a data structure.

include Ppx_hash_lib.Hashable.S1 with type 'a t := 'a t
val hash_fold_t : (Base.Hash.state -> 'a -> Base.Hash.state) -> Base.Hash.state -> 'a t -> Base.Hash.state
val sexp_of_t : ('a -> Sexplib0.Sexp.t) -> 'a t -> Sexplib0.Sexp.t
val hd : ('hd * _) t -> 'hd
val tl : (_ * 'tl) t -> 'tl t

Innovation. Community. Security.