creek

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2022 License: MIT Imports: 9 Imported by: 0

README

Creek Logo
Creek is a fully-featured Streams library for Go.
Creek creates wrappers around a specific data source (array or slice), allowing us to operate with that data source and making bulk processing convenient and fast. Creek helps you to follow the functional programming paradigms. Creek is not just a Streams library, you can work with arrays and slices more easily than ever.


Why creek?

If you've always missed the functional programming paradigms from Golang, or ever wanted to work with streams in Go, this library will totally fit your needs.
If you are from a C# or a Java background, working with this library going to be easy since creek is very similar to Linq or the Java Streams API.
Even if you just want to work with slices and arrays more conveniently and easily, but don't like functional programming, you can do that too.


Installation

Before installing this library, you need to have Go installed.

Since creek uses generics, version 1.18+ is required.

Now, you can initialize a project and use this library:

go get -u github.com/0l1v3rr/creek

Quick start

package main

import (
    "fmt"

    "github.com/0l1v3rr/creek"
)

func main() {
    arr := []int{1, 8, 2, 14, 22, 4, 7, 92}

    result := creek.FromArray(arr).Filter(func(item int) bool {
        return item > 3
    }).OrderBy().Collect()

    fmt.Println(result) // [4 7 8 14 22 92]
}

Table of Contents


Create stream

You can create a stream from almost every type of array or slice.

Empty stream

The Empty function returns an empty stream.

emptyStream := creek.Empty[int]()

Stream from regular arrays and slices

The supported types are the following:

string | byte | float32 | float64 | int | int16 | int32 | int64 | uint16 | uint32 | uint64

In order to create a stream, use the FromArray function:

// slice
arr := []int{1, 8, 2, 14, 22, 4, 7, 92}
intStream := creek.FromArray(arr)

// array
arr2 := [3]string{"One", "Two", "Three"}
stringStream := creek.FromArray(arr2[:])

Stream from parameter values

stream := creek.FromValues("Apple", "Strawberry", "Peach")

Stream from a file

The FromFile function creates a stream from a file.
The file is read line by line. Each line is an element of the stream.
Parameters:

  • os.File file
file, _ := os.Open("/path/to/file.txt")
stream := creek.FromFile(file)

Stream from struct arrays

The FromStruct function creates a new stream from the given struct array.
If the given array is not made of struct, it throws an error.

type YourStruct struct {
	Id   int64
	Name string
}

func yourFunction() {
	structArray := []YourStruct{
		{Id: 1, Name: "John"},
		{Id: 2, Name: "Will"},
		{Id: 3, Name: "Mark"},
	}

	structStream := creek.FromStructs(structArray)
}

If you use this stream, there are some functions, where you need to pass the name of the field in order to work. These functions are the following:
Average, Max, MaxIndex, Min, MinIndex, OrderBy, OrderByDescending, Sum.
For example:

structArray := []YourStruct{
    {Id: 1, Name: "John"},
    {Id: 2, Name: "Will"},
    {Id: 3, Name: "Mark"},
}

// The functions are case sensitive. 'name', 'nAme', or 'nAMe' won't work.
// Make sure you spell it correctly, otherwise it throws an error.
structStream := creek.FromStructs(structArray).OrderBy("Name")

Warning: this stream does not support 3 functions:

  • Distinct
  • Join
  • RemoveDuplicates

Important

The functions do not modify the original array or the previous stream. Most of the functions return a new stream.


Functions

All

The All function determines whether all elements of the stream satisfy the passed condition.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).All(func(item int) bool {
    return item%2 == 0
}) 
// false

Append

The Append function adds an element to the stream.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).Append(2) // [2, 7, 3, 1, 2]

AppendAt

The AppendAt function inserts the specified element at the specified position in the stream.
If the index is out of range, nothing happens.
The first parameter is the index, and the second is the item.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).AppendAt(1, 14) // [2, 14, 7, 3, 1]

AppendIf

