stream

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: BSD-3-Clause Imports: 2 Imported by: 0

README

A Go library of functional-style generic filter-map-reduce operations on slices.

See example_test.go

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect

func Collect[T, A any](st Stream[T], identity A, accumulator func(A, T)) A

func Distinct

func Distinct[T comparable]() func(T) bool
Example
package main

import (
	"fmt"

	"github.com/AlexanderYastrebov/stream"
)

func print[T any](x T) {
	fmt.Println(x)
}

func main() {
	stream.Of("a", "bb", "bb", "a", "ccc", "a", "dddd").
		Filter(stream.Distinct[string]()).
		ForEach(print[string])

}
Output:

a
bb
ccc
dddd

func DistinctUsing

func DistinctUsing[T any, C comparable](mapper func(T) C) func(T) bool
Example
package main

import (
	"fmt"
	"strings"

	"github.com/AlexanderYastrebov/stream"
)

func print[T any](x T) {
	fmt.Println(x)
}

func main() {
	concat := func(s []string) string {
		return strings.Join(s, " ")
	}

	stream.Of(
		[]string{"a"},
		[]string{"b", "b"},
		[]string{"b", "b"},
		[]string{"a"},
		[]string{"c", "c", "c"},
		[]string{"a"},
		[]string{"d", "d", "d", "d"}).
		Filter(stream.DistinctUsing(concat)).
		ForEach(print[[]string])

}
Output:

[a]
[b b]
[c c c]
[d d d d]

func NaturalOrder

func NaturalOrder[T constraints.Ordered](a, b T) bool

func ReverseOrder

func ReverseOrder[T constraints.Ordered](a, b T) bool

Types

type Stream

type Stream[T any] interface {
	Filter(predicate func(element T) bool) Stream[T]
	Map(mapper func(element T) T) Stream[T]
	FlatMap(mapper func(element T) Stream[T]) Stream[T]
	Peek(consumer func(element T)) Stream[T]
	Limit(n int) Stream[T]
	Skip(n int) Stream[T]
	Sorted(less func(T, T) bool) Stream[T]
	Append(Stream[T]) Stream[T]

	ForEach(consumer func(element T))
	Reduce(accumulator func(T, T) T) (T, bool)
	AllMatch(predicate func(element T) bool) bool
	AnyMatch(predicate func(element T) bool) bool
	NoneMatch(predicate func(element T) bool) bool
	FindFirst() (T, bool)
	Min(less func(T, T) bool) (T, bool)
	Max(less func(T, T) bool) (T, bool)
	Count() int
	ToSlice() []T
	// contains filtered or unexported methods
}

func FlatMap

func FlatMap[T, R any](st Stream[T], mapper func(element T) Stream[R]) Stream[R]
Example
package main

import (
	"fmt"

	"github.com/AlexanderYastrebov/stream"
)

func print[T any](x T) {
	fmt.Println(x)
}

func main() {
	runes := func(s string) stream.Stream[rune] {
		return stream.Slice([]rune(s))
	}

	s := stream.Of("a", "bb", "ccc", "dddd")
	stream.FlatMap(s, runes).
		Limit(4).
		ForEach(print[rune])

}
Output:

97
98
98
99

func Generate

func Generate[T any](generator func() T) Stream[T]
Example
package main

import (
	"fmt"

	"github.com/AlexanderYastrebov/stream"
)

func print[T any](x T) {
	fmt.Println(x)
}

func main() {
	stream.Generate(func() string { return "a" }).
		Limit(3).
		ForEach(print[string])

}
Output:

a
a
a

func Iterate

func Iterate[T any](seed T, operator func(T) T) Stream[T]
Example
package main

import (
	"fmt"

	"github.com/AlexanderYastrebov/stream"
)

func print[T any](x T) {
	fmt.Println(x)
}

func main() {
	stream.Iterate(3, func(x int) int { return x + 2 }).
		Limit(4).
		ForEach(print[int])

}
Output:

3
5
7
9

func Map

func Map[T, R any](st Stream[T], mapper func(element T) R) Stream[R]
Example
package main

import (
	"fmt"

	"github.com/AlexanderYastrebov/stream"
)

func print[T any](x T) {
	fmt.Println(x)
}

func main() {
	stream.Of("a", "bb", "ccc").
		Map(func(s string) string {
			return s + s
		}).
		ForEach(print[string])

}
Output:

aa
bbbb
cccccc

func Of

func Of[T any](x ...T) Stream[T]

func Slice

func Slice[T any](x []T) Stream[T]

func While

func While[T any](hasNext func() bool, supplier func() T) Stream[T]

Jump to

Keyboard shortcuts

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