steams

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2025 License: MIT Imports: 4 Imported by: 7

README

Steams

Go functional programming library inspired (mostly) on Java Streams

Caveats

  • This library requires Go 1.23+
  • Contains several Java style streams (called steams).

Intallation

go get -u github.com/javiorfo/steams@latest

Example

More examples here
package main

import (
  "fmt"

  "github.com/javiorfo/steams"
)

var PeopleWithPets = []Person{
  {Name: "Carl", Age: 34, Pets: []Pet{}},
  {Name: "John", Age: 20, Pets: []Pet{{Name: "Bobby", Type: "DOG", Age: 2}, {Name: "Mike", Type: "DOG", Age: 12}}},
  {Name: "Grace", Age: 40, Pets: []Pet{{Name: "Pepe", Type: "DOG", Age: 4}, {Name: "Snowball", Type: "CAT", Age: 8}}},
  {Name: "Robert", Age: 40, Pets: []Pet{{Name: "Ronny", Type: "CAT", Age: 3}}},
}

func main() {
  steams.OfSlice(data.PeopleWithPets).
    Filter(func(p data.Person) bool { return p.Age > 21 }).
    Peek(func(p data.Person) { fmt.Println("After Filter => Person:", p.Name) }).
    FlatMapToAny(func(p data.Person) steams.Steam[any] {
      return steams.OfSlice(p.Pets).MapToAny(func(p data.Pet) any { return any(p) })
    }).
    Peek(func(p any) { fmt.Println("After FlatMap = Pet:", p.(data.Pet).Name) }).
    Filter(isCat).
    Peek(func(p any) { fmt.Println("After second Filter => Pet:", p.(data.Pet).Name) }).
    GetCompared(comparator).
    IfPresentOrElse(print, func() { fmt.Println("No results") })
}

func isCat(p any) bool {
  animal, ok := p.(data.Pet)
  if ok {
    if animal.Type == data.CAT {
	  return true
    }
  }
  return false
}

func comparator(a any, b any) bool {
  ageA := a.(data.Pet).Age
  ageB := b.(data.Pet).Age
  return ageA < ageB
}

func print(cat any) {
  younger := cat.(data.Pet)
  fmt.Printf("The younger cat of the list is %s, age %d", younger.Name, younger.Age)
}

Interfaces

// Steam[T] is an interface for a collection of elements of type T,
// providing various methods for functional-style processing.
type Steam[T any] interface {
  Filter(predicate func(T) bool) Steam[T]
  MapToAny(mapper func(T) any) Steam[any]
  MapToInt(mapper func(T) int) Steam[int]
  MapToString(mapper func(T) string) Steam[string]
  FilterMapToAny(predicate func(T) bool, mapper func(T) any) Steam[any]
  FilterMapToInt(predicate func(T) bool, mapper func(T) int) Steam[int]
  FilterMapToString(predicate func(T) bool, mapper func(T) string) Steam[string]
  FlatMapToAny(mapper func(T) Steam[any]) Steam[any]
  FlatMapToInt(mapper func(T) Steam[int]) Steam[int]
  FlatMapToString(mapper func(T) Steam[string]) Steam[string]
  ForEach(consumer func(T))
  ForEachWithIndex(consumer func(int, T))
  Peek(consumer func(T)) Steam[T]
  Limit(limit int) Steam[T]
  AllMatch(predicate func(T) bool) bool
  AnyMatch(predicate func(T) bool) bool
  NoneMatch(predicate func(T) bool) bool
  TakeWhile(predicate func(T) bool) Steam[T]
  DropWhile(predicate func(T) bool) Steam[T]
  Reduce(initValue T, acc func(T, T) T) T
  Reverse() Steam[T]
  Sorted(cmp func(T, T) bool) Steam[T]
  GetCompared(cmp func(T, T) bool) nilo.Optional[T]
  FindFirst() nilo.Optional[T]
  FindOne(predicate func(T) bool) nilo.Optional[T]
  Last() nilo.Optional[T]
  Position(predicate func(T) bool) nilo.Optional[int]
  Skip(n int) Steam[T]
  Count() int
  Collect() []T
}