The AppendIf function adds an element to the stream if the second parameter is true.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).AppendIf(8, len(arr) == 4) // [2, 7, 3, 1, 8]

ArrEquals

The ArrEquals function compares the stream and the passed array and returns true if they're equals.

arr := []int{2, 7, 3, 1}
result = creek.FromArray(arr).ArrEquals([]int{2, 7, 3, 2})
// false

Average

The Average function calculates the average of the stream.
This function doesn't work with strings.

arr := []int{2, 7, 3, 1, 9, 12, 5}
result := creek.FromArray(arr).Average() // 5.571428571428571

Clear

The Clear function clears every element from the stream.

arr := []int{3, 4, 1, 4, 2, 9}
result := creek.FromArray(arr).Clear() // []

Collect

The Collect function returns the modified array from the streams.

arr := []int{1, 8, 2, 14, 22, 4, 7, 92}
arrFromStream := creek.FromArray(arr).Collect() // [1, 8, 2, 14, 22, 4, 7, 92]

Contains

The Contains function checks whether the stream contains the passed item.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).Contains(2) // true

Count

The Count function returns the count of elements in the stream.

arr := []int{3, 4, 1, 4, 2, 9}
result := creek.FromArray(arr).Count() // 6

Distinct

The Distinct function filters every distinct element from the stream.

arr := []int{2, 7, 3, 1, 3, 9, 3}
result := creek.FromArray(arr).Distinct() // [2, 7, 3, 1, 9]

ElementAt

The ElementAt function is used to get an element from the stream at a particular index.
If the element is not present, it throws a panic.

arr := []int{3, 4, 1, 4, 2, 9}
result := creek.FromArray(arr).ElementAt(2) // 1

ElementAtOrElse

The ElementAtOrElse function is used to get an element from the stream at a particular index.
If the element is not present, it returns the elseValue, which is the second parameter.

arr := []int{3, 4, 1, 4, 2, 9}

result := creek.FromArray(arr).ElementAtOrElse(5, 100) // 9

result2 = creek.FromArray(arr).ElementAtOrElse(6, 100) // 100

Equals

The Equals function compares two streams and returns true if they're equals.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).Equals(creek.Stream[int]{
    Array: []int{2, 7, 3, 1},
})
// true

Filter

The Filter function leaves only those elements in the array that make the specified condition true.

arr := []string{"One", "Two", "Three"}
result := creek.FromArray(arr).Filter(func(item string) bool {
    return strings.HasPrefix(item, "T")
}) // [Two, Three]

// ----------------------------------------------

arr2 := []int{1, 4, 6, 2, 7, 3}
result2 := creek.FromArray(arr2).Filter(func(item int) bool {
    return item%2 == 0
}) // [4, 6, 2]

Find

The Find function searches for an element that matches the conditions passed and returns the first occurrence within the entire stream.

res := []int{2, 7, 3, 1, 4}
result := creek.FromArray(arr).Find(func(item int) bool {
    return item%2 == 0
})
// 2

FindIndex

The FindIndex function searches for an element that matches the conditions passed and returns the index of the first occurrence within the entire stream.

res := []int{2, 7, 3, 1, 4}
result := creek.FromArray(arr).FindIndex(func(item int) bool {
    return item%2 == 0
})
// 0

FindLast

The FindLast function searches for an element that matches the conditions passed and returns the last occurrence within the entire stream.

res := []int{2, 7, 3, 1, 4}
result := creek.FromArray(arr).FindLast(func(item int) bool {
    return item%2 == 0
})
// 4

FindLastIndex

The FindLastIndex function searches for an element that matches the conditions passed and returns the index of the last occurrence within the entire stream.

res := []int{2, 7, 3, 1, 4}
result := creek.FromArray(arr).FindLastIndex(func(item int) bool {
    return item%2 == 0
})
// 4

First

The First method returns the first element in the stream.

res := []int{2, 7, 3, 1, 4}
result := creek.FromArray(arr).First() // 2

ForEach

