linq

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2022 License: MIT Imports: 3 Imported by: 0

README

LINQ for Go with type parameters

LINQ (Language-Integrated Query) is a component that adds data querying capabilities of Microsoft .Net languages. This package provides the implementation of the LINQ functions for Go with type parameters.

Quick Start

Install
go get github.com/makiuchi-d/linq
Example
package main

import (
	"fmt"

	"github.com/makiuchi-d/linq"
)

type Student struct {
	Name  string
	Class string
	Score int
}

func main() {
	students := []Student{
		// generated by https://testdata.userlocal.jp/
		{"熊木 緑", "1-A", 953},
		{"山本 千佳子", "1-C", 559},
		{"星 雅彦", "1-B", 136},
		{"齊藤 綾子", "1-A", 149},
		{"杉原 和己", "1-C", 737},
		{"山路 信之", "1-B", 425},
		{"佐々木 淑子", "1-C", 759},
		{"三宅 直人", "1-B", 594},
		{"緒方 俊", "1-B", 405},
		{"稲井 隆生", "1-A", 495},
	}

	e := linq.FromSlice(students)
	e = linq.Where(e, func(s Student) (bool, error) { return s.Class == "1-B", nil })
	e = linq.OrderByDescending(e, func(s Student) (int, error) { return s.Score, nil })

	linq.ForEach(e, func(s Student) error {
		fmt.Printf("%d %s\n", s.Score, s.Name)
		return nil
	})
}

Output:

594 三宅 直人
425 山路 信之
405 緒方 俊
136 星 雅彦

Functions

italics are unique to this package.

Sorting Data
  • OrderBy
  • OrderByDescending
  • OrderByFunc
  • ThenBy
  • ThenByDescending
  • Reverse
Set Operations
  • Distinct
  • DistinctBy
  • Except
  • ExceptBy
  • Intersect
  • IntersectBy
  • Union
  • UnionBy
Filtering Data
  • Where
Quantifier Operations
  • All
  • Any
  • Contains
  • ContainsFunc
Projection Operations
  • Select
  • SelectMany
  • Zip
Partitioning Data
  • Skip
  • SkipLast
  • SkipWhile
  • Take
  • TakeLast
  • TakeWhile
  • Chunk
Join Operations
  • GroupJoin
  • Join
Grouping Data
  • GroupBy
Generation Operations
  • DefaultIfEmpty
  • Empty
  • Range
  • Repeat
Element Operations
  • ElementAt
  • ElementAtOrDefault
  • First
  • FirstOrDefault
  • Last
  • LastOrDefault
  • Single
  • SingleOrDefault
Converting Data Types
  • FromMap
  • FromSlice
  • ToMap
  • ToMapFunc
  • ToSlice
Concatenation Operations
  • Concat
Aggregation Operations
  • Aggregate
  • Average
  • AverageFunc
  • Count
  • Max
  • MaxBy
  • MaxByFunc
  • Min
  • MinBy
  • MinByFunc
  • Sum
  • Sumf
  • SumByFunc
  • SumByFuncf
Other
  • ForEach

C# LINQ Documents

Standard Query Operators
Sorting Data
Set Operations
Filtering Data
Quantifier Operations
Projection Operations
Partitioning Data
Join Operations
Grouping Data
Generation Operations
Equality Operations
Element Operations
Converting Data Types
Concatenation Operations
Aggregation Operations

Documentation

Overview

LINQ

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Aggregate

func Aggregate[S, T any](src Enumerator[S], seed T, fn func(acc T, v S) (T, error)) (T, error)

Aggregate applies an accumulator function over a sequence.

func All

func All[T any](src Enumerator[T], pred func(v T) (bool, error)) (bool, error)

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

func Any

func Any[T any](src Enumerator[T], pred func(v T) (bool, error)) (bool, error)

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

func Average

func Average[T Real](src Enumerator[T]) (float64, error)

Average computes the average of a sequence of numeric (real number) values.

func AverageFunc

func AverageFunc[T any, U Real](src Enumerator[T], selector func(T) (U, error)) (float64, error)

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

func Contains

func Contains[T comparable](src Enumerator[T], val T) (bool, error)