// Steam2[K, V] is an interface for a map of elements of type K and V,
// providing various methods for functional-style processing.
type Steam2[K comparable, V any] interface {
  Filter(predicate func(K, V) bool) Steam2[K, V]
  MapToAny(mapper func(K, V) any) Steam2[K, any]
  MapToInt(mapper func(K, V) int) Steam2[K, int]
  MapToString(mapper func(K, V) string) Steam2[K, string]
  FilterMapToAny(predicate func(K, V) bool, mapper func(K, V) any) Steam2[K, any]
  FilterMapToInt(predicate func(K, V) bool, mapper func(K, V) int) Steam2[K, int]
  FilterMapToString(predicate func(K, V) bool, mapper func(K, V) string) Steam2[K, string]
  ForEach(consumer func(K, V))
  Peek(consumer func(K, V)) Steam2[K, V]
  Limit(limit int) Steam2[K, V]
  AllMatch(predicate func(K, V) bool) bool
  AnyMatch(predicate func(K, V) bool) bool
  NoneMatch(predicate func(K, V) bool) bool
  Sorted(cmp func(K, K) bool) Steam2[K, V]
  GetCompared(cmp func(K, K) bool) nilo.Optional[Pair[K, V]]
  Count() int
  Collect() map[K]V
  KeysToSteam() Steam[K]
  ValuesToSteam() Steam[V]
  ToAnySteam(mapper func(K, V) any) Steam[any]
}

Integration functions

func Of[T any](args ...T) Steam[T]
func OfSlice[T any](slice []T) Steam[T]
func OfMap[K comparable, V any](m map[K]V) Steam2[K, V]
func Mapping[T, R any](s Steam[T], mapper func(T) R) Steam[R]
func Distinct[T comparable](s Steam[T]) Steam[T]
func CollectSteamToSteam2[K comparable, V, T any](s Steam[T], keyFunc func(T) K, valueFunc func(T) V) Steam2[K, V] 
func CollectSteam2ToSteam[K comparable, V, R any](s Steam2[K, V], mapper func(K, V) R) Steam[R]
func GroupBy[K comparable, V any](s Steam[V], classifier func(V) K) Steam2[K, Steam[V]] 
func GroupByCounting[K comparable, V any](s Steam[V], classifier func(V) K) Steam2[K, int]
func Zip[T, R any](s1 Steam[T], s2 Steam[R]) Steam[struct { first  T; second R }]

Donate
  • Bitcoin (QR) 1GqdJ63RDPE4eJKujHi166FAyigvHu5R7v
  • Paypal

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindPosition

func FindPosition[T Number](p T) func(T) bool

FindPosition returns a function that checks if a given value x is equal to the specified position p. The returned function takes a single argument of type T and returns true if it matches p.

func Max

func Max[T Ordered](a, b T) bool

Max returns true if the first argument a is less than the second argument b. It is intended to be used with types that implement the Ordered interface, which allows for comparison operations.

func Min

func Min[T Ordered](a, b T) bool

Min returns true if the first argument a is greater than the second argument b. It is intended to be used with types that implement the Ordered interface, which allows for comparison operations.

func OrderAsc

func OrderAsc[T Ordered](a, b T) bool

OrderAsc compares two Ordered values in ascending order. It returns true if the first value is greater than the second.

func OrderDesc

func OrderDesc[T Ordered](a, b T) bool

OrderDesc compares two Ordered values in descending order. It returns true if the first value is less than the second.

func OrderStructAsc

func OrderStructAsc[T OrderedStruct[T]](a, b T) bool

OrderStructAsc compares two OrderedStructs in ascending order. It returns true if the first struct is greater than the second.

func OrderStructDesc

func OrderStructDesc[T OrderedStruct[T]](a, b T) bool

OrderStructDesc compares two OrderedStructs in descending order. It returns true if the first struct is less than the second.

func Println

func Println[T any](v T)