The ForEach method runs the specified method on every element in the Stream.
Warning: this method doesn't return anything

arr := []string{"One", "Two", "Three"}
creek.FromArray(arr).ForEach(func(item string) {
    fmt.Println(item)
})

// -- Output: --
// One
// Two
// Three

IndexOf

The IndexOf function returns the position of the first occurrence of the passed value in a stream.

arr := []int{3, 4, 1, 4, 2, 9}
result := creek.FromArray(arr).IndexOf(4) // 1

IsEmpty

The IsEmpty function checks whether the stream is empty.

arr := []int{2, 7, 3, 1}
result = creek.FromArray(arr).IsEmpty() // false

IsNotEmpty

The IsNotEmpty function checks whether the stream is not empty.

arr := []int{2, 7, 3, 1}
result = creek.FromArray(arr).IsEmpty() // true

Join

The Join function concatenates the elements of the stream to create a single string.
The passed parameter is placed between the elements.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).Join(", ") // result: "2, 7, 3, 1"

Last

The Last method returns the last element in the stream.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).Last() // 1

LastIndexOf

The LastIndexOf function returns the position of the last occurrence of the passed value in a stream.

arr := []int{3, 4, 1, 4, 2, 9}
result := creek.FromArray(arr).LastIndexOf(4) // 3

Limit

The Limit function constrains the number of elements returned by the stream.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).Limit(2) // [2, 7]

Map

The Map function creates a new stream populated with the results of calling the provided function on every element.

arr := []string{"One", "Two", "Three"}
result := creek.FromArray(arr).Map(func(item string) string {
    return strings.ToUpper(item)
}) // [ONE, TWO, THREE]

Max

The Max function returns the largest element from the stream.

arr := []int{2, 7, 3, 1, 9, 12, 5}
result := creek.FromArray(arr).Max() // 12

MaxIndex

The MaxIndex function returns the index of the largest element from the stream.

arr := []int{2, 7, 3, 1, 9, 12, 5}
result := creek.FromArray(arr).MaxIndex() // 5

Min

The Min function returns the smallest element from the stream.

arr := []int{2, 7, 3, 1, 9, 12, 5}
result := creek.FromArray(arr).Min() // 1

MinIndex

The MaxIndex function returns the index of the smallest element from the stream.

arr := []int{2, 7, 3, 1, 9, 12, 5}
result := creek.FromArray(arr).MinIndex() // 3

OrderBy

The OrderBy function sorts the stream in ascending order.

arr := []int{1, 8, 2, 14, 22, 4, 7, 92}
result := creek.FromArray(arr).OrderBy() // [1, 2, 4, 7, 8, 14, 22, 92]

OrderByDescending

The OrderByDescending function sorts the stream in descending order.

arr := []int{1, 8, 2, 14, 22, 4, 7, 92}
result := creek.FromArray(arr).OrderByDescending() // [92, 22, 14, 8, 7, 4, 2, 1]

Remove

The Remove function removes the passed element from a stream.

arr := []int{2, 7, 3, 1, 3}
result = creek.FromArray(arr).Remove(3) // [2, 7, 1]

RemoveAt

The RemoveAt function removes the item if its index matches the index passed in.
If the index is out of range, nothing happens.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).RemoveAt(2) // [2, 7, 1]

RemoveDuplicates

The RemoveDuplicates function removes every duplicate item from the stream.

arr := []int{2, 7, 3, 1, 3, 9, 3}
result := creek.FromArray(arr).RemoveDuplicates() // [2, 7, 3, 1, 9]

RemoveIf

The RemoveIf function removes the passed element from a stream if the second parameter is true.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).RemoveIf(7, len(arr) == 4) // [2, 3, 1]

RemoveWhere

The RemoveWhere function removes all the entries that satisfy the provided condition.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).RemoveWhere(func(item int) bool {
    return item > 2
}) // [2, 1]

Reverse

The Reverse function reverses the stream.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).Reverse() // [1, 3, 7, 2]

Sum

