lingo

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 5 Imported by: 0

README

lingo

go report card test status Go Reference

lingo is a library written in Go. It is LinQ in .NET for Go. It will help the array processing code more neat.

The key features of lingo are:

Getting started

Prerequisites
  • Go: version is newer than 1.18
Installation
go get -u github.com/vanh01/lingo
Quick Start

For example, I want to get top 3 students in a class whose name contains "1"

package main

import (
	"fmt"

	lingo "github.com/vanh01/lingo"
)

type Student struct {
	Id      int
	Name    string
	Score	int
	ClassId int
}

type Class struct {
	Id    int
	Name  string
	Total int
}

func main() {
	source := []Student{
		{Id: 1, Name: "A", ClassId: 1},
		{Id: 2, Name: "B", ClassId: 2},
		{Id: 3, Name: "C", ClassId: 2},
		{Id: 4, Name: "D", ClassId: 1},
		{Id: 5, Name: "E", ClassId: 2},
		{Id: 6, Name: "F", ClassId: 1},
		{Id: 7, Name: "G", ClassId: 1},
		{Id: 8, Name: "H", ClassId: 2},
		{Id: 9, Name: "J", ClassId: 1},
		{Id: 10, Name: "K", ClassId: 2},
		{Id: 12, Name: "Q", ClassId: 2},
	}

	classes := []Class{
		{Id: 1, Name: "Class 1", Total: 20},
		{Id: 2, Name: "Class 2", Total: 10},
	}
	classIds := lingo.AsEnumerable(classes).
		Where(func(c Class) bool {
			return strings.Contains(c.Name, "1")
		}).
		Select(func(c Class) any {
			return c.Id
		}).
		ToSlice()

	enumerable := lingo.AsEnumerable(source).
		Where(func(t Student) bool {
			return lingo.AsEnumerable(classIds).Contains(t.ClassId)
		}).
		OrderByDescending(func(s Student) any {
			return s.Score
		}).
		Take(3)
	fmt.Println(enumerable.ToSlice())
}
// Result: [{9 J 10 1} {4 D 7 1} {1 A 6 1}]

Features

Classification table

The following table classifies each supported method, there are two types: Immediate execution and Chain execution.

  • Immediate execution terminates an enumerable and returns the result
  • Chain execution can be called many times.
Standard query operator Return type Immediate execution Chain execution
Min T x
MinBy T x
Max T x
MaxBy T x
Sum Number x
Average float64 x
Count int64 x
Aggregate any x
ToSlice []T x
ToMap map[any]any x
Concat Enumerable[T] x
Where Enumerable[T] x
GroupBy Enumerable[any] x
Join Enumerable[any] x
Skip Enumerable[T] x
SkipWhile Enumerable[T] x
Take Enumerable[T] x
TakeWhile Enumerable[T] x
Select Enumerable[any] x
SelectMany Enumerable[any] x
Zip Enumerable[any] x
All bool x
Any bool x
Contains bool x
FirstOrNil T x
FirstOrDefault T x
LastOrNil T x
LastOrDefault T x
ElementAtOrNil T x
ElementAtOrDefault T x
Distinct Enumerable[T] x
DistinctBy Enumerable[T] x
Except Enumerable[T] x
ExceptBy Enumerable[T] x
Intersect Enumerable[T] x
IntersectBy Enumerable[T] x
Union Enumerable[T] x
UnionBy Enumerable[T] x
OrderBy Enumerable[T] x
OrderByDescending Enumerable[T] x
Reverse Enumerable[T] x
Append Enumerable[T] x
AppendRange Enumerable[T] x
Prepend Enumerable[T] x
PrependRange Enumerable[T] x
Clear Enumerable[T] x
Insert Enumerable[T] x
Remove Enumerable[T] x
RemoveAt Enumerable[T] x
RemoveRange Enumerable[T] x

I have defined the Student structure for the following examples

type Student struct {
	Id      int
	Name    string
	ClassId int
}

type Class struct {
	Id    int
	Name  string
	Total int
}

Following are all the examples related to the supported methods in this library.

Please click here

Reference

Contributors

Thanks to all the people who already contributed!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSorter

func NewSorter[T any, K any](origin []T, source []K, comparer ...definition.Comparer[K]) sorter[T, K]

func SliceAnyToT

func SliceAnyToT[T any](source interface{}) []T

SliceAnyToT converts from slice of any to slice of specific type

func SliceTToAny

func SliceTToAny[T any](source []T) []any

SliceTToAny converts from slice of specific type to slice of any

Types

type Enumerable

type Enumerable[T any] struct {
	// contains filtered or unexported fields
}

func AsEnumerable

func AsEnumerable[T any](t []T) Enumerable[T]

AsEnumerable creates a new Enumerable

func AsEnumerableAnyFromSliceT added in v1.0.0

func AsEnumerableAnyFromSliceT[T any](a []T) Enumerable[any]

AsEnumerableAnyFromSliceT creates a new Enumerable of any type from slice of specific type

func AsEnumerableAnyFromT added in v1.0.0

func AsEnumerableAnyFromT[T any](e Enumerable[T]) Enumerable[any]

AsEnumerableAnyFromT creates a new Enumerable of any type from Enumerable of specific type

func AsEnumerableTFromAny

func AsEnumerableTFromAny[T any](e Enumerable[any]) Enumerable[T]

AsEnumerableTFromAny creates a new Enumerable of specific type from Enumerable of any

This will be useful after using projection operations

func AsEnumerableTFromSliceAny

func AsEnumerableTFromSliceAny[T any](a []any) Enumerable[T]

AsEnumerableTFromSliceAny creates a new Enumerable of specific type from slice of any

This will be useful after using projection operations

func Chunk added in v1.0.0

func Chunk[T any](e Enumerable[T], size int) Enumerable[[]T]

Chunk splits the elements of a sequence into chunks of a specified maximum size.

func Empty added in v0.2.0

func Empty[T any]() Enumerable[T]

Empty returns an empty Enumerable[T] that has the specified type argument.

func Range added in v0.2.0

func Range(start, end int) Enumerable[int]

Range generates a sequence of integral numbers within a specified range.

func Repeat added in v0.2.0

func Repeat[T any](element T, times int) Enumerable[T]

Repeat generates a sequence that contains one repeated value.

func (Enumerable[T]) Aggregate added in v0.2.1

func (e Enumerable[T]) Aggregate(
	seed any,
	accumulator definition.Accumulator[any, T],
	resultSelector ...definition.SingleSelector[any],
) any

Aggregate applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.

If resultSelector is not empty or nil, we will use the first comparer

Noted that the type of seed, the left type of Accumulator function and the input type of selector must be the same

func (Enumerable[T]) All

func (e Enumerable[T]) All(predicate Predicate[T]) bool

All determines whether all the elements in a sequence satisfy a condition.

func (Enumerable[T]) Any

func (e Enumerable[T]) Any(predicate Predicate[T]) bool

Any determines whether any elements in a sequence satisfy a condition.

func (Enumerable[T]) Append added in v0.3.0

func (e Enumerable[T]) Append(t T) Enumerable[T]

Append appends a value to the end of the sequence.

func (Enumerable[T]) AppendRange added in v0.3.0

func (e Enumerable[T]) AppendRange(second Enumerable[T]) Enumerable[T]

AppendRange appends the elements of the specified collection to the end of the sequence.

func (Enumerable[T]) AsParallel added in v1.1.0

func (e Enumerable[T]) AsParallel() ParallelEnumerable[T]

AsParallel creates a new ParallelEnumerable from an Enumerable, it will need more resources but performance will be improved

Note that ParallelEnumerable will be useful in some cases, you must really consider when using it.

In worse cases, not only will performance not be optimized, but costs will also increase.

func (Enumerable[T]) Average added in v0.2.0

func (e Enumerable[T]) Average(selector ...definition.SingleSelector[T]) float64

Average computes the average of a sequence of numeric values.

If selector is not empty or nil, we will use the first comparer

func (Enumerable[T]) Clear added in v0.3.0

func (e Enumerable[T]) Clear() Enumerable[T]

Clear removes all elements of the sequence.

func (Enumerable[T]) Concat added in v0.2.0

func (e Enumerable[T]) Concat(second Enumerable[T]) Enumerable[T]

Concat concatenates two sequences.

func (Enumerable[T]) Contains

func (e Enumerable[T]) Contains(value T, comparer ...definition.Comparer[T]) bool

Contains determines whether a sequence contains a specified element.

In this method, comparer is returns whether left is equal to right or not.

If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) Count added in v0.2.0