Println prints the value of the provided argument v to the standard output. It can accept any type of value due to the use of a type parameter T.

func Println2

func Println2[K comparable, T any](k K, v T)

Println prints the values of the provided arguments k and v to the standard output. It can accept a comparable K value and any type to second value due to the use of a type parameter T.

func Sum

func Sum[T Number](a, b T) T

Sum returns the sum of two numbers a and b. It is intended to be used with types that implement the Number interface, which allows for addition operations.

Types

type List

type List[T any] []T

List is a generic type that represents a slice of elements of type T. It provides methods to perform various operations on the list, following a functional programming style.

func (List[T]) AllMatch

func (list List[T]) AllMatch(predicate func(T) bool) bool

AllMatch checks if all elements in the List match the provided predicate function. It returns true if all elements match, false otherwise.

func (List[T]) AnyMatch

func (list List[T]) AnyMatch(predicate func(T) bool) bool

AnyMatch checks if any element in the List matches the provided predicate function. It returns true if at least one element matches, false otherwise.

func (List[T]) Collect

func (list List[T]) Collect() []T

Collect returns the underlying slice of the List.

func (List[T]) Count

func (list List[T]) Count() int

Count returns the number of elements in the List.

func (List[T]) DropWhile

func (list List[T]) DropWhile(predicate func(T) bool) Steam[T]

DropWhile returns a new List that skips elements from the start of the List as long as they match the provided predicate function. It includes all subsequent elements after the first non-matching element.

func (List[T]) Filter

func (list List[T]) Filter(predicate func(T) bool) Steam[T]

Filter returns a new List containing only the elements that match the provided predicate function.

func (List[T]) FilterMapToAny

func (list List[T]) FilterMapToAny(predicate func(T) bool, mapper func(T) any) Steam[any]

FilterMapToAny filters the elements based on the provided predicate and then maps the remaining elements using the provided mapper function, returning a new List of type any.

func (List[T]) FilterMapToInt added in v1.0.0

func (list List[T]) FilterMapToInt(predicate func(T) bool, mapper func(T) int) Steam[int]

FilterMapToInt filters the elements based on the provided predicate and then maps the remaining elements using the provided mapper function, returning a new List of type int.

func (List[T]) FilterMapToString added in v1.0.0

func (list List[T]) FilterMapToString(predicate func(T) bool, mapper func(T) string) Steam[string]

FilterMapToString filters the elements based on the provided predicate and then maps the remaining elements using the provided mapper function, returning a new List of type string.

func (List[T]) FindFirst

func (list List[T]) FindFirst() nilo.Optional[T]

FindFirst returns an Optional containing the first element of the List if it is present; otherwise, it returns an empty Optional.

func (List[T]) FindOne added in v1.4.0

func (list List[T]) FindOne(predicate func(T) bool) nilo.Optional[T]

FindOne returns a nilo.Optional[T] that match the given predicate function.

func (List[T]) FlatMapToAny

func (list List[T]) FlatMapToAny(mapper func(T) Steam[any]) Steam[any]

FlatMapToAny applies the provided mapper function to each element in the List, which returns a Steam, and concatenates the results into a single List of type any.

func (List[T]) FlatMapToInt added in v0.2.0

func (list List[T]) FlatMapToInt(mapper func(T) Steam[int]) Steam[int]

FlatMapToInt applies the provided mapper function to each element in the List, which returns a Steam, and concatenates the results into a single List of type int.

func (List[T]) FlatMapToString added in v0.2.0

func (list List[T]) FlatMapToString(mapper func(T) Steam[string]) Steam[string]

FlatMapToString applies the provided mapper function to each element in the List, which returns a Steam, and concatenates the results into a single List of type string.

func (List[T]) ForEach

func (list List[T]) ForEach(consumer func(T))

ForEach applies the provided consumer function to each element in the List.

func (List[T]) ForEachWithIndex added in v1.2.0

func (list List[T]) ForEachWithIndex(consumer func(int, T))

ForEachWithIndex applies the provided index and consumer function to each element in the List.

func (List[T]) GetCompared

