reducer

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 24, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package reducer provides a few often used Reducers for iterator.Reduce

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count added in v0.0.4

func Count[T any](acc *int, _ T)

Count increases acc on each call, effectively counting the number of inputs.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
	"github.com/KrischanCS/go-toolbox/iterator/reducer"
)

func main() {
	i := iterator.Of("a,", "b", "c", "d", "e")

	count := 0
	iterator.Reduce(i, &count, reducer.Count)

	fmt.Println(count)

}
Output:

5

func GroupBy

func GroupBy[KEY comparable, IN any](keyFunc func(IN) KEY) iterator.Reducer[map[KEY][]IN, IN]

GroupBy creates a iterator.Reducer which groups the given inputs by the result of the given keyFunc.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
	"github.com/KrischanCS/go-toolbox/iterator/reducer"
)

func main() {
	type person struct {
		name string
		age  int
	}

	persons := iterator.Of(
		person{"Alice", 30},
		person{"Bob", 2},
		person{"Charlie", 13},
		person{"Dory", 25},
		person{"Edward", 8},
		person{"Fiona", 2},
		person{"Greg", 5},
	)

	keyFunc := func(p person) string {
		switch {
		case p.age < 3:
			return "toddler"
		case p.age < 13:
			return "child"
		case p.age < 18:
			return "teen"
		default:
			return "adult"
		}
	}

	groups := make(map[string][]person)
	iterator.Reduce(persons, &groups, reducer.GroupBy(keyFunc))

	// Print results in a deterministic order
	for _, category := range []string{"toddler", "child", "teen", "adult"} {
		if people, exists := groups[category]; exists {
			fmt.Printf("%s: %+v\n", category, people)
		}
	}

}
Output:

toddler: [{name:Bob age:2} {name:Fiona age:2}]
child: [{name:Edward age:8} {name:Greg age:5}]
teen: [{name:Charlie age:13}]
adult: [{name:Alice age:30} {name:Dory age:25}]

func Join

func Join(separator string) iterator.Reducer[strings.Builder, string]

Join creates a iterator.Reducer which joins all given strings with the given separator.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/KrischanCS/go-toolbox/iterator"
	"github.com/KrischanCS/go-toolbox/iterator/reducer"
)

func main() {
	i := iterator.Of("a", "b", "c")

	var sb strings.Builder

	iterator.Reduce(i, &sb, reducer.Join(", "))

	fmt.Println(sb.String())

}
Output:

a, b, c

func Product

func Product[T constraints.RealNumber](acc *T, in T)

Product multiplies acc by input.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
	"github.com/KrischanCS/go-toolbox/iterator/reducer"
)

func main() {
	i := iterator.Of(3, 4)

	product := 1
	iterator.Reduce(i, &product, reducer.Product)

	fmt.Println(product)

}
Output:

12

func Sum

func Sum[T constraints.RealNumber](acc *T, in T)

Sum adds the given value to acc.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
	"github.com/KrischanCS/go-toolbox/iterator/reducer"
)

func main() {
	i := iterator.Of(1, 2, 3, 4, 5)

	sum := 0
	iterator.Reduce(i, &sum, reducer.Sum)

	fmt.Println(sum)

}
Output:

15

Types

This section is empty.

Directories

Path Synopsis
Package statistics provides reducers for gathering statistics from an iterator.
Package statistics provides reducers for gathering statistics from an iterator.

Jump to

Keyboard shortcuts

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