stream

package
v0.0.0-...-4eba188 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package stream implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Concat

func Concat[T any](a, b stream[T]) stream[T]

Concat creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream. Play: https://go.dev/play/p/HM4OlYk_OUC

Example
s1 := FromSlice([]int{1, 2, 3})
s2 := FromSlice([]int{4, 5, 6})

s := Concat(s1, s2)

data := s.ToSlice()

fmt.Println(data)
Output:

[1 2 3 4 5 6]

func FromChannel

func FromChannel[T any](source <-chan T) stream[T]

FromChannel creates stream from channel. Play: https://go.dev/play/p/9TZYugGMhXZ

Example
ch := make(chan int)
go func() {
	for i := 1; i < 4; i++ {
		ch <- i
	}
	close(ch)
}()

s := FromChannel(ch)

data := s.ToSlice()

fmt.Println(data)
Output:

[1 2 3]

func FromRange

func FromRange[T constraints.Integer | constraints.Float](start, end, step T) stream[T]

FromRange creates a number stream from start to end. both start and end are included. [start, end] Play: https://go.dev/play/p/9Ex1-zcg-B-

Example
s := FromRange(1, 5, 1)

data := s.ToSlice()
fmt.Println(data)
Output:

[1 2 3 4 5]

func FromSlice

func FromSlice[T any](source []T) stream[T]

FromSlice creates stream from slice. Play: https://go.dev/play/p/wywTO0XZtI4

Example
s := FromSlice([]int{1, 2, 3})

data := s.ToSlice()

fmt.Println(data)
Output:

[1 2 3]

func Generate

func Generate[T any](generator func() func() (item T, ok bool)) stream[T]

Generate stream where each element is generated by the provided generater function Play: https://go.dev/play/p/rkOWL1yA3j9

Example
n := 0
max := 4

generator := func() func() (int, bool) {
	return func() (int, bool) {
		n++
		return n, n < max
	}
}

s := Generate(generator)

data := s.ToSlice()

fmt.Println(data)
Output:

[1 2 3]

func Of

func Of[T any](elems ...T) stream[T]

Of creates a stream whose elements are the specified values. Play: https://go.dev/play/p/jI6_iZZuVFE

Example
s := Of(1, 2, 3)

data := s.ToSlice()

fmt.Println(data)
Output:

[1 2 3]

Types

This section is empty.

Jump to

Keyboard shortcuts

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