func (list List[T]) GetCompared(cmp func(T, T) bool) nilo.Optional[T]

GetCompared returns an Optional containing the element that is compared according to the provided comparison function. If the List is empty, it returns an empty Optional.

func (List[T]) Last

func (list List[T]) Last() nilo.Optional[T]

Last returns an Optional containing the last element of the List if it is present; otherwise, it returns an empty Optional.

func (List[T]) Limit

func (list List[T]) Limit(limit int) Steam[T]

Limit restricts the number of elements in the List to the specified limit and returns a new List.

func (List[T]) MapToAny

func (list List[T]) MapToAny(mapper func(T) any) Steam[any]

MapToAny applies the provided mapper function to each element in the List and returns a new List of type any. If result to specific type is needed, use integration function Mapping[T, R](s Steam[T], mapper func(T) R)

func (List[T]) MapToInt

func (list List[T]) MapToInt(mapper func(T) int) Steam[int]

MapToInt applies the provided mapper function to each element in the List and returns a new List of integers.

func (List[T]) MapToString

func (list List[T]) MapToString(mapper func(T) string) Steam[string]

MapToString applies the provided mapper function to each element in the List and returns a new List of strings.

func (List[T]) NoneMatch

func (list List[T]) NoneMatch(predicate func(T) bool) bool

NoneMatch checks if no elements in the List match the provided predicate function. It returns true if no elements match, false otherwise.

func (List[T]) Peek

func (list List[T]) Peek(consumer func(T)) Steam[T]

Peek applies the provided consumer function to each element in the List without modifying it, and returns the original List.

func (List[T]) Position

func (list List[T]) Position(predicate func(T) bool) nilo.Optional[int]

Position returns an Optional containing the index of the first element that matches the provided predicate function; otherwise, it returns an empty Optional.

func (List[T]) Reduce

func (list List[T]) Reduce(initValue T, acc func(T, T) T) T

Reduce applies an accumulator function to the elements of the List, starting with the provided initial value. It returns the final accumulated value.

func (List[T]) Reverse

func (list List[T]) Reverse() Steam[T]

Reverse returns a new List containing the elements of the original List in reverse order.

func (List[T]) Skip

func (list List[T]) Skip(n int) Steam[T]

Skip returns a new List that skips the first n elements of the original List. If n is greater than or equal to the length of the List, it returns an empty List.

func (List[T]) Sorted

func (list List[T]) Sorted(cmp func(T, T) bool) Steam[T]

Sorted returns a new List containing the elements of the original List sorted according to the provided comparison function.

func (List[T]) TakeWhile

func (list List[T]) TakeWhile(predicate func(T) bool) Steam[T]

TakeWhile returns a new List containing elements from the start of the List as long as they match the provided predicate function. It stops including elements as soon as an element does not match.

type Map

type Map[K comparable, V any] map[K]V

Map is a generic type that represents a collection of key-value pairs, where keys are of type K and values are of type V.

func (Map[K, V]) AllMatch

func (m Map[K, V]) AllMatch(predicate func(K, V) bool) bool

AllMatch checks if all key-value pairs in the Map match the provided predicate function. It returns true if all pairs match; otherwise, it returns false.

func (Map[K, V]) AnyMatch

func (m Map[K, V]) AnyMatch(predicate func(K, V) bool) bool

AnyMatch checks if any key-value pair in the Map matches the provided predicate function. It returns true if at least one pair matches; otherwise, it returns false.

func (Map[K, V]) Collect

func (m Map[K, V]) Collect() map[K]V

Collect returns the underlying map of key-value pairs.

func (Map[K, V]) Count

func (m Map[K, V]) Count() int

Count returns the number of key-value pairs in the Map.

func (Map[K, V]) Filter

func (m Map[K, V]) Filter(predicate func(K, V) bool) Steam2[K, V]

Filter returns a new Map containing only the key-value pairs that match the provided predicate function.

func (Map[K, V]) FilterMapToAny

func (m Map[K, V]) FilterMapToAny(predicate func(K, V) bool, mapper func(K, V) any) Steam2[K, any]

