glinq

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: MIT Imports: 3 Imported by: 2

README

Glinq

Go Reference GitHub go.mod Go version GitHub tag (latest by date)

Go implemention of LINQ using generics

Examples


package main

import (
	"github.com/rlarkin212/glinq"
)

type user struct {
	name string
	age  int
}

func main() {
	users := []user{
		{
			name: "a",
			age:  20,
		},
		{
			name: "b",
			age:  12,
		},
		{
			name: "c",
			age:  5,
		},
		{
			name: "d",
			age:  90,
		},
	}

	userNames := glinq.Select(users, func(x user) string {
		return x.name
	})
    
    //output: [a b c d]

	userAges := glinq.Select(users, func(x user) int {
		return x.age
	})

    //output: [20 12 5 90]

	maxAge := glinq.Max(userAges)

    //output: 90

    minAge := glinq.Min(userAges)

    //output: 5

	ageAverage := glinq.Average(userAges)

    //output: 31.75

	orderByAgeAscending := glinq.OrderBy(users, func(x user) int {
		return x.age
	})

    //output: [{c 5} {b 12} {a 20} {d 90}]

	orderByAgeDescending := glinq.OrderByDescending(users, func(x user) int {
		return x.age
	})

    //output: [{d 90} {a 20} {b 12} {c 5}]
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v1.0.4

func All[T any](s []T, f func(x T) bool) bool

All return true if all items in slice satisfy func

func Any

func Any[T comparable](s []T, f func(T) bool) bool

Any return true if item in slice satisfies func

func Average added in v1.0.3

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

Average return the average of items in slice

func Contains

func Contains[T comparable](term T, s []T) bool

Contains returns true if term is contained in slice, else false

func Distinct added in v1.0.4

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

Distinct returns slice of distinct values

func Except added in v1.0.9

func Except[T comparable](s []T, s1 []T) []T

Except retusn slice of items in first slice but not in second

func Exists added in v1.0.4

func Exists[T comparable](s []T, item T) bool

Exists return true if item exists in slice

func First added in v1.0.7

func First[T any](s []T) (T, error)

First returns first item in slice or error if slice contains no items

func FirstWhere added in v1.0.7

func FirstWhere[T any](s []T, f func(x T) bool) (T, error)

FirstWhere returns first item in slice in slice that satisfies func or error if slice contains no items

func Flatten added in v1.0.8

func Flatten[T any](s [][]T) []T

Flatten returns slice from slice of slice

func Index

func Index[T comparable](term T, s []T) int

Index returns index of item if found in slice else returns -1

func Intersct added in v1.0.9

func Intersct[T comparable](s []T, s1 []T) []T

Intersect returns slice of items that are in both slices

func Join added in v1.0.4

func Join[T any](s ...[]T) []T

Join merges the provided slices into one

func JoinWhere added in v1.0.4

func JoinWhere[T any](f func(x T) bool, s ...[]T) []T

JoinWhere merges items in provided slices where item satisfies func

func Last added in v1.0.7

func Last[T any](s []T) (T, error)

Last returns last item in slice or error if slice contains no items

func LastWhere added in v1.0.7

func LastWhere[T any](s []T, f func(x T) bool) (T, error)

LastWhere returns last item in slice in slice that satisfies func or error if slice contains no items

func Max added in v1.0.3

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

Max returns max value in slice

func Min added in v1.0.3

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

Min returns min value of slice

func OrderBy added in v0.1.7

func OrderBy[T any, T2 constraints.Ordered](s []T, f func(x T) T2) []T

OrderBy returns slice with items ordered by returned item in func in ascending order

func OrderByDescending added in v0.1.7

func OrderByDescending[T any, T1 constraints.Ordered](s []T, f func(x T) T1) []T

OrderByDescending returns slice with items ordered by returned item in func in descending order

func Range added in v0.1.5

func Range[T any](s []T, start int, end int) []T

Range returns slice of items between index start & end

func Reduce added in v1.0.6

func Reduce[T any](s []T, f func(x T, y T) T, initial T) T

Reduce executes a user-supplied "reducer" function on each element of the slice

func Reverse added in v1.0.9

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

func Select added in v0.1.5

func Select[T any, T2 any](s []T, f func(T) T2) []T2

Select return slice of T based on supplied func

func Skip added in v1.0.7

func Skip[T any](s []T, n int) ([]T, error)

Skip skips over supplied number iof items and returns rest of slice

func Sum

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

Sum returns sum of items in slice

func Take added in v1.0.7

func Take[T any](s []T, n int) ([]T, error)

Take returns suppleid number of items from slice

func Where

func Where[T comparable](s []T, f func(T) bool) []T

Where return slice of items where item in slice satisfies func

Types

type Number

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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