Documentation
¶
Overview ¶
Package iterutil provides a set of iterutil for various data structures.
Elements of slices as pointers ¶
The main motivation is making it easier to work with slices of large structures, where people often choose an iterator pattern, which makes a shallow copy of each struct in the slice:
var myStructs []MyStruct
for _, myStruct := range myStructs {
// myStruct is a shallow *COPY* of each element - any changes in the loop have no effect on the slice element.
}
In order to improve that, and also not have to use the throw-away `_` variable, SlicePointerValues may be used to iterate across pointers to each element in the slice:
var myStructs []MyStruct
for myStructPtr := range iterutil.SlicePointerValues(myStructs) {
// myStructPtr is a pointer to each element - any changes in the loop will be reflected in the slice element.
}
If you still need the index, you can use SlicePointers:
var myStructs []MyStruct
for i, myStructPtr := range iterutil.SlicePointers(myStructs) {
// myStructPtr is a pointer to each element - any changes in the loop will be reflected in the slice element.
// i is the index of the element.
}
Walking json ¶
The https://github.com/tidwall/gjson library is already included as a dependency. If you need to walk a json document, the WalkGjsonLeaves iterator is available, which will yield the json paths to all leaves in the document along with the gjson.Result for each leaf.
Index ¶
- func Firsts[T, U any](seq2 iter.Seq2[T, U]) iter.Seq[T]
- func Seconds[T, U any](seq2 iter.Seq2[T, U]) iter.Seq[U]
- func SlicePointerValues[Slice ~[]T, T any](s Slice) iter.Seq[*T]
- func SlicePointers[Slice ~[]T, T any](s Slice) iter.Seq2[int, *T]
- func WalkGjsonLeaves(result gjson.Result) iter.Seq2[string, gjson.Result]
- type GjsonElem
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SlicePointerValues ¶
SlicePointerValues returns an iterator that yields pointers to the elements of a slice.
func SlicePointers ¶
SlicePointers returns an iterator that yields indices and pointers to the elements of a slice.