FilterMapToAny filters the key-value pairs based on the provided predicate and then maps the remaining pairs using the provided mapper function, returning a new Map with values of type any.

func (Map[K, V]) FilterMapToInt added in v1.0.0

func (m Map[K, V]) FilterMapToInt(predicate func(K, V) bool, mapper func(K, V) int) Steam2[K, int]

FilterMapToInt filters the key-value pairs based on the provided predicate and then maps the remaining pairs using the provided mapper function, returning a new Map with values of type int.

func (Map[K, V]) FilterMapToString added in v1.0.0

func (m Map[K, V]) FilterMapToString(predicate func(K, V) bool, mapper func(K, V) string) Steam2[K, string]

FilterMapToString filters the key-value pairs based on the provided predicate and then maps the remaining pairs using the provided mapper function, returning a new Map with values of type string.

func (Map[K, V]) ForEach

func (m Map[K, V]) ForEach(consumer func(K, V))

ForEach applies the provided consumer function to each key-value pair in the Map.

func (Map[K, V]) GetCompared

func (m Map[K, V]) GetCompared(cmp func(K, K) bool) nilo.Optional[Pair[K, V]]

GetCompared returns an Optional containing the key-value pair that is compared according to the provided comparison function. If the Map is empty, it returns an empty Optional.

func (Map[K, V]) KeysToSteam

func (m Map[K, V]) KeysToSteam() Steam[K]

KeysToSteam returns a Steam containing all the keys from the Map.

func (Map[K, V]) Limit

func (m Map[K, V]) Limit(limit int) Steam2[K, V]

Limit restricts the number of key-value pairs in the Map to the specified limit and returns a new Map containing only the first 'limit' pairs.

func (Map[K, V]) MapToAny

func (m Map[K, V]) MapToAny(mapper func(K, V) any) Steam2[K, any]

MapToAny applies the provided mapper function to each key-value pair in the Map and returns a new Map with values of type any.

func (Map[K, V]) MapToInt

func (m Map[K, V]) MapToInt(mapper func(K, V) int) Steam2[K, int]

MapToInt applies the provided mapper function to each key-value pair in the Map and returns a new Map with values of type int.

func (Map[K, V]) MapToString

func (m Map[K, V]) MapToString(mapper func(K, V) string) Steam2[K, string]

MapToString applies the provided mapper function to each key-value pair in the Map and returns a new Map with values of type string.

func (Map[K, V]) NoneMatch

func (m Map[K, V]) NoneMatch(predicate func(K, V) bool) bool

NoneMatch checks if no key-value pairs in the Map match the provided predicate function. It returns true if no pairs match; otherwise, it returns false.

func (Map[K, V]) Peek

func (m Map[K, V]) Peek(consumer func(K, V)) Steam2[K, V]

Peek applies the provided consumer function to each key-value pair in the Map without modifying it, and returns the original Map.

func (Map[K, V]) Sorted

func (m Map[K, V]) Sorted(cmp func(K, K) bool) Steam2[K, V]

Sorted returns a new Map containing the key-value pairs sorted according to the provided comparison function. The comparison function should define the order of the keys.

func (Map[K, V]) ToAnySteam

func (m Map[K, V]) ToAnySteam(mapper func(K, V) any) Steam[any]

ToAnySteam applies the provided mapper function to each key-value pair in the Map and returns a Steam containing the mapped values of type any.

func (Map[K, V]) ValuesToSteam

func (m Map[K, V]) ValuesToSteam() Steam[V]

ValuesToSteam returns a Steam containing all the values from the Map.

type Number

type Number interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64
}

Number is a type constraint that includes all sumable types. It allows for summing various numeric types.

type Ordered

type Ordered interface {
	Number | ~string
}

Ordered is a type constraint that includes all ordered types. It allows for comparison of various numeric types and strings.

type OrderedStruct

type OrderedStruct[T any] interface {
	Compare(other T) int
}

