Module Accessor.Index
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.mapi
(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
=
|
([]) : Base.unit t
|
(::) : 'a * 'b t -> ('a * 'b) t
A stack of indices accumulated during traversal of a data structure.
val sexp_of_t : ('a -> Ppx_sexp_conv_lib.Sexp.t) -> 'a t -> Ppx_sexp_conv_lib.Sexp.t