Contains cetermines whether a sequence contains a specified element.

func ContainsFunc

func ContainsFunc[T any](src Enumerator[T], val T, equals func(T, T) (bool, error)) (bool, error)

ContainsFunc determines whether a sequence contains a specified element by using a specified comparer function.

func Count

func Count[T any](src Enumerator[T], pred func(T) (bool, error)) (int, error)

Count returns a number that represents how many elements in the specified sequence satisfy a condition.

func ElementAt

func ElementAt[T any](src Enumerator[T], n int) (def T, err error)

ElementAt returns the element at a specified index in a sequence.

func ElementAtOrDefault

func ElementAtOrDefault[T any](src Enumerator[T], n int) (T, error)

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

func First

func First[T any](src Enumerator[T], pred func(T) (bool, error)) (def T, _ error)

First returns the first element in a sequence that satisfies a specified condition.

func FirstOrDefault

func FirstOrDefault[T any](src Enumerator[T], pred func(T) (bool, error), defaultValue T) (T, error)

FirstOrDefault returns the first element of the sequence that satisfies a condition, or a specified default value if no such element is found.

func ForEach

func ForEach[T any](e Enumerator[T], f func(T) error) error

ForEach performs the specified action on each element of the specified enumerator.

func Last

func Last[T any](src Enumerator[T], pred func(T) (bool, error)) (def T, _ error)

Last returns the last element of a sequence that satisfies a specified condition.

func LastOrDefault

func LastOrDefault[T any](src Enumerator[T], pred func(T) (bool, error), defaultValue T) (T, error)

LastOrDefault returns the last element of a sequence that satisfies a condition, or a specified default value if no such element is found.

func Max

func Max[T constraints.Ordered](src Enumerator[T]) (def T, _ error)

Maxby returns the maximum value in a sequence of values.

func MaxBy

func MaxBy[T any, K constraints.Ordered](src Enumerator[T], keySelector func(T) (K, error)) (def T, _ error)

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

func MaxByFunc

func MaxByFunc[T any](src Enumerator[T], greater func(a, b T) (bool, error)) (def T, _ error)

MaxByFunc returns the maximum value in a generic sequence according to a comparer function.

func Min

func Min[T constraints.Ordered](src Enumerator[T]) (def T, _ error)

Min returns the minimum value in a sequence of values.

func MinBy

func MinBy[T any, K constraints.Ordered](src Enumerator[T], keySelector func(T) (K, error)) (def T, _ error)

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

func MinByFunc

func MinByFunc[T any](src Enumerator[T], less func(a, b T) (bool, error)) (def T, _ error)

MinByFunc returns the minimum value in a generic sequence according to a comparer function.

func Single

func Single[T any](src Enumerator[T], pred func(T) (bool, error)) (def T, _ error)

Single returns the only element of a sequence that satisfies a specified condition, and return an error InvalidOperation if more than one such element exists.

func SingleOrDefault

func SingleOrDefault[T any](src Enumerator[T], pred func(T) (bool, error), defaultValue T) (def T, _ error)

SingleOrDefault returns the only element of a sequence that satisfies a specified condition, or a specified default value if no such element exists; this function returns an error InvalidOperation if more than one element satisfies the condition.

func Sum

func Sum[T constraints.Integer](src Enumerator[T]) (int, error)

Sum calculates the sum of a integer sequence.

func SumByFunc

func SumByFunc[T any, K constraints.Integer](src Enumerator[T], selector func(v T) (K, error)) (int, error)

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

func SumByFuncf

func SumByFuncf[T any, K constraints.Float](src Enumerator[T], selector func(v T) (K, error)) (float64, error)

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

func Sumf

func Sumf[T constraints.Float](src Enumerator[T]) (float64, error)

Sumf calculates the sum of a floating number sequence.

func ToMap

func ToMap[K comparable, V any](src Enumerator[KeyValue[K, V]]) (map[K]V, error)

ToMap creates a map[K]V from an Enumerator[T]. T must be a type KeyValue[K, V].

func ToMapFunc

func ToMapFunc[T any, K comparable, V any](src Enumerator[T], selector func(T) (K, V, error)) (map[K]V, error)

ToMapFunc creates a map[K]V from an Enumerator[T] according to specified key-value selector function.

func ToSlice

func ToSlice[T any](src Enumerator[T]) ([]T, error)

ToSlice creates a slice from an Enumerator[T]

Types

type Enumerator

type Enumerator[T any] interface {

	// Next returns a next element of this collection.
	// It returns EOC as an `error` when it reaches the end of the collection.
	Next() (T, error)
}

Enumerator[T] is a queryable collection

func Chunk

func Chunk[T any](src Enumerator[T], size int) Enumerator[[]T]

Chunk splits the elements of a sequence into chunks of size at most `size`.

func Concat

func Concat[T any](first, second Enumerator[T]) Enumerator[T]

Concat concatenates two sequences.

func DefaultIfEmpty

func DefaultIfEmpty[T any](src Enumerator[T], defaultValue T) Enumerator[T]

DefaultIfEmpty returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty.

func Distinct

func Distinct[T any](src Enumerator[T], equals func(T, T) (bool, error), getHashCode func(T) (int, error)) Enumerator[T]

Distinct returns distinct elements from a sequence by using the specified comparer functions.

func DistinctBy

func DistinctBy[T any, K comparable](src Enumerator[T], keySelector func(v T) (K, error)) Enumerator[T]

DistinctBy returns distinct elements from a sequence according to a specified key selector function.

func Empty

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

Empty returns an empty IEnumerable<T> that has the specified type argument.

func Except

func Except[T any](first, second Enumerator[T], equals func(T, T) (bool, error), getHashCode func(T) (int, error)) Enumerator[T]

Except produces the set difference of two sequences by using the specified comparer functions.

func ExceptBy

func ExceptBy[T any, K comparable](first, second Enumerator[T], keySelector func(v T) (K, error)) Enumerator[T]

ExceptBy produces the set difference of two sequences according to a specified key selector function.

func FromMap

func FromMap[T ~map[K]V, K comparable, V any](m T) Enumerator[KeyValue[K, V]]

FromMap generates an Enumerator[T] from a map.

func FromSlice

func FromSlice[S ~[]T, T any](s S) Enumerator[T]

FromSlice generates an Enumerator[T] from a slice.

func GroupBy

func GroupBy[T any, K comparable](src Enumerator[T], keySelector func(T) (K, error)) Enumerator[Grouping[K, T]]

GroupBy groups the elements of a sequence according to a specified key selector function.

func GroupJoin

func GroupJoin[S1, S2, T any, K comparable](
	outer Enumerator[S1],
	inner Enumerator[S2],
	outerKeySelector func(S1) (K, error),
	innerKeySelector func(S2) (K, error),
	resultSelector func(S1, Enumerator[S2]) (T, error),
) Enumerator[T]

GroupJoin correlates the elements of two sequences based on equality of keys and groups the results.

func Intersect

func Intersect[T any](first, second Enumerator[T], equals func(T, T) (bool, error), getHashCode func(T) (int, error)) Enumerator[T]

Intersect produces the set intersection of two sequences by using the specified comparer functions.

func IntersectBy

func IntersectBy[T any, K comparable](first, second Enumerator[T], keySelector func(v T) (K, error)) Enumerator[T]

IntersectBy produces the set intersection of two sequences according to a specified key selector function.

func Join

func Join[S1, S2, T any, K comparable](
	outer Enumerator[S1],
	inner Enumerator[S2],
	outerKeySelector func(S1) (K, error),
	innerKeySelector func(S2) (K, error),
	resultSelector func(S1, S2) (T, error),
) Enumerator[T]

Join correlates the elements of two sequences based on matching keys.

func OrderByFunc

func OrderByFunc[T any](src Enumerator[T], less func(a, b T) bool) Enumerator[T]

OrderByFunc sorts the elements of a sequence by the provided less function.

func Range

func Range(start, count int) Enumerator[int]

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

func Repeat

func Repeat[T any](v T, n int) Enumerator[T]

Repeat generates a sequence that contains one repeated value.

func Reverse

func Reverse[T any](src Enumerator[T]) Enumerator[T]