func (e Enumerable[T]) Count() int64

Count returns the number of elements in a sequence.

func (Enumerable[T]) Distinct

func (e Enumerable[T]) Distinct() Enumerable[T]

Distinct removes duplicate values from a collection.

func (Enumerable[T]) DistinctBy

func (e Enumerable[T]) DistinctBy(keySelector definition.SingleSelector[T], comparer ...definition.Comparer[any]) Enumerable[T]

DistinctBy removes duplicate values from a collection with keySelector and comparer,

In this method, comparer is returns whether left is equal to right or not. If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) ElementAtOrDefault added in v0.2.0

func (e Enumerable[T]) ElementAtOrDefault(index int64, defaultValue T) T

ElementAtOrDefault returns the element at a specified index in a sequence or a default value if the index is out of range.

func (Enumerable[T]) ElementAtOrNil added in v0.2.0

func (e Enumerable[T]) ElementAtOrNil(index int64) T

ElementAtOrNil returns the element at a specified index in a sequence or a default value if the index is out of range.

func (Enumerable[T]) Except

func (e Enumerable[T]) Except(second Enumerable[T]) Enumerable[T]

Except returns the set difference, which means the elements of one collection that don't appear in a second collection.

func (Enumerable[T]) ExceptBy added in v1.0.0

func (e Enumerable[T]) ExceptBy(second Enumerable[any], keySelector definition.SingleSelector[T], comparer ...definition.Comparer[any]) Enumerable[T]

ExceptBy returns the set difference, which means the elements of one collection that don't appear in a second collection according to a specified key selector function.

In this method, comparer is returns whether left is equal to right or not. If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) FirstOrDefault added in v0.2.0

func (e Enumerable[T]) FirstOrDefault(defaultValue T, predicate ...Predicate[T]) T

FirstOrDefault returns the first element of a sequence (with condition if any), or a default value if no element is found

predicate can be nil. If predicate is not empty or nil, we will use the first predicate

func (Enumerable[T]) FirstOrNil added in v0.2.0

func (e Enumerable[T]) FirstOrNil(predicate ...Predicate[T]) T

FirstOrNil returns the first element of a sequence (with condition if any), or a nil value if no element is found

predicate can be nil. If predicate is not empty or nil, we will use the first predicate

func (Enumerable[T]) GetIter added in v1.0.0

func (e Enumerable[T]) GetIter() <-chan T

GetIter returns an unbuffered channel of T that iterates through a collection.

func (Enumerable[T]) GroupBy

func (e Enumerable[T]) GroupBy(
	keySelector definition.SingleSelector[T],
	elementSelector definition.SingleSelector[T],
	resultSelector definition.GroupBySelector[any, any],
	getHash definition.GetHashCode[any],
) Enumerable[any]

GroupBy groups elements that share a common attribute.

In this method, getHash returns the key of the grouping.

elementSelector, getHash can be nil

func (Enumerable[T]) Insert added in v0.3.0

func (e Enumerable[T]) Insert(index int, t T) Enumerable[T]

Insert inserts an element into the sequence at the specified index.

func (Enumerable[T]) Intersect

func (e Enumerable[T]) Intersect(second Enumerable[T]) Enumerable[T]

Intersect returns the set intersection, which means elements that appear in each of two collections.

func (Enumerable[T]) IntersectBy added in v1.0.0

func (e Enumerable[T]) IntersectBy(second Enumerable[any], keySelector definition.SingleSelector[T], comparer ...definition.Comparer[any]) Enumerable[T]

IntersectBy returns the set intersection, which means elements that appear in each of two collections according to a specified key selector function.

In this method, comparer is returns whether left is equal to right or not. If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) Join

func (e Enumerable[T]) Join(
	inner Enumerable[any],
	outerKeySelector definition.SingleSelector[T],
	innerKeySelector definition.SingleSelector[any],
	resultSelector definition.CombinationSelector[T, any],
	comparer ...definition.Comparer[any],
) Enumerable[any]

Join joins two sequences based on key selector functions and extracts pairs of values.

In this method, comparer is returns whether left is equal to right or not.

If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) LastOrDefault added in v0.2.0

func (e Enumerable[T]) LastOrDefault(defaultValue T, predicate ...Predicate[T]) T

LastOrDefault returns the last element of a sequence (with condition if any), or a default value if no element is found

predicate can be nil. If predicate is not empty or nil, we will use the first predicate

func (Enumerable[T]) LastOrNil added in v0.2.0

func (e Enumerable[T]) LastOrNil(predicate ...Predicate[T]) T

LastOrNil returns the last element of a sequence (with condition if any), or a nil value if no element is found

predicate can be nil. If predicate is not empty or nil, we will use the first predicate

func (Enumerable[T]) Max added in v0.2.0

func (e Enumerable[T]) Max(comparer ...definition.Comparer[T]) T

Max returns the maximum value in a sequence of values.

In this method, comparer is returns whether left is greater than right or not. The left one will be returned

If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) MaxBy added in v1.1.0

func (e Enumerable[T]) MaxBy(selector definition.SingleSelector[T], comparer ...definition.Comparer[any]) T

MaxBy returns the maximum value in a sequence of values according to a specified key selector function.

In this method, comparer is returns whether left is smaller than right or not. The left one will be returned

If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) Min added in v0.2.0

func (e Enumerable[T]) Min(comparer ...definition.Comparer[T]) T

Min returns the minimum value in a sequence of values.

In this method, comparer is returns whether left is smaller than right or not. The left one will be returned

If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) MinBy added in v1.1.0

func (e Enumerable[T]) MinBy(selector definition.SingleSelector[T], comparer ...definition.Comparer[any]) T

MinBy returns the minimum value in a sequence of values according to a specified key selector function.

In this method, comparer is returns whether left is smaller than right or not. The left one will be returned

If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) OrderBy

func (e Enumerable[T]) OrderBy(selector definition.SingleSelector[T], comparer ...definition.Comparer[any]) Enumerable[T]

OrderBy sorts values in ascending order.

In this method, comparer is returns whether left is smaller than right or not. If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) OrderByDescending

func (e Enumerable[T]) OrderByDescending(selector definition.SingleSelector[T], comparer ...definition.Comparer[any]) Enumerable[T]

OrderByDescending sorts values in descending order.

In this method, comparer is returns whether left is smaller than right or not. If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) Prepend added in v0.3.0

func (e Enumerable[T]) Prepend(t T) Enumerable[T]

Prepend adds a value to the beginning of the sequence.

func (Enumerable[T]) PrependRange added in v0.3.0

func (e Enumerable[T]) PrependRange(second Enumerable[T]) Enumerable[T]

PrependRange appends the elements of the specified collection to the beginning of the sequence.

func (Enumerable[T]) Remove added in v0.3.0

func (e Enumerable[T]) Remove(t T, comparer ...definition.Comparer[T]) Enumerable[T]

Remove removes the first occurrence of the given element, if found.

If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) RemoveAt added in v0.3.0

func (e Enumerable[T]) RemoveAt(index int) Enumerable[T]

RemoveAt removes the element at the specified index of the sequence.

func (Enumerable[T]) RemoveRange added in v0.3.0

func (e Enumerable[T]) RemoveRange(index int, count int) Enumerable[T]

RemoveRange removes a range of elements from the sequence.

func (Enumerable[T]) Reverse

func (e Enumerable[T]) Reverse() Enumerable[T]

Reverse reverses the order of the elements in a collection.

func (Enumerable[T]) Select

func (e Enumerable[T]) Select(selector definition.SingleSelector[T]) Enumerable[any]

Select projects values that are based on a transform function.

func (Enumerable[T]) SelectMany

func (e Enumerable[T]) SelectMany(selector definition.SingleSelector[T]) Enumerable[any]

SelectMany projects sequences of values that are based on a transform function and then flattens them into one sequence.

func (Enumerable[T]) Skip

func (e Enumerable[T]) Skip(number int) Enumerable[T]

Skip skips elements up to a specified position in a sequence.

func (Enumerable[T]) SkipWhile

func (e Enumerable[T]) SkipWhile(predicate Predicate[T]) Enumerable[T]

SkipWhile skips elements based on a predicate function until an element doesn't satisfy the condition.

func (Enumerable[T]) Sum added in v0.2.0

func (e Enumerable[T]) Sum(selector ...definition.SingleSelector[T]) any

Sum computes the sum of a sequence of numeric values.

If selector is not empty or nil, we will use the first comparer

func (Enumerable[T]) Take

func (e Enumerable[T]) Take(number int) Enumerable[T]

Take takes elements up to a specified position in a sequence.

