to

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package to provides utilities to deconstruct or consume iterators down to other types or values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chan

func Chan[T any](ctx context.Context, src iter.Seq[T], buf int) <-chan T

Chan spawns a goroutine that consumes values emitted by the source and sends them on the returned channel. The channel is created with the provided buf size.

Important note: the context cancellation is used to detect when to stop sending data on the returned channel and stop consuming the source, but since the iter.Seq API doesn't support cancellation, the goroutine spawned by Chan will only return once a yield call is performed by the source. Users of Chan should make sure that the source iterator stops when the related context is done.

func Contains

func Contains[T any](src iter.Seq[T], predicate func(T) bool) bool

Contains reports whether there is at least one value in the source iterator for which predicate returns true. It stops consuming the source at the first match.

func First

func First[T any](src iter.Seq[T], predicate func(T) bool) (t T, found bool)

First returns the first value for which predicate returns true and stops consuming src.

func Len

func Len[T any](src iter.Seq[T]) int

Len consumes the entire source and reports how many values it consumed.

func Max

func Max[T constraints.Ordered](src iter.Seq[T]) (_ T, ok bool)

Max returns the maximum element emitted by the source and reports whether at least one value was consumed.

func Min

func Min[T constraints.Ordered](src iter.Seq[T]) (_ T, ok bool)

Min returns the minimum element emitted by the source and reports whether at least one value was consumed.

func Reduce

func Reduce[T any](src iter.Seq[T],
	startAccum T,
	predicate func(accum, current T) (newAccum T, ok bool)) (lastAccum T)

Reduce scans the source and applies predicate on all elements it consumes until the predicate returns false or the source is exhausted.

It passes subsequent accumulator values to each call of the predicate and returns the last value for it.

The returned value may be the value for which predicate returned false or the last one before the source was exhausted.

Types

This section is empty.

Jump to

Keyboard shortcuts

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