iter

package
v0.0.0-...-886be44 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForEach

func ForEach[T any](input []T, f func(*T))

ForEach executes f in parallel over each element in input.

It is safe to mutate the input parameter, which makes it possible to map in place.

ForEach always uses at most runtime.GOMAXPROCS goroutines. It takes roughly 2µs to start up the goroutines and adds an overhead of roughly 50ns per element of input. For a configurable goroutine limit, use a custom Iterator.

func ForEachIdx

func ForEachIdx[T any](input []T, f func(int, *T))

ForEachIdx is the same as ForEach except it also provides the index of the element to the callback.

func Map

func Map[T, R any](input []T, f func(*T) R) []R

Map applies f to each element of input, returning the mapped result.

Map always uses at most runtime.GOMAXPROCS goroutines. For a configurable goroutine limit, use a custom Mapper.

func MapErr

func MapErr[T, R any](input []T, f func(*T) (R, error)) ([]R, error)

MapErr applies f to each element of the input, returning the mapped result and a combined error of all returned errors.

Map always uses at most runtime.GOMAXPROCS goroutines. For a configurable goroutine limit, use a custom Mapper.

Types

type Iterator

type Iterator[T any] struct {
	// MaxGoroutines controls the maximum number of goroutines
	// to use on this Iterator's methods.
	//
	// If unset, MaxGoroutines defaults to runtime.GOMAXPROCS(0).
	MaxGoroutines int
}

Iterator can be used to configure the behaviour of ForEach and ForEachIdx. The zero value is safe to use with reasonable defaults.

Iterator is also safe for reuse and concurrent use.

Example
package main

import (
	"fmt"

	"github.com/zcong1993/conc/iter"
)

func main() {
	input := []int{1, 2, 3, 4}
	iterator := iter.Iterator[int]{
		MaxGoroutines: len(input) / 2,
	}

	iterator.ForEach(input, func(v *int) {
		if *v%2 != 0 {
			*v = -1
		}
	})

	fmt.Println(input)
}
Output:

[-1 2 -1 4]

func (Iterator[T]) ForEach

func (iter Iterator[T]) ForEach(input []T, f func(*T))

ForEach executes f in parallel over each element in input, using up to the Iterator's configured maximum number of goroutines.

It is safe to mutate the input parameter, which makes it possible to map in place.

It takes roughly 2µs to start up the goroutines and adds an overhead of roughly 50ns per element of input.

func (Iterator[T]) ForEachIdx

func (iter Iterator[T]) ForEachIdx(input []T, f func(int, *T))

ForEachIdx is the same as ForEach except it also provides the index of the element to the callback.

type Mapper

type Mapper[T, R any] Iterator[T]

Mapper is an Iterator with a result type R. It can be used to configure the behaviour of Map and MapErr. The zero value is safe to use with reasonable defaults.

Mapper is also safe for reuse and concurrent use.

Example
package main

import (
	"fmt"

	"github.com/zcong1993/conc/iter"
)

func main() {
	input := []int{1, 2, 3, 4}
	mapper := iter.Mapper[int, bool]{
		MaxGoroutines: len(input) / 2,
	}

	results := mapper.Map(input, func(v *int) bool { return *v%2 == 0 })
	fmt.Println(results)
}
Output:

[false true false true]

func (Mapper[T, R]) Map

func (m Mapper[T, R]) Map(input []T, f func(*T) R) []R

Map applies f to each element of input, returning the mapped result.

Map uses up to the configured Mapper's maximum number of goroutines.

func (Mapper[T, R]) MapErr

func (m Mapper[T, R]) MapErr(input []T, f func(*T) (R, error)) ([]R, error)

MapErr applies f to each element of the input, returning the mapped result and a combined error of all returned errors.

Map uses up to the configured Mapper's maximum number of goroutines.

Jump to

Keyboard shortcuts

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