The Sum function adds up all values in a stream.

arr := []int{2, 7, 3, 1, 9, 12, 5}
result := creek.FromArray(arr).Sum() // 39

Shuffle

The Shuffle function shuffles the stream.

arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
result := creek.FromArray(arr).Shuffle() // [7, 2, 8, 1, 9, 5, 6, 3, 4]

Some

The Some function determines whether any of the elements of the stream satisfy the passed condition.

arr := []int{2, 7, 3, 1}
result := creek.FromArray(arr).Some(func(item int) bool {
    return item%2 == 0
})
// true

Wait

The Wait function pauses the current stream for the duration passed. The first and only parameter expects a value from the built-in time.Duration package.

arr := []int{3, 4, 2, 9}
result := creek.FromArray(arr).Wait(time.Second * 5) // waits for 5 seconds

Method chaining

With creek, method chaining is very straightforward and easy to read.
This code below:

  • filters the array, so only the even numbers stay
  • multiplies every number by 3
  • sorts the array in descending order
  • constrains the array to the length of 5
  • collects the array from the stream
arr := []int{2, 7, 3, 1, 12, 6, 82, 101, 23, 24, 72, 13, 7}

result := creek.FromArray(arr).Filter(func(item int) bool {
    return item%2 == 0
}).Map(func(item int) int {
    return item * 3
}).OrderByDescending().Limit(5).Collect()

// result: [246, 216, 72, 36, 18]

Download and build from source

First, clone the repository:

https://github.com/0l1v3rr/creek.git
cd creek
  • Running the tests: make test
  • Running the test in detailed version: make test_detailed
  • Test coverage: make test_coverage
  • Go formatting: make fmt

Creek VS Linq VS Java Streams API

Creek is quite similar compared to these libraries.
Here you can see the similarities between Creek, Linq, and Java Streams API.
The expected output is the following:

4
8
12
16

Linq

List<int> list = new List<int>{ 1, 7, 2, 5, 9, 6, 3, 4, 8 };
var result = list
    .Where(num => num % 2 == 0)
    .Select(num => num * 2)
    .OrderBy(num => num)
    .ToList();

result.ForEach(num => Console.WriteLine(num));

Java Streams API

List<Integer> list = List.of(1, 7, 2, 5, 9, 6, 3, 4, 8);
List<Integer> result = list
        .stream()
        .filter(num -> num % 2 == 0)
        .map(num -> num * 2)
        .sorted()
        .toList();

result.forEach(System.out::println);

Creek

list := creek.FromValues(1, 7, 2, 5, 9, 6, 3, 4, 8)
result := list.Filter(func(item int) bool {
    return item%2 == 0
}).Map(func(item int) int {
    return item * 2
}).OrderBy().Collect()

creek.FromArray(result).ForEach(func(item int) {
    fmt.Println(item)
})

Contributing

Every contribution is welcomed.
You can find here a contributing guideline.
A star would be appreciated as well. 😉


License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stream

type Stream[T Streamable] struct {
	Array []T
}

func Empty added in v1.1.0

func Empty[T Streamable]() Stream[T]

The Empty function returns an empty stream.

func FromArray

func FromArray[T Streamable](array []T) Stream[T]

The FromArray function creates a new stream from the given array.

func FromFile added in v1.2.0

func FromFile(file *os.File) Stream[string]

The FromFile function creates a stream from a file. The file is read line by line. Each line is an element of the stream.

func FromValues added in v1.1.0

func FromValues[T Streamable](values ...T) Stream[T]

The FromValues function returns a stream made of the specified parameters.

func (Stream[T]) All added in v1.1.0

func (s Stream[T]) All(expression func(item T) bool) bool

The All function determines whether all elements of the stream satisfy the passed condition.

func (Stream[T]) Append

func (s Stream[T]) Append(item T) Stream[T]

The Append function adds an element to the stream.

func (Stream[T]) AppendAt

func (s Stream[T]) AppendAt(index int, item T) Stream[T]

