iterfolder

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2024 License: MIT Imports: 2 Imported by: 0

README

iterfolder

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.

database usage example

See examples for a database example using pgx, embedded structs and pgx.RowToStructByName via a docker example.

Example output:

Roald Dahl cars 3 tickets 4
> {ACA-00138-A Kia Stinger}
> > c2890f7b 2023-11-03 lat/long 51.829620/-2.532015
> {BDD-00845-B Honda Insight}
> > 6b30c01e 2024-02-21 lat/long 51.726021/-1.755015
> > fafb4c88 2023-06-06 lat/long 51.824020/-2.490015
> {EEE-00744-E Volvo V90}
> > af36e4bd 2024-02-17 lat/long 51.873222/-2.859015
Gosho Aoyama cars 3 tickets 4
> {ADB-00245-A Volvo V60}
> > 61509c9b 2023-05-08 lat/long 51.847622/-2.667015
> {BBA-00043-B Chevrolet Colorado Extended Cab}
> > dc978c72 2023-10-24 lat/long 51.806824/-2.361015
> > 37c4e259 2023-05-28 lat/long 51.796421/-2.283015
> {FBA-00548-F Chevrolet Silverado 2500 HD Double Cab}
> > 1087a451 2024-06-28 lat/long 51.634422/-1.068015

License

MIT

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

func IterFolder[A, B, C comparable](abc iter.Seq[ABC[A, B, C]]) iter.Seq[Obj[A, Obj[B, C]]]

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.

type Obj

type Obj[T comparable, U any] struct {
	This T
	// contains filtered or unexported fields
}

Obj is a composable type supporting recursive iteration

func (*Obj[T, U]) Iter

func (o *Obj[T, U]) Iter() iter.Seq[U]

Iter provides an iterator over the subsidiary items contained in Obj.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL