Documentation
¶
Overview ¶
iterfolder is a package for go1.22 (using the "rangefunc" experiment) or higher that takes an iterator which is a composite of three types and "folds" left hand items of common value.
This might be useful, for example, for putting the results of a sql "domain aggregate" query returning rows coerced to compound struct of structs (e.g. rows of `{person}{car}{ticket}`) into a tiered iterator which can be used as follows:
for _, person := range <IterFolder.results> {
// do something with person
for _, car := person.Iter() {
// do something with car
for _, ticket := car.Iter() {
// do something with ticket
}
}
}
Note that the input iterator should be pre-sorted and that duplicate "rows" are not squashed.
Note that as of July 2024, Go templates do not yet support iterating over an iter.Seq. See https://go.dev/issue/66107.
For more information about the rangefunc experiment, see https://go.dev/wiki/RangefuncExperiment.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IterFolder ¶
IterFolder takes an iter.Seq of ABC and returns a three-level iterable of iter.Seq[Obj[A, Obj[B, C]]] which "folds" the left hand items of common value. Conceptually IterFolder translates an iterable that would provide:
1 2 3 1 2 4 2 3 4
into an iterable that provides:
1
2
3
4
2
3
4
Note that duplicate rows will provide duplicate right hand values in the output.
Example ¶
// define input types and data
type xyz = ABC[int, int, int]
input := []xyz{
xyz{1, 2, 3},
xyz{1, 2, 4},
xyz{2, 3, 5},
xyz{3, 4, 6},
xyz{3, 5, 7},
}
// construct input iterator to send to folder
xyzIter := func() iter.Seq[xyz] {
return func(yield func(xyz) bool) {
for _, this := range input {
yield(this)
}
}
}()
// run the folder
fmt.Println("")
for a := range IterFolder[int, int, int](xyzIter) {
fmt.Println(a.This)
for b := range a.Iter() {
fmt.Println(">", b.This)
for c := range b.Iter() {
fmt.Println("> >", c)
}
}
}
Output: 1 > 2 > > 3 > > 4 2 > 3 > > 5 3 > 4 > > 6 > 5 > > 7
Types ¶
type ABC ¶
type ABC[A, B, C comparable] struct { A A B B C C }
ABC is a composite struct of the three types making up the recursive object/s. ABC is the required type of Iter.Seq to provide to IterFolder.