The AppendAt function inserts the specified element at the specified position in the stream.

func (Stream[T]) AppendIf added in v1.1.0

func (s Stream[T]) AppendIf(item T, c bool) Stream[T]

The AppendIf function adds an element to the stream if the second parameter is true.

func (Stream[T]) ArrEquals added in v1.1.0

func (s Stream[T]) ArrEquals(arr []T) bool

The ArrEquals function compares the stream and the passed array and returns true if they're equals.

func (Stream[T]) Average added in v1.1.0

func (s Stream[T]) Average() float64

The Average function calculates the average of the stream. This function doesn't work with strings.

func (Stream[T]) Clear

func (s Stream[T]) Clear() Stream[T]

The Clear function clears every element from the stream.

func (Stream[T]) Collect

func (s Stream[T]) Collect() []T

The Collect function returns the modified array from the streams.

func (Stream[T]) Contains

func (s Stream[T]) Contains(item T) bool

The Contains function checks whether the stream contains the passed item.

func (Stream[T]) Count

func (s Stream[T]) Count() int

The Count function returns the count of elements in the stream.

func (Stream[T]) Distinct added in v1.1.0

func (s Stream[T]) Distinct() Stream[T]

The Distinct function filters every distinct element from the stream.

func (Stream[T]) ElementAt added in v1.1.0

func (s Stream[T]) ElementAt(index int) T

The ElementAt function is used to get an element from the stream at a particular index. If the element is not present, it throws a panic.

func (Stream[T]) ElementAtOrElse added in v1.1.0

func (s Stream[T]) ElementAtOrElse(index int, elseValue T) T

The ElementAt function is used to get an element from the stream at a particular index. If the element is not present, it returns the elseValue, which is the second parameter.

func (Stream[T]) Equals added in v1.1.0

func (s Stream[T]) Equals(b Stream[T]) bool

The Equals function compares two streams and returns true if they're equals.

func (Stream[T]) Filter

func (s Stream[T]) Filter(expression func(item T) bool) Stream[T]

The Filter function leaves only those elements in the array that make the specified condition true.

func (Stream[T]) Find added in v1.1.0

func (s Stream[T]) Find(expression func(item T) bool) T

The Find function searches for an element that matches the conditions passed and returns the first occurrence within the entire stream.

func (Stream[T]) FindIndex added in v1.1.0

func (s Stream[T]) FindIndex(expression func(item T) bool) int

The FindIndex function searches for an element that matches the conditions passed and returns the index of the first occurrence within the entire stream.

func (Stream[T]) FindLast added in v1.1.0

func (s Stream[T]) FindLast(expression func(item T) bool) T

The FindLast function searches for an element that matches the conditions passed and returns the last occurrence within the entire stream.

func (Stream[T]) FindLastIndex added in v1.1.0

func (s Stream[T]) FindLastIndex(expression func(item T) bool) int

The FindLastIndex function searches for an element that matches the conditions passed and returns the index of the last occurrence within the entire stream.

func (Stream[T]) First added in v1.2.1

func (s Stream[T]) First() T

The First method returns the first element in the stream.

func (Stream[T]) ForEach

func (s Stream[T]) ForEach(expression func(item T))

The ForEach method runs the specified method with every element in the Stream.

func (Stream[T]) IndexOf added in v1.1.0

func (s Stream[T]) IndexOf(item T) int

The IndexOf function returns the position of the first occurrence of the passed value in a stream.

func (Stream[T]) IsEmpty

func (s Stream[T]) IsEmpty() bool

The IsEmpty function checks whether the stream is empty.

func (Stream[T]) IsNotEmpty added in v1.2.1

func (s Stream[T]) IsNotEmpty() bool

The IsNotEmpty function checks whether the stream is not empty.

func (Stream[T]) Join

func (s Stream[T]) Join(separator string) string

Join concatenates the elements of the stream to create a single string. The passed parameter is placed between the elements.

func (Stream[T]) Last added in v1.2.1