OrderedStruct is an interface for structs that can be compared. It requires the implementation of a Compare method that returns an integer. The method should return a negative value if the receiver is less than the other, zero if they are equal, and a positive value if the receiver is greater.

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
}

Pair is a generic struct that holds a key-value pair.

type Steam

type Steam[T any] interface {
	// Filter returns a new Steam containing only the elements that match the
	// given predicate function.
	Filter(predicate func(T) bool) Steam[T]

	// MapToAny transforms each element of the Steam using the provided mapper
	// function and returns a new Steam of type any.
	// If result to specific type is needed, use integration function Mapping[T, R](s Steam[T], mapper func(T) R)
	MapToAny(mapper func(T) any) Steam[any]

	// MapToInt transforms each element of the Steam using the provided mapper
	// function and returns a new Steam of type int.
	MapToInt(mapper func(T) int) Steam[int]

	// MapToString transforms each element of the Steam using the provided mapper
	// function and returns a new Steam of type string.
	MapToString(mapper func(T) string) Steam[string]

	// FilterMapToAny combines filtering and mapping. Returns a new Steam containing
	// elements that match the predicate and are transformed by the mapper.
	FilterMapToAny(predicate func(T) bool, mapper func(T) any) Steam[any]

	// FilterMapToInt combines filtering and mapping. Returns a new Steam containing
	// elements that match the predicate and are transformed by the mapper.
	FilterMapToInt(predicate func(T) bool, mapper func(T) int) Steam[int]

	// FilterMapToString combines filtering and mapping. Returns a new Steam containing
	// elements that match the predicate and are transformed by the mapper.
	FilterMapToString(predicate func(T) bool, mapper func(T) string) Steam[string]

	// FlatMapToAny transforms each element of the Steam into a new Steam using
	// the provided mapper function and flattens the result into a single Steam.
	FlatMapToAny(mapper func(T) Steam[any]) Steam[any]

	// FlatMapToInt transforms each element of the Steam into a new Steam using
	// the provided mapper function and flattens the result into a single Steam.
	FlatMapToInt(mapper func(T) Steam[int]) Steam[int]

	// FlatMapToString transforms each element of the Steam into a new Steam using
	// the provided mapper function and flattens the result into a single Steam.
	FlatMapToString(mapper func(T) Steam[string]) Steam[string]

	// ForEach executes the provided consumer function for each element in the Steam.
	ForEach(consumer func(T))

	// ForEachWithIndex executes the provided index and consumer function for each element in the Steam.
	ForEachWithIndex(consumer func(int, T))

	// Peek allows for inspecting each element without consuming it. It returns
	// a new Steam that allows for further operations after consuming the elements.
	Peek(consumer func(T)) Steam[T]

	// Limit returns a new Steam containing only the first 'limit' elements.
	Limit(limit int) Steam[T]

	// AllMatch returns true if all elements match the given predicate.
	AllMatch(predicate func(T) bool) bool

	// AnyMatch returns true if any element matches the given predicate.
	AnyMatch(predicate func(T) bool) bool

	// NoneMatch returns true if no elements match the given predicate.
	NoneMatch(predicate func(T) bool) bool

	// TakeWhile returns a new Steam containing elements as long as they match
	// the given predicate.
	TakeWhile(predicate func(T) bool) Steam[T]

	// DropWhile returns a new Steam that skips elements as long as they match
	// the given predicate.
	DropWhile(predicate func(T) bool) Steam[T]

	// Reduce combines elements of the Steam into a single value using the provided
	// accumulator function, starting with the given initial value.
	Reduce(initValue T, acc func(T, T) T) T

	// Reverse returns a new Steam with the elements in reverse order.
	Reverse() Steam[T]

	// Sorted returns a new Steam with the elements sorted according to the
	// provided comparison function.
	Sorted(cmp func(T, T) bool) Steam[T]

	// GetCompared returns the first element that matches the comparison function
	// wrapped in a nilo.Optional[T].
	// This can be used as an implementation of Max or Min function.
	GetCompared(cmp func(T, T) bool) nilo.Optional[T]

	// FindFirst returns the first element in the Steam as a nilo.Optional[T]
	// indicating if an element exists.
	FindFirst() nilo.Optional[T]

	// FindOne returns a nilo.Optional[T] that match the given predicate function.
	FindOne(predicate func(T) bool) nilo.Optional[T]

	// Last returns an nilo.Optional of the last element in the Steam
	// indicating if an element exists.
	Last() nilo.Optional[T]

	// Position returns the index of the first element that matches the predicate
	// wrapped in a nilo.Optional[int].
	Position(predicate func(T) bool) nilo.Optional[int]

	// Skip returns a new Steam that skips the first 'n' elements.
	Skip(n int) Steam[T]

	// Count returns the number of elements in the Steam.
	Count() int

	// Collect returns a slice containing all elements in the Steam.
	Collect() []T
}

Steam[T] is an interface for a collection of elements of type T, providing various methods for functional-style processing.

func CollectSteam2ToSteam

func CollectSteam2ToSteam[K comparable, V, R any](s Steam2[K, V], mapper func(K, V) R) Steam[R]

CollectSteam2ToSteam transforms a Steam2 of key-value pairs into a Steam of type R, using the provided mapper function to convert each key-value pair into a single value of type R.

func Distinct

func Distinct[T comparable](s Steam[T]) Steam[T]

Distinct returns a new Steam containing only the unique elements from the input Steam. It uses a map to track seen elements and filters out duplicates.

func ListOf

func ListOf[T any](args ...T) Steam[T]

ListOf creates a List from a variadic list of elements of type T and returns it as a Steam.

func Mapping added in v0.2.0

func Mapping[T, R any](s Steam[T], mapper func(T) R) Steam[R]

Mapping applies the provided mapper function to each element in the List and returns a new List of type R.

func Of

func Of[T any](args ...T) Steam[T]

Of creates a Steam from a variadic list of elements of type T.

func OfSlice

func OfSlice[T any](slice []T) Steam[T]

OfSlice creates a Steam from a slice of elements of type T.

func Zip

func Zip[T, R any](s1 Steam[T], s2 Steam[R]) Steam[struct {
	first  T
	second R
}]

Zip combines two Steams into a single Steam of structs, where each struct contains one element from each input Steam. It panics if the two Steams do not have the same length.

type Steam2