func (Enumerable[T]) TakeWhile

func (e Enumerable[T]) TakeWhile(predicate Predicate[T]) Enumerable[T]

TakeWhile takes elements based on a predicate function until an element doesn't satisfy the condition.

func (Enumerable[T]) ToMap

func (e Enumerable[T]) ToMap(keySelector definition.SingleSelector[T], elementSelector definition.SingleSelector[T]) map[any]any

ToMap converts the iterator into a map with specific selector

func (Enumerable[T]) ToSlice

func (e Enumerable[T]) ToSlice() []T

ToSlice converts the iterator into a slice

func (Enumerable[T]) Union

func (e Enumerable[T]) Union(second Enumerable[T]) Enumerable[T]

Union returns the set union, which means unique elements that appear in either of two collections.

func (Enumerable[T]) UnionBy added in v1.0.0

func (e Enumerable[T]) UnionBy(second Enumerable[T], keySelector definition.SingleSelector[T], comparer ...definition.Comparer[any]) Enumerable[T]

UnionBy returns the set union, which means unique elements that appear in either of two collections according to a specified key selector function.

In this method, comparer is returns whether left is equal to right or not. If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (Enumerable[T]) Where

func (e Enumerable[T]) Where(predicate Predicate[T]) Enumerable[T]

Where selects values that are based on a predicate function.

func (Enumerable[T]) Zip

func (e Enumerable[T]) Zip(second Enumerable[any], resultSelector ...definition.CombinationSelector[T, any]) Enumerable[any]

Zip produces a sequence of tuples with elements from 2 specified sequences

If resultSelector is nil, the default result is a slice combined with each element On the other hand, we just use the first resultSelector

type Grouping added in v1.0.0

type Grouping[K any, V any] struct {
	Enumerable[V]
	Key   K
	Count int
}

Grouping represents a collection of objects that have a common key.

type Lookup added in v1.0.0

type Lookup[K any, V any] struct {
	Enumerable[Grouping[K, V]]

	Count int
	// contains filtered or unexported fields
}

Lookup represents a collection of keys each mapped to one or more values.

func AsLookup added in v1.0.0

func AsLookup[T any, K any, V any](
	e Enumerable[T],
	keySelector definition.SingleSelectorFull[T, K],
	elementSelector ...definition.SingleSelectorFull[T, V],
) Lookup[K, V]

AsLookup Creates a generic Lookup[K, V] from an Enumerable[T].

elementSelector can be nil. If elementSelector is not empty or nil, we will use the first elementSelector

func AsPLookup added in v1.1.0

func AsPLookup[T any, K any, V any](
	p ParallelEnumerable[T],
	keySelector definition.SingleSelectorFull[T, K],
	elementSelector ...definition.SingleSelectorFull[T, V],
) Lookup[K, V]

AsPLookup creates a generic Lookup[K, V] from an ParallelEnumerable[T].

elementSelector can be nil. If elementSelector is not empty or nil, we will use the first elementSelector

func (Lookup[K, V]) ContainsKey added in v1.0.0

func (l Lookup[K, V]) ContainsKey(t K) bool

ContainsKey etermines whether a specified key exists in the Lookup[T, K].

func (Lookup[K, V]) GetValue added in v1.0.0

func (l Lookup[K, V]) GetValue(key K) []V

GetValue returns a sequence of values indexed by a specified key.

type ParallelEnumerable added in v1.1.0

type ParallelEnumerable[T any] struct {
	// contains filtered or unexported fields
}

ParallelEnumerable provides a set of methods for querying objects that implement ParallelQuery[T]. This is the parallel equivalent of Enumerable.

Note that ParallelEnumerable will be useful in some cases, you must really consider when using it.

In worse cases, not only will performance not be optimized, but costs will also increase.

func AsParallelEnumerable added in v1.1.0

func AsParallelEnumerable[T any](t []T) ParallelEnumerable[T]

AsParallelEnumerable creates a new ParallelEnumerable, it will need more resources but performance will be improved

Note that ParallelEnumerable will be useful in some cases, you must really consider when using it.

In worse cases, not only will performance not be optimized, but costs will also increase.

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

func (p ParallelEnumerable[T]) All(predicate Predicate[T]) bool

All determines in parallel whether all elements of a sequence satisfy a condition.

func (ParallelEnumerable[T]) Any added in v1.1.0

func (p ParallelEnumerable[T]) Any(predicate Predicate[T]) bool

Any determines in parallel whether any element of a sequence satisfies a condition.

func (ParallelEnumerable[T]) AsEnumerable added in v1.1.0

func (p ParallelEnumerable[T]) AsEnumerable() Enumerable[T]

AsEnumerable creates a new Enumerable from ParallelEnumerable

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

func (p ParallelEnumerable[T]) Average(selector ...definition.SingleSelector[T]) float64

Average computes in parallel the average of a sequence of numeric values that are obtained by invoking a transform function on each element of the input sequence.

If selector is not empty or nil, we will use the first comparer

func (ParallelEnumerable[T]) Contains added in v1.1.0

func (p ParallelEnumerable[T]) Contains(value T, comparer ...definition.Comparer[T]) bool

Contains determines in parallel whether a sequence contains a specified element.

In this method, comparer is returns whether left is equal to right or not.

If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (ParallelEnumerable[T]) GetIter added in v1.1.0

func (p ParallelEnumerable[T]) GetIter() <-chan T

GetIter returns an unbuffered channel of T that iterates through a collection.

func (ParallelEnumerable[T]) GroupBy added in v1.1.0

func (p ParallelEnumerable[T]) GroupBy(
	keySelector definition.SingleSelector[T],
	elementSelector definition.SingleSelector[T],
	resultSelector definition.GroupBySelector[any, any],
	getHash definition.GetHashCode[any],
) ParallelEnumerable[any]

GroupBy groups in parallel elements that share a common attribute.

In this method, getHash returns the key of the grouping.

elementSelector, getHash can be nil

func (ParallelEnumerable[T]) MaxBy added in v1.1.0

func (p ParallelEnumerable[T]) MaxBy(selector definition.SingleSelector[T], comparer ...definition.Comparer[any]) T

MaxBy invokes in parallel a transform function on each element of a sequence and returns the maximum value.

In this method, comparer is returns whether left is smaller than right or not. The left one will be returned

If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (ParallelEnumerable[T]) MinBy added in v1.1.0

func (p ParallelEnumerable[T]) MinBy(selector definition.SingleSelector[T], comparer ...definition.Comparer[any]) T

MinBy invokes in parallel a transform function on each element of a sequence and returns the minimum value.

In this method, comparer is returns whether left is smaller than right or not. The left one will be returned

If comparer is empty or nil, we will use the default comparer. On the other hand, we just use the first comparer

func (ParallelEnumerable[T]) Select added in v1.1.0

Select projects in parallel each element of a sequence into a new form.

func (ParallelEnumerable[T]) SelectMany added in v1.1.0

func (p ParallelEnumerable[T]) SelectMany(selector definition.SingleSelector[T]) ParallelEnumerable[any]

SelectMany projects in parallel each element of a sequence to an []T and flattens the resulting sequences into one sequence.

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

func (p ParallelEnumerable[T]) Sum(selector ...definition.SingleSelector[T]) any

Sum computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on each element of the input sequence.

If selector is not empty or nil, we will use the first comparer

func (ParallelEnumerable[T]) ToMap added in v1.1.0

func (p ParallelEnumerable[T]) ToMap(keySelector definition.SingleSelector[T], elementSelector definition.SingleSelector[T]) map[any]any

ToMap converts the iterator into a map with specific selector

func (ParallelEnumerable[T]) ToSlice added in v1.1.0

func (p ParallelEnumerable[T]) ToSlice() []T

ToSlice converts the iterator into a slice

func (ParallelEnumerable[T]) Where added in v1.1.0

func (p ParallelEnumerable[T]) Where(predicate Predicate[T]) ParallelEnumerable[T]

Where filters in parallel a sequence of values based on a predicate.

func (ParallelEnumerable[T]) Zip added in v1.1.0

Zip merges in parallel two sequences by using the specified predicate function.

If resultSelector is nil, the default result is a slice combined with each element On the other hand, we just use the first resultSelector

type Predicate

type Predicate[T any] func(T) bool

func (Predicate[T]) And

func (p Predicate[T]) And(right Predicate[T]) Predicate[T]

And returns a new predicate, it is current && right

func (Predicate[T]) Or

func (p Predicate[T]) Or(right Predicate[T]) Predicate[T]

Or returns a new predicate, it is current || right

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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