func (s Stream[T]) Last() T

The Last method returns the last element in the stream.

func (Stream[T]) LastIndexOf added in v1.1.0

func (s Stream[T]) LastIndexOf(item T) int

The LastIndexOf function returns the position of the last occurrence of the passed value in a stream.

func (Stream[T]) Limit

func (s Stream[T]) Limit(amount int) Stream[T]

The Limit function constrains the number of elements returned by the stream.

func (Stream[T]) Map

func (s Stream[T]) Map(expression func(item T) T) Stream[T]

The Map function creates a new stream populated with the results of calling the provided function on every element.

func (Stream[T]) Max added in v1.1.0

func (s Stream[T]) Max() T

The Max function returns the largest element from the stream.

func (Stream[T]) MaxIndex added in v1.1.0

func (s Stream[T]) MaxIndex() int

The MaxIndex function returns the index of the largest element from the stream.

func (Stream[T]) Min added in v1.1.0

func (s Stream[T]) Min() T

The Min function returns the smallest element from the stream.

func (Stream[T]) MinIndex added in v1.1.0

func (s Stream[T]) MinIndex() int

The MaxIndex function returns the index of the smallest element from the stream.

func (Stream[T]) OrderBy

func (s Stream[T]) OrderBy() Stream[T]

The OrderBy function sorts the stream in ascending order.

func (Stream[T]) OrderByDescending

func (s Stream[T]) OrderByDescending() Stream[T]

The OrderByDescending function sorts the stream in descending order.

func (Stream[T]) Remove

func (s Stream[T]) Remove(item T) Stream[T]

The Remove function removes the passed element from a stream.

func (Stream[T]) RemoveAt

func (s Stream[T]) RemoveAt(index int) Stream[T]

The RemoveAt function removes the item if its index matches the index passed in.

func (Stream[T]) RemoveDuplicates

func (s Stream[T]) RemoveDuplicates() Stream[T]

The RemoveDuplicates function removes every duplicate item from the stream.

func (Stream[T]) RemoveIf added in v1.1.0

func (s Stream[T]) RemoveIf(item T, c bool) Stream[T]

The RemoveIf function removes the passed element from a stream if the second parameter is true.

func (Stream[T]) RemoveWhere

func (s Stream[T]) RemoveWhere(expression func(item T) bool) Stream[T]

The RemoveWhere function removes all the entries that satisfy the provided condition.

func (Stream[T]) Reverse

func (s Stream[T]) Reverse() Stream[T]

The Reverse function reverses the stream.

func (Stream[T]) Shuffle added in v1.2.1

func (s Stream[T]) Shuffle() Stream[T]

The Shuffle function shuffles the stream.

func (Stream[T]) Some added in v1.1.0

func (s Stream[T]) Some(expression func(item T) bool) bool

The Some function determines whether any of the elements of the stream satisfy the passed condition.

func (Stream[T]) Sum added in v1.1.0

func (s Stream[T]) Sum() T

The Sum function adds up all values in a stream.

func (Stream[T]) Wait added in v1.1.0

func (s Stream[T]) Wait(duration time.Duration) Stream[T]

The Wait function pauses the current stream for the duration passed. The first and only parameter expects a value from the built-in time.Duration package.

type Streamable

type Streamable interface {
	string | byte | float32 | float64 | int | int16 | int32 | int64 | uint16 | uint32 | uint64
}

The Streamable interface defines every type you can use the streams with.

type StructStream added in v1.2.0

type StructStream[T interface{}] struct {
	Array []T
}

func FromStructs added in v1.2.0

func FromStructs[T interface{}](array []T) StructStream[T]

The FromStruct function creates a new stream from the given struct array. If the given array is not made of struct, it throws an error.

func (StructStream[T]) All added in v1.2.0

func (s StructStream[T]) All(expression func(item T) bool) bool

The All function determines whether all elements of the stream satisfy the passed condition.

func (StructStream[T]) Append added in v1.2.0

