channels

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2022 License: MIT Imports: 4 Imported by: 0

README

genesis/channels

Package channels provides generic functions for channels.

Available functions:

  1. Any
  2. All
  3. ChunkEvery
  4. Count
  5. Drop
  6. Each
  7. Filter
  8. Map
  9. Max
  10. Min
  11. Reduce
  12. Scan
  13. Sum
  14. Take
  15. Tee
  16. ToSlice
  17. Counter
  18. Exponential
  19. Iterate
  20. Range
  21. Repeat
  22. Replicate

Documentation

Overview

Package channels provides generic functions for channels.

Index

Constants

This section is empty.

Variables

View Source
var ErrEmpty = errors.New("container is empty")

ErrEmpty is an error for empty slice when it's expected to have elements

Functions

func All

func All[T any](c <-chan T, f func(el T) bool) bool

All returns true if f returns true for all elements in channel

func Any

func Any[T any](c <-chan T, f func(el T) bool) bool

Any returns true if f returns true for any element in channel

func ChunkEvery

func ChunkEvery[T any](c <-chan T, count int) chan []T

ChunkEvery returns channel with slices containing count elements each

func Count

func Count[T comparable](c <-chan T, el T) int

Count return count of el occurrences in channel.

func Counter

func Counter[T constraints.Integer](ctx context.Context, start T, step T) chan T

Counter is like Range, but infinite

func Drop

func Drop[T any](c <-chan T, n int) chan T

Drop drops first n elements from channel c and returns a new channel with the rest. It returns channel do be unblocking. If you want array instead, wrap result into TakeAll.

func Each

func Each[T any](c <-chan T, f func(el T))

Each calls f for every element in the channel

func Exponential

func Exponential[T constraints.Integer](ctx context.Context, start T, factor T) chan T

Exponential generates elements from start with multiplication of value on factor on every step

func Filter

func Filter[T any](c <-chan T, f func(el T) bool) chan T

Filter returns a new channel with elements from input channel for which f returns true

func Iterate

func Iterate[T constraints.Integer](ctx context.Context, val T, f func(val T) T) chan T

Iterate returns an infinite list of repeated applications of f to val

func Map

func Map[T any, G any](c <-chan T, f func(el T) G) chan G

Map applies f to all elements from channel and returns channel with results

func Max

func Max[T constraints.Ordered](c <-chan T) (T, error)

Max returns the maximal element from channel

func Min

func Min[T constraints.Ordered](c <-chan T) (T, error)

Min returns the minimal element from channel

func Range

func Range[T constraints.Integer](ctx context.Context, start T, end T, step T) chan T

Range generates elements from start to end with given step

func Reduce

func Reduce[T any, G any](c <-chan T, acc G, f func(el T, acc G) G) G

Reduce applies f to acc and every element from channel and returns acc

func Repeat

func Repeat[T constraints.Integer](ctx context.Context, val T) chan T

Repeat returns channel that produces val infinite times

func Replicate

func Replicate[T constraints.Integer](ctx context.Context, val T, n int) chan T

Replicate returns channel that produces val n times

func Scan

func Scan[T any, G any](c <-chan T, acc G, f func(el T, acc G) G) chan G

Scan is like Reduce, but returns slice of f results

func Sum

func Sum[T constraints.Ordered](c <-chan T) T

Sum returns sum of all elements from channel

func Take

func Take[T any](c <-chan T, count int) chan T

Take takes first count elements from the channel.

func Tee

func Tee[T any](c <-chan T, count int) []chan T

Tee returns 2 channels with elements from the input channel

func ToSlice

func ToSlice[T any](c <-chan T) []T

ToSlice returns slice with all elements from channel.

Types

This section is empty.

Jump to

Keyboard shortcuts

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