type Steam2[K comparable, V any] interface {

	// Filter returns a new Steam2 instance containing only the elements that match the given predicate.
	// The predicate function takes a key and a value and returns true if the element should be included.
	Filter(predicate func(K, V) bool) Steam2[K, V]

	// MapToAny transforms the elements of the Steam2 instance using the provided mapper function,
	// which takes a key and a value and returns a value of any type.
	// The result is a new Steam2 instance with the transformed values.
	MapToAny(mapper func(K, V) any) Steam2[K, any]

	// MapToInt transforms the elements of the Steam2 instance using the provided mapper function,
	// which takes a key and a value and returns an int.
	// The result is a new Steam2 instance with the transformed integer values.
	MapToInt(mapper func(K, V) int) Steam2[K, int]

	// MapToString transforms the elements of the Steam2 instance using the provided mapper function,
	// which takes a key and a value and returns a string.
	// The result is a new Steam2 instance with the transformed string values.
	MapToString(mapper func(K, V) string) Steam2[K, string]

	// FilterMapToAny applies a predicate to filter elements and then maps the remaining elements
	// using the provided mapper function. The result is a new Steam2 instance with the transformed values.
	FilterMapToAny(predicate func(K, V) bool, mapper func(K, V) any) Steam2[K, any]

	// FilterMapToInt applies a predicate to filter elements and then maps the remaining elements
	// using the provided mapper function. The result is a new Steam2 instance with the transformed values.
	FilterMapToInt(predicate func(K, V) bool, mapper func(K, V) int) Steam2[K, int]

	// FilterMapToString applies a predicate to filter elements and then maps the remaining elements
	// using the provided mapper function. The result is a new Steam2 instance with the transformed values.
	FilterMapToString(predicate func(K, V) bool, mapper func(K, V) string) Steam2[K, string]

	// ForEach applies the provided consumer function to each element in the Steam2 instance.
	// The consumer function takes a key and a value and performs an action for each element.
	ForEach(consumer func(K, V))

	// Peek allows you to inspect each element in the Steam2 instance using the provided consumer function
	// without modifying the stream. The result is a new Steam2 instance with the same elements.
	Peek(consumer func(K, V)) Steam2[K, V]

	// Limit restricts the number of elements in the Steam2 instance to the specified limit.
	// The result is a new Steam2 instance containing only the first 'limit' elements.
	Limit(limit int) Steam2[K, V]

	// AllMatch checks if all elements in the Steam2 instance match the given predicate.
	// The predicate function takes a key and a value and returns true if the element matches.
	// It returns true if the stream is empty.
	AllMatch(predicate func(K, V) bool) bool

	// AnyMatch checks if any element in the Steam2 instance matches the given predicate.
	// The predicate function takes a key and a value and returns true if the element matches.
	// It returns false if the stream is empty.
	AnyMatch(predicate func(K, V) bool) bool

	// NoneMatch checks if no elements in the Steam2 instance match the given predicate.
	// The predicate function takes a key and a value and returns true if the element matches.
	// It returns true if the stream is empty.
	NoneMatch(predicate func(K, V) bool) bool

	// Sorted returns a new Steam2 instance with elements sorted according to the provided comparison function.
	// The comparison function takes two keys and returns true if the first key is less than the second.
	Sorted(cmp func(K, K) bool) Steam2[K, V]

	// GetCompared returns an optional Pair containing the first two elements of the stream compared
	// using the provided comparison function. If the stream has fewer than two elements, it returns an empty optional.
	GetCompared(cmp func(K, K) bool) nilo.Optional[Pair[K, V]]

	// Count returns the number of elements in the Steam2 instance.
	Count() int

	// Collect gathers all elements in the Steam2 instance into a map with keys of type K and values of type V.
	Collect() map[K]V

	// KeysToSteam returns a new Steam instance containing only the keys from the Steam2 instance.
	KeysToSteam() Steam[K]

	// ValuesToSteam returns a new Steam instance containing only the values from the Steam2 instance.
	ValuesToSteam() Steam[V]

	// ToAnySteam transforms the elements of the Steam2 instance using the provided mapper function,
	// which takes a key and a value and returns a value of any type.
	// The result is a new Steam instance with the transformed values.
	ToAnySteam(mapper func(K, V) any) Steam[any]
}

Steam2[K, V] is an interface for a map of elements of type K and V, providing various methods for functional-style processing. Steam2 is a generic interface that provides a fluent API for processing key-value pairs.

func CollectSteamToSteam2

func CollectSteamToSteam2[K comparable, V, T any](s Steam[T], keyFunc func(T) K, valueFunc func(T) V) Steam2[K, V]

CollectSteamToSteam2 transforms a Steam of type T into a Steam2 of key-value pairs, where keys and values are derived from the provided keyFunc and valueFunc. It collects elements from the input Steam and maps them to a new Steam2 instance.

func GroupBy

func GroupBy[K comparable, V any](s Steam[V], classifier func(V) K) Steam2[K, Steam[V]]

GroupBy groups elements of a Steam by a classifier function that maps each element to a key. It returns a Steam2 where each key corresponds to a Steam of elements that share that key.

func GroupByCounting

func GroupByCounting[K comparable, V any](s Steam[V], classifier func(V) K) Steam2[K, int]

GroupByCounting groups elements of a Steam by a classifier function and counts the occurrences of each key. It returns a Steam2 where each key corresponds to the count of elements that share that key.

func OfMap

func OfMap[K comparable, V any](m map[K]V) Steam2[K, V]

OfMap creates a Steam2 from a map of key-value pairs. The keys and values are derived from the provided map.

Directories

Path Synopsis
examples
advanced command
basic command
groupby command
zip command

Jump to

Keyboard shortcuts

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