func (s StructStream[T]) Append(item T) StructStream[T]

The Append function adds an element to the stream.

func (StructStream[T]) AppendAt added in v1.2.0

func (s StructStream[T]) AppendAt(index int, item T) StructStream[T]

The AppendAt function inserts the specified element at the specified position in the stream.

func (StructStream[T]) AppendIf added in v1.2.0

func (s StructStream[T]) AppendIf(item T, c bool) StructStream[T]

The AppendIf function adds an element to the stream if the second parameter is true.

func (StructStream[T]) ArrEquals added in v1.2.0

func (s StructStream[T]) ArrEquals(arr []T) bool

The ArrEquals function compares the stream and the passed array and returns true if they're equals.

func (StructStream[T]) Average added in v1.2.0

func (s StructStream[T]) Average(fieldName string) float64

The Average function calculates the average of the stream. This function doesn't work with strings. The first parameter is the name of the field you want to calculate by.

func (StructStream[T]) Clear added in v1.2.0

func (s StructStream[T]) Clear() StructStream[T]

The Clear function clears every element from the stream.

func (StructStream[T]) Collect added in v1.2.0

func (s StructStream[T]) Collect() []T

The Collect function returns the modified array from the streams.

func (StructStream[T]) Contains added in v1.2.0

func (s StructStream[T]) Contains(item T) bool

The Contains function checks whether the stream contains the passed item.

func (StructStream[T]) Count added in v1.2.0

func (s StructStream[T]) Count() int

The Count function returns the count of elements in the stream.

func (StructStream[T]) ElementAt added in v1.2.0

func (s StructStream[T]) ElementAt(index int) T

The ElementAt function is used to get an element from the stream at a particular index. If the element is not present, it throws a panic.

func (StructStream[T]) ElementAtOrElse added in v1.2.0

func (s StructStream[T]) ElementAtOrElse(index int, elseValue T) T

The ElementAt function is used to get an element from the stream at a particular index. If the element is not present, it returns the elseValue, which is the second parameter.

func (StructStream[T]) Equals added in v1.2.0

func (s StructStream[T]) Equals(b StructStream[T]) bool

The Equals function compares two streams and returns true if they're equals.

func (StructStream[T]) Filter added in v1.2.0

func (s StructStream[T]) Filter(expression func(item T) bool) StructStream[T]

The Filter function leaves only those elements in the array that make the specified condition true.

func (StructStream[T]) Find added in v1.2.0

func (s StructStream[T]) Find(expression func(item T) bool) T

The Find function searches for an element that matches the conditions passed and returns the first occurrence within the entire stream.

func (StructStream[T]) FindIndex added in v1.2.0

func (s StructStream[T]) FindIndex(expression func(item T) bool) int

The FindIndex function searches for an element that matches the conditions passed and returns the index of the first occurrence within the entire stream.

func (StructStream[T]) FindLast added in v1.2.0

func (s StructStream[T]) FindLast(expression func(item T) bool) T

The FindLast function searches for an element that matches the conditions passed and returns the last occurrence within the entire stream.

func (StructStream[T]) FindLastIndex added in v1.2.0

func (s StructStream[T]) FindLastIndex(expression func(item T) bool) int

The FindLastIndex function searches for an element that matches the conditions passed and returns the index of the last occurrence within the entire stream.

func (StructStream[T]) First added in v1.2.1

func (s StructStream[T]) First() T

The First method returns the first element in the stream.

func (StructStream[T]) ForEach added in v1.2.0

func (s StructStream[T]) ForEach(expression func(item T))

The ForEach method runs the specified method with every element in the Stream.

func (StructStream[T]) IndexOf added in v1.2.0

func (s StructStream[T]) IndexOf(item T) int

The IndexOf function returns the position of the first occurrence of the passed value in a stream.

func (StructStream[T]) IsEmpty added in v1.2.0

func (s StructStream[T]) IsEmpty() bool

The IsEmpty function checks whether the stream is empty.

func (StructStream[T]) IsNotEmpty added in v1.2.1

func (s StructStream[T]) IsNotEmpty() bool

The IsNotEmpty function checks whether the stream is not empty.

func (StructStream[T]) Last added in v1.2.1

func (s StructStream[T]) Last() T

The Last method returns the last element in the stream.

func (StructStream[T]) LastIndexOf added in v1.2.0

func (s StructStream[T]) LastIndexOf(item T) int

The LastIndexOf function returns the position of the last occurrence of the passed value in a stream.

func (StructStream[T]) Limit added in v1.2.0

func (s StructStream[T]) Limit(amount int) StructStream[T]

The Limit function constrains the number of elements returned by the stream.

func (StructStream[T]) Map added in v1.2.0

func (s StructStream[T]) Map(expression func(item T) T) StructStream[T]

The Map function creates a new stream populated with the results of calling the provided function on every element.

func (StructStream[T]) Max added in v1.2.0

func (s StructStream[T]) Max(fieldName string) T

The Max function returns the largest element from the stream. The first parameter is the name of the field you want to calculate by.

func (StructStream[T]) MaxIndex added in v1.2.0

func (s StructStream[T]) MaxIndex(fieldName string) int

The MaxIndex function returns the index of the largest element from the stream. The first parameter is the name of the field you want to calculate by.

func (StructStream[T]) Min added in v1.2.0

func (s StructStream[T]) Min(fieldName string) T

The Min function returns the smallest element from the stream. The first parameter is the name of the field you want to calculate by.

func (StructStream[T]) MinIndex added in v1.2.0

func (s StructStream[T]) MinIndex(fieldName string) int

The MaxIndex function returns the index of the smallest element from the stream. The first parameter is the name of the field you want to calculate by.

func (StructStream[T]) OrderBy added in v1.2.0

func (s StructStream[T]) OrderBy(fieldName string) StructStream[T]

The OrderBy function sorts the stream in ascending order. The first parameter is the name of the field you want to sort by.

func (StructStream[T]) OrderByDescending added in v1.2.0

func (s StructStream[T]) OrderByDescending(fieldName string) StructStream[T]

The OrderByDescending function sorts the stream in descending order The first parameter is the name of the field you want to sort by.

func (StructStream[T]) Remove added in v1.2.0

func (s StructStream[T]) Remove(item T) StructStream[T]

The Remove function removes the passed element from a stream.

func (StructStream[T]) RemoveAt added in v1.2.0

func (s StructStream[T]) RemoveAt(index int) StructStream[T]

The RemoveAt function removes the item if its index matches the index passed in.

func (StructStream[T]) RemoveIf added in v1.2.0

func (s StructStream[T]) RemoveIf(item T, c bool) StructStream[T]

The RemoveIf function removes the passed element from a stream if the second parameter is true.

func (StructStream[T]) RemoveWhere added in v1.2.0

func (s StructStream[T]) RemoveWhere(expression func(item T) bool) StructStream[T]

The RemoveWhere function removes all the entries that satisfy the provided condition.

func (StructStream[T]) Reverse added in v1.2.0

func (s StructStream[T]) Reverse() StructStream[T]

The Reverse function reverses the stream.

func (StructStream[T]) Shuffle added in v1.2.1

func (s StructStream[T]) Shuffle() StructStream[T]

The Shuffle function shuffles the stream.

func (StructStream[T]) Some added in v1.2.0

func (s StructStream[T]) Some(expression func(item T) bool) bool

The Some function determines whether any of the elements of the stream satisfy the passed condition.

func (StructStream[T]) Sum added in v1.2.0

func (s StructStream[T]) Sum(fieldName string) float64

The Sum function adds up all values in a stream. The first parameter is the name of the field you want to calculate by.

func (StructStream[T]) Wait added in v1.2.0

func (s StructStream[T]) Wait(duration time.Duration) StructStream[T]

The Wait function pauses the current stream for the duration passed. The first and only parameter expects a value from the built-in time.Duration package.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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