Documentation ¶
Index ¶
- func Aggregate[T any](f func(T, T) T) func(q Query[T]) T
- func AggregateWithSeed[T any](seed T, f func(T, T) T) func(q Query[T]) (t T)
- func AggregateWithSeedBy[T any, V any](seed T, f func(T, T) T, resultSelector func(T) V) func(q Query[T]) (v V)
- func All[T any](predicates ...func(T) bool) func(q Query[T]) bool
- func Any[T any](q Query[T]) bool
- func AnyWith[T any](predicates ...func(T) bool) func(q Query[T]) bool
- func Average[T constraints.Integer | constraints.Float](q Query[T]) (r float64)
- func Contains[T comparable](value T) func(q Query[T]) bool
- func Count[T any](q Query[T]) (r int)
- func CountWith[T any](predicates ...func(T) bool) func(q Query[T]) (r int)
- func DistinctBy[T any, V comparable](selector func(T) V) func(q Query[T]) Query[T]
- func Eq[T comparable](v T) func(t T) bool
- func ExceptBy[T any, V comparable](selector func(T) V) func(q, q2 Query[T]) Query[T]
- func First[T any](q Query[T]) (T, bool)
- func FirstWith[T any](predicates ...func(T) bool) func(q Query[T]) (T, bool)
- func ForEach[T any](action func(T)) func(q Query[T])
- func ForEachIndexed[T any](action func(int, T)) func(q Query[T])
- func GroupBy[T any, K comparable, V any](keySelector func(T) K, elementSelector func(T) V) func(q Query[T]) Query[Group[K, V]]
- func GroupJoin[T, V, O any, K comparable](outerKeySelector func(T) K, innerKeySelector func(V) K, ...) func(q Query[T], inner Query[V]) Query[O]
- func Gt[T constraints.Ordered](v T) func(t T) bool
- func Gte[T constraints.Ordered](v T) func(t T) bool
- func In[T comparable](v ...T) func(t T) bool
- func IndexOf[T any](predicates ...func(T) bool) func(q Query[T]) int
- func IntersectBy[T any, V comparable](selector func(T) V) func(q, q2 Query[T]) Query[T]
- func Join[T, V, Q any, K comparable](outerKeySelector func(T) K, innerKeySelector func(V) K, ...) func(q Query[T], inner Query[V]) Query[Q]
- func Last[T any](q Query[T]) (r T, exist bool)
- func LastWith[T any](predicates ...func(T) bool) func(q Query[T]) (r T, exist bool)
- func Lt[T constraints.Ordered](v T) func(t T) bool
- func Lte[T constraints.Ordered](v T) func(t T) bool
- func Max[T any](compare func(t1, t2 T) int) func(q Query[T]) (r T, exist bool)
- func Min[T any](compare func(t1, t2 T) int) func(q Query[T]) (r T, exist bool)
- func Neq[T comparable](v T) func(t T) bool
- func Nil(t any) bool
- func NonNil(t any) bool
- func Not[T any](predicate func(T) bool) func(T) bool
- func NotIn[T comparable](v ...T) func(t T) bool
- func OrderBy[T, V any](compare func(V, V) int, selector func(T) V) func(q Query[T]) OrderedQuery[T]
- func OrderByDescending[T, V any](compare func(V, V) int, selector func(T) V) func(q Query[T]) OrderedQuery[T]
- func Predicates[T any](predicates ...func(T) bool) func(T) bool
- func PredicatesIndexed[T any](predicates ...func(int, T) bool) func(int, T) bool
- func Results[T any](q Query[T]) (r []T)
- func Select[T, V any](selector func(T) V) func(q Query[T]) Query[V]
- func SelectIndexed[T, V any](selector func(int, T) V) func(q Query[T]) Query[V]
- func SelectMany[T, V any](selector func(T) Query[V]) func(q Query[T]) Query[V]
- func SelectManyBy[T, V, O any](selector func(T) Query[V], resultSelector func(V, T) O) func(q Query[T]) Query[O]
- func SelectManyByIndexed[T, V, O any](selector func(int, T) Query[V], resultSelector func(V, T) O) func(q Query[T]) Query[O]
- func SelectManyIndexed[T, V any](selector func(int, T) Query[V]) func(q Query[T]) Query[V]
- func Self[T any](t T) T
- func SequenceEqual[T comparable](q, q2 Query[T]) bool
- func Single[T any](q Query[T]) (r T, found bool)
- func SingleWith[T any](predicates ...func(T) bool) func(q Query[T]) (r T, found bool)
- func SkipWhile[T any](predicates ...func(T) bool) func(q Query[T]) Query[T]
- func SkipWhileIndexed[T any](predicates ...func(int, T) bool) func(q Query[T]) Query[T]
- func Sort[T any](less func(i, j T) bool) func(q Query[T]) Query[T]
- func Sum[T constraints.Integer | constraints.Float](q Query[T]) (r T)
- func TakeWhile[T any](predicates ...func(T) bool) func(q Query[T]) Query[T]
- func TakeWhileIndexed[T any](predicates ...func(int, T) bool) func(q Query[T]) Query[T]
- func ThenBy[T, V any](compare func(V, V) int, selector func(T) V) func(oq OrderedQuery[T]) OrderedQuery[T]
- func ThenByDescending[T, V any](compare func(V, V) int, selector func(T) V) func(oq OrderedQuery[T]) OrderedQuery[T]
- func ToChannel[T any](q Query[T], result chan<- T)
- func ToMap[K comparable, V any](q Query[KeyValue[K, V]]) map[K]V
- func ToMapBy[K comparable, V, T any](keySelector func(T) K, valueSelector func(T) V) func(q Query[T]) map[K]V
- func ToMapFromGroup[K comparable, V any](q Query[Group[K, V]]) map[K][]V
- func ToSlice[T any](q Query[T]) []T
- func ToString(q Query[rune]) string
- func ValidateQuery[T comparable](q Query[T], output []T) bool
- func Where[T any](predicates ...func(T) bool) func(q Query[T]) Query[T]
- func WhereIndexed[T any](predicates ...func(int, T) bool) func(q Query[T]) Query[T]
- func Zip[T, V, O any](resultSelector func(T, V) O) func(q Query[T], q2 Query[V]) Query[O]
- type Group
- type Iterator
- type KeyValue
- type OrderedQuery
- type Query
- func Append[T any](q Query[T], items ...T) Query[T]
- func Concat[T any](q, q2 Query[T]) Query[T]
- func DefaultIfEmpty[T any](q Query[T], defaultValue T) Query[T]
- func Distinct[T comparable](q Query[T]) Query[T]
- func Except[T comparable](q, q2 Query[T]) Query[T]
- func FromChannel[T any](source <-chan T) Query[T]
- func FromIterable[T any](sourceFunc Iterator[T]) Query[T]
- func FromMap[K comparable, V any](source map[K]V) Query[KeyValue[K, V]]
- func FromSlice[T any](source []T) Query[T]
- func FromString(source string) Query[rune]
- func Intersect[T comparable](q, q2 Query[T]) Query[T]
- func Prepend[T any](q Query[T], items ...T) Query[T]
- func Range(start, count int) Query[int]
- func Repeat[T any](value T, count int) Query[T]
- func Reverse[T any](q Query[T]) Query[T]
- func Skip[T any](q Query[T], count int) Query[T]
- func Take[T any](q Query[T], count int) Query[T]
- func Union[T comparable](q, q2 Query[T]) Query[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Aggregate ¶
Aggregate applies an accumulator function over a sequence.
Aggregate method makes it simple to perform a calculation over a sequence of values. This method works by calling f() one time for each element in source except the first one. Each time f() is called, Aggregate passes both the element from the sequence and an aggregated value (as the first argument to f()). The first element of source is used as the initial aggregate value. The result of f() replaces the previous aggregated value.
Aggregate returns the final result of f().
func AggregateWithSeed ¶
AggregateWithSeed applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
Aggregate method makes it simple to perform a calculation over a sequence of values. This method works by calling f() one time for each element in source except the first one. Each time f() is called, Aggregate passes both the element from the sequence and an aggregated value (as the first argument to f()). The value of the seed parameter is used as the initial aggregate value. The result of f() replaces the previous aggregated value.
Aggregate returns the final result of f().
func AggregateWithSeedBy ¶
func AggregateWithSeedBy[T any, V any](seed T, f func(T, T) T, resultSelector func(T) V) func(q Query[T]) (v V)
AggregateWithSeedBy 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.
Aggregate method makes it simple to perform a calculation over a sequence of values. This method works by calling f() one time for each element in source. Each time func is called, Aggregate passes both the element from the sequence and an aggregated value (as the first argument to func). The value of the seed parameter is used as the initial aggregate value. The result of func replaces the previous aggregated value.
The final result of func is passed to resultSelector to obtain the final result of Aggregate.
func Average ¶
func Average[T constraints.Integer | constraints.Float](q Query[T]) (r float64)
Average computes the average of a collection of numeric values.
func Contains ¶
func Contains[T comparable](value T) func(q Query[T]) bool
Contains determines whether a collection contains a specified element.
func CountWith ¶
CountWith returns a number that represents how many elements in the specified collection satisfy a condition.
func DistinctBy ¶
func DistinctBy[T any, V comparable](selector func(T) V) func(q Query[T]) Query[T]
DistinctBy method returns distinct elements from a collection. This method executes selector function for each element to determine a value to compare. The result is an unordered collection that contains no duplicate values.
func Eq ¶ added in v0.0.7
func Eq[T comparable](v T) func(t T) bool
func ExceptBy ¶
func ExceptBy[T any, V comparable](selector func(T) V) func(q, q2 Query[T]) Query[T]
ExceptBy invokes a transform function on each element of a collection and produces the set difference of two sequences. The set difference is the members of the first sequence that don't appear in the second sequence.
func FirstWith ¶
FirstWith returns the first element of a collection that satisfies a specified condition.
func ForEachIndexed ¶
ForEachIndexed performs the specified action on each element of a collection.
The first argument to action represents the zero-based index of that element in the source collection. This can be useful if the elements are in a known order and you want to do something with an element at a particular index, for example. It can also be useful if you want to retrieve the index of one or more elements. The second argument to action represents the element to process.
func GroupBy ¶
func GroupBy[T any, K comparable, V any](keySelector func(T) K, elementSelector func(T) V) func(q Query[T]) Query[Group[K, V]]
GroupBy method groups the elements of a collection according to a specified key selector function and projects the elements for each group by using a specified function.
func GroupJoin ¶
func GroupJoin[T, V, O any, K comparable]( outerKeySelector func(T) K, innerKeySelector func(V) K, resultSelector func(outer T, inners []V) O) func(q Query[T], inner Query[V]) Query[O]
GroupJoin correlates the elements of two collections based on key equality, and groups the results.
This method produces hierarchical results, which means that elements from outer query are paired with collections of matching elements from inner. GroupJoin enables you to base your results on a whole set of matches for each element of outer query.
The resultSelector function is called only one time for each outer element together with a collection of all the inner elements that match the outer element. This differs from the Join method, in which the result selector function is invoked on pairs that contain one element from outer and one element from inner.
GroupJoin preserves the order of the elements of outer, and for each element of outer, the order of the matching elements from inner.
func Gt ¶ added in v0.0.7
func Gt[T constraints.Ordered](v T) func(t T) bool
func Gte ¶ added in v0.0.7
func Gte[T constraints.Ordered](v T) func(t T) bool
func In ¶ added in v0.0.7
func In[T comparable](v ...T) func(t T) bool
func IndexOf ¶
IndexOf searches for an element that matches the conditions defined by a specified predicate and returns the zero-based index of the first occurrence within the collection. This method returns -1 if an item that matches the conditions is not found.
func IntersectBy ¶
func IntersectBy[T any, V comparable](selector func(T) V) func(q, q2 Query[T]) Query[T]
IntersectBy produces the set intersection of the source collection and the provided input collection. The intersection of two sets A and B is defined as the set that contains all the elements of A that also appear in B, but no other elements.
IntersectBy invokes a transform function on each element of both collections.
func Join ¶
func Join[T, V, Q any, K comparable]( outerKeySelector func(T) K, innerKeySelector func(V) K, resultSelector func(outer T, inner V) Q) func(q Query[T], inner Query[V]) Query[Q]
Join correlates the elements of two collection based on matching keys.
A join refers to the operation of correlating the elements of two sources of information based on a common key. Join brings the two information sources and the keys by which they are matched together in one method call. This differs from the use of SelectMany, which requires more than one method call to perform the same operation.
Join preserves the order of the elements of outer collection, and for each of these elements, the order of the matching elements of inner.
func LastWith ¶
LastWith returns the last element of a collection that satisfies a specified condition.
func Lt ¶ added in v0.0.7
func Lt[T constraints.Ordered](v T) func(t T) bool
func Lte ¶ added in v0.0.7
func Lte[T constraints.Ordered](v T) func(t T) bool
func Neq ¶ added in v0.0.7
func Neq[T comparable](v T) func(t T) bool
func NotIn ¶ added in v0.0.7
func NotIn[T comparable](v ...T) func(t T) bool
func OrderBy ¶
func OrderBy[T, V any](compare func(V, V) int, selector func(T) V) func(q Query[T]) OrderedQuery[T]
OrderBy sorts the elements of a collection in ascending order. Elements are sorted according to a key.
func OrderByDescending ¶
func OrderByDescending[T, V any](compare func(V, V) int, selector func(T) V) func(q Query[T]) OrderedQuery[T]
OrderByDescending sorts the elements of a collection in descending order. Elements are sorted according to a key.
func Predicates ¶ added in v0.0.9
func PredicatesIndexed ¶ added in v0.0.9
func Select ¶
Select projects each element of a collection into a new form. Returns a query with the result of invoking the transform function on each element of original source.
This projection method requires the transform function, selector, to produce one value for each value in the source collection. If selector returns a value that is itself a collection, it is up to the consumer to traverse the subcollections manually. In such a situation, it might be better for your query to return a single coalesced collection of values. To achieve this, use the SelectMany method instead of Select. Although SelectMany works similarly to Select, it differs in that the transform function returns a collection that is then expanded by SelectMany before it is returned.
func SelectIndexed ¶
SelectIndexed projects each element of a collection into a new form by incorporating the element's index. Returns a query with the result of invoking the transform function on each element of original source.
The first argument to selector represents the zero-based index of that element in the source collection. This can be useful if the elements are in a known order and you want to do something with an element at a particular index, for example. It can also be useful if you want to retrieve the index of one or more elements. The second argument to selector represents the element to process.
This projection method requires the transform function, selector, to produce one value for each value in the source collection. If selector returns a value that is itself a collection, it is up to the consumer to traverse the subcollections manually. In such a situation, it might be better for your query to return a single coalesced collection of values. To achieve this, use the SelectMany method instead of Select. Although SelectMany works similarly to Select, it differs in that the transform function returns a collection that is then expanded by SelectMany before it is returned.
func SelectMany ¶
SelectMany projects each element of a collection to a Query, iterates and flattens the resulting collection into one collection.
func SelectManyBy ¶
func SelectManyBy[T, V, O any](selector func(T) Query[V], resultSelector func(V, T) O) func(q Query[T]) Query[O]
SelectManyBy projects each element of a collection to a Query, iterates and flattens the resulting collection into one collection, and invokes a result selector function on each element therein.
func SelectManyByIndexed ¶
func SelectManyIndexed ¶
SelectManyIndexed projects each element of a collection to a Query, iterates and flattens the resulting collection into one collection.
The first argument to selector represents the zero-based index of that element in the source collection. This can be useful if the elements are in a known order and you want to do something with an element at a particular index, for example. It can also be useful if you want to retrieve the index of one or more elements. The second argument to selector represents the element to process.
func SequenceEqual ¶
func SequenceEqual[T comparable](q, q2 Query[T]) bool
SequenceEqual determines whether two collections are equal.
func Single ¶
Single returns the only element of a collection, and nil if there is not exactly one element in the collection.
func SingleWith ¶
SingleWith returns the only element of a collection that satisfies a specified condition, and nil if more than one such element exists.
func SkipWhile ¶
SkipWhile bypasses elements in a collection as long as a specified condition is true and then returns the remaining elements.
This method tests each element by using predicate and skips the element if the result is true. After the predicate function returns false for an element, that element and the remaining elements in source are returned and there are no more invocations of predicate.
func SkipWhileIndexed ¶
SkipWhileIndexed bypasses elements in a collection as long as a specified condition is true and then returns the remaining elements. The element's index is used in the logic of the predicate function.
This method tests each element by using predicate and skips the element if the result is true. After the predicate function returns false for an element, that element and the remaining elements in source are returned and there are no more invocations of predicate.
func Sort ¶
Sort returns a new query by sorting elements with provided less function in ascending order. The comparer function should return true if the parameter i is less than j. While this method is uglier than chaining OrderBy, OrderByDescending, ThenBy and ThenByDescending methods, it's performance is much better.
func Sum ¶
func Sum[T constraints.Integer | constraints.Float](q Query[T]) (r T)
Sum computes the sum of a collection of numeric values.
Values can be of any integer type: int, int8, int16, int32, int64. The result is int64. Method returns zero if collection contains no elements.
func TakeWhile ¶
TakeWhile returns elements from a collection as long as a specified condition is true, and then skips the remaining elements.
func TakeWhileIndexed ¶
TakeWhileIndexed returns elements from a collection as long as a specified condition is true. The element's index is used in the logic of the predicate function. The first argument of predicate represents the zero-based index of the element within collection. The second argument represents the element to test.
func ThenBy ¶
func ThenBy[T, V any](compare func(V, V) int, selector func(T) V) func(oq OrderedQuery[T]) OrderedQuery[T]
ThenBy performs a subsequent ordering of the elements in a collection in ascending order. This method enables you to specify multiple sort criteria by applying any number of ThenBy or ThenByDescending methods.
func ThenByDescending ¶
func ThenByDescending[T, V any](compare func(V, V) int, selector func(T) V) func(oq OrderedQuery[T]) OrderedQuery[T]
ThenByDescending performs a subsequent ordering of the elements in a collection in descending order. This method enables you to specify multiple sort criteria by applying any number of ThenBy or ThenByDescending methods.
func ToChannel ¶
ToChannel iterates over a collection and outputs each element to a channel, then closes it.
func ToMap ¶
func ToMap[K comparable, V any](q Query[KeyValue[K, V]]) map[K]V
ToMap iterates over a collection and populates result map with elements. Collection elements have to be of KeyValue type to use this method. To populate a map with elements of different type use ToMapBy method. ToMap doesn't empty the result map before populating it.
func ToMapBy ¶
func ToMapBy[K comparable, V, T any](keySelector func(T) K, valueSelector func(T) V) func(q Query[T]) map[K]V
ToMapBy iterates over a collection and populates the result map with elements. Functions keySelector and valueSelector are executed for each element of the collection to generate key and value for the map. Generated key and value types must be assignable to the map's key and value types. ToMapBy doesn't empty the result map before populating it.
func ToMapFromGroup ¶ added in v0.0.11
func ToMapFromGroup[K comparable, V any](q Query[Group[K, V]]) map[K][]V
func ToSlice ¶
ToSlice iterates over a collection and saves the results in the slice pointed by v. It overwrites the existing slice, starting from index 0.
If the slice pointed by v has sufficient capacity, v will be pointed to a resliced slice. If it does not, a new underlying array will be allocated and v will point to it.
func ValidateQuery ¶ added in v0.0.3
func ValidateQuery[T comparable](q Query[T], output []T) bool
func WhereIndexed ¶
WhereIndexed filters a collection of values based on a predicate. Each element's index is used in the logic of the predicate function.
The first argument represents the zero-based index of the element within collection. The second argument of predicate represents the element to test.
func Zip ¶
Zip applies a specified function to the corresponding elements of two collections, producing a collection of the results.
The method steps through the two input collections, applying function resultSelector to corresponding elements of the two collections. The method returns a collection of the values that are returned by resultSelector. If the input collections do not have the same number of elements, the method combines elements until it reaches the end of one of the collections. For example, if one collection has three elements and the other one has four, the result collection has only three elements.
Types ¶
type Group ¶
type Group[K, V any] struct { Key K Group []V }
Group is a type that is used to store the result of GroupBy method.
type KeyValue ¶
type KeyValue[K comparable, V any] struct { Key K Value V }
KeyValue is a type that is used to iterate over a map (if query is created from a map). This type is also used by ToMap() method to output result of a query into a map.
type OrderedQuery ¶
OrderedQuery is the type returned from OrderBy, OrderByDescending ThenBy and ThenByDescending functions.
func DistinctOrderedQuery ¶
func DistinctOrderedQuery[T comparable](oq OrderedQuery[T]) OrderedQuery[T]
DistinctOrderedQuery method returns distinct elements from a collection. The result is an ordered collection that contains no duplicate values.
NOTE: DistinctOrderedQuery method on OrderedQuery type has better performance than Distinct method on Query type.
type Query ¶
Query is the type returned from query functions. It can be iterated manually as shown in the example.
func Concat ¶
Concat concatenates two collections.
The Concat method differs from the Union method because the Concat method returns all the original elements in the input sequences. The Union method returns only unique elements.
func DefaultIfEmpty ¶
DefaultIfEmpty returns the elements of the specified sequence if the sequence is empty.
func Distinct ¶
func Distinct[T comparable](q Query[T]) Query[T]
Distinct method returns distinct elements from a collection. The result is an unordered collection that contains no duplicate values.
func Except ¶
func Except[T comparable](q, q2 Query[T]) Query[T]
Except produces the set difference of two sequences. The set difference is the members of the first sequence that don't appear in the second sequence.
func FromChannel ¶
FromChannel initializes a linq query with passed channel, linq iterates over channel until it is closed.
func FromIterable ¶
FromIterable initializes a linq query with custom collection passed. This collection has to implement Iterable interface, linq iterates over items, that has to implement Comparable interface or be basic types.
func FromString ¶
FromString initializes a linq query with passed string, linq iterates over runes of string.
func Intersect ¶
func Intersect[T comparable](q, q2 Query[T]) Query[T]
Intersect produces the set intersection of the source collection and the provided input collection. The intersection of two sets A and B is defined as the set that contains all the elements of A that also appear in B, but no other elements.
func Prepend ¶
Prepend inserts an item to the beginning of a collection, so it becomes the first item.
func Reverse ¶
Reverse inverts the order of the elements in a collection.
Unlike OrderBy, this sorting method does not consider the actual values themselves in determining the order. Rather, it just returns the elements in the reverse order from which they are produced by the underlying source.
func Skip ¶
Skip bypasses a specified number of elements in a collection and then returns the remaining elements.
func Union ¶
func Union[T comparable](q, q2 Query[T]) Query[T]
Union produces the set union of two collections.
This method excludes duplicates from the return set. This is different behavior to the Concat method, which returns all the elements in the input collection including duplicates.