Reverse inverts the order of the elements in a sequence.

func Select

func Select[S, T any](src Enumerator[S], selector func(v S) (T, error)) Enumerator[T]

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

func SelectMany

func SelectMany[S, C, T any](src Enumerator[S], collectionSelector func(S) (Enumerator[C], error), resultSelector func(C) (T, error)) Enumerator[T]

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

func Skip

func Skip[T any](src Enumerator[T], count int) Enumerator[T]

Skip bypasses a specified number of elements in a sequence and then returns the remaining elements.

func SkipLast

func SkipLast[T any](src Enumerator[T], count int) Enumerator[T]

SkipLast returns a new enumerable collection that contains the elements from source with the last count elements of the source collection omitted.

func SkipWhile

func SkipWhile[T any](src Enumerator[T], pred func(T) (bool, error)) Enumerator[T]

SkipWhile bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.

func Take

func Take[T any](src Enumerator[T], count int) Enumerator[T]

Take returns a specified number of contiguous elements from the start of a sequence.

func TakeLast

func TakeLast[T any](src Enumerator[T], count int) Enumerator[T]

TakeLast returns a new enumerable collection that contains the last count elements from source.

func TakeWhile

func TakeWhile[T any](src Enumerator[T], pred func(T) (bool, error)) Enumerator[T]

TakeWhile returns elements from a sequence as long as a specified condition is true, and then skips the remaining elements.

func Union

func Union[T any](first, second Enumerator[T], equals func(T, T) (bool, error), getHashCode func(T) (int, error)) Enumerator[T]

Union produces the set union of two sequences by using the specified comparer functions.

func UnionBy

func UnionBy[T any, K comparable](first, second Enumerator[T], keySelector func(v T) (K, error)) Enumerator[T]

UnionBy produces the set union of two sequences according to a specified key selector function.

func Where

func Where[T any](src Enumerator[T], pred func(v T) (bool, error)) Enumerator[T]

Where filters a sequence of values based on a predicate.

func Zip

func Zip[S1, S2, T any](first Enumerator[S1], second Enumerator[S2], resultSelector func(S1, S2) (T, error)) Enumerator[T]

Zip applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.

type Error

type Error string

Error : LINQ error type.

const (
	// EOC : End of the collection.
	EOC Error = "End of the collection"

	// OutOfRange : Index out of range.
	OutOfRange Error = "Out of range"

	// InvalidOperation : Invalid operation such as no element satisfying the condition.
	InvalidOperation Error = "Invalid operation"
)

func (Error) Error

func (e Error) Error() string

type Grouping

type Grouping[K comparable, T any] interface {
	Enumerator[T]
	Key() K
}

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

type KeyValue

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

KeyValue pair as an element of map[K]V

type OrderedEnumerator

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

OrderedEnumerator is an implementation of Enumerator[T] which produces a sorted sequence.

func OrderBy

func OrderBy[T any, K constraints.Ordered](src Enumerator[T], keySelector func(T) (K, error)) *OrderedEnumerator[T]

OrderBy sorts the elements of a sequence in ascending order according to a key.

func OrderByDescending

func OrderByDescending[T any, K constraints.Ordered](src Enumerator[T], keySelector func(T) (K, error)) *OrderedEnumerator[T]

OrderByDescending sorts the elements of a sequence in descending order according to a key.

func ThenBy

func ThenBy[T any, K constraints.Ordered](src *OrderedEnumerator[T], keySelector func(T) (K, error)) *OrderedEnumerator[T]

ThenBy performs a subsequent ordering of the elements in a sequence in ascending order according to a key.

func ThenByDescending

func ThenByDescending[T any, K constraints.Ordered](src *OrderedEnumerator[T], keySelector func(T) (K, error)) *OrderedEnumerator[T]

ThenByDescending performs a subsequent ordering of the elements in a sequence in descending order, according to a key.

func (*OrderedEnumerator[T]) Next

func (e *OrderedEnumerator[T]) Next() (def T, _ error)

type Real

type Real interface {
	constraints.Integer | constraints.Float
}

Jump to

Keyboard shortcuts

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