glinq

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2022 License: MIT Imports: 4 Imported by: 0

README

glinq

A collection of generic data structures and LINQ written in Go.

data structures include:

  • List
  • Linked List
  • Array
  • Stack
  • B Tree

examples:

var employees = NewListOf([]Employee{
	{"Hao", 44, 0, 8000.5},
	{"Bob", 36, 10, 5000.5},
	{"Alice", 23, 5, 9000.0},
	{"Jack", 26, 0, 4000.0},
	{"Tom", 48, 9, 7500.75},
	{"Marry", 29, 0, 6000.0},
	{"Mike", 38, 8, 4000.3},
})

func TestEmployeeList(t *testing.T) {
	// 添加 Max 员工
	employees.Push(Employee{"Max", 26, 0, 4000.0})
	// 所有人的薪水
	totalPaySalary := Reduce(employees.ToQueryable(), 0.0, func(result float64, employee Employee) float64 {
		return result + employee.Salary
	})
	assert.Equal(t, totalPaySalary, 43502.05+4000.0)

	//统计年龄大于40岁的员工数
	orderCount := employees.ToQueryable().Where(func(employee Employee) bool {
		return employee.Age > 35
	}).Count()
	assert.Equal(t, orderCount, 4)

	//统计薪水超过 6000元的员工数
	moreThan6000Count := employees.ToQueryable().Where(func(employee Employee) bool {
		return employee.Salary >= 6000
	}).Count()
	assert.Equal(t, moreThan6000Count, 4)

	//统计年龄小于30岁员工要支付的所有薪水 [ Max: 4000.0, Alice: 9000.0 ,Jack: 4000.0, Marry: 6000.0 ] = 23000.0
	employeeQuery := employees.ToQueryable()
	// Reduce are equal Sum of Map to Queryable
	youngerTotalPaySalary := Reduce(Map(employeeQuery.Where(func(employee Employee) bool {
		return employee.Age < 30
	}), func(e Employee) float64 {
		return e.Salary
	}), 0.0, func(result float64, item float64) float64 {
		return result + item
	})
	assert.Equal(t, youngerTotalPaySalary, 23000.0)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorCannotFound      = errors.New("can not find element")
	ErrorIndexoutOfBounds = errors.New("index out of bounds")
)

Functions

func DoWhile

func DoWhile[T any](enumerator IEnumerator[T], action func(T) bool)

func Equal

func Equal[T1, T2 any](a T1, b T2) bool

func Reduce

func Reduce[T1, T2 any](from Queryable[T1], initializer T2, f func(T2, T1) T2) T2

Reduce uses the summary function Queryable[T1] summary into one result of type T2.

func Sum

func Sum[T Number](query Queryable[T]) T

Types

type ArrayList

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

ArrayList Array List for the slice

func NewList

func NewList[T any]() *ArrayList[T]

NewList new list of empty slice

func NewListOf

func NewListOf[T any](array []T) *ArrayList[T]

NewListOf new list of slice

func (*ArrayList[T]) Contains

func (list *ArrayList[T]) Contains(elem T) bool

Contains return bool,that element is in the list.

func (*ArrayList[T]) Count

func (list *ArrayList[T]) Count() int

Count return a number, that's list elements count.

func (*ArrayList[T]) GetEnumerator

func (list *ArrayList[T]) GetEnumerator() IEnumerator[T]

GetEnumerator get enumerable object

func (*ArrayList[T]) IndexOf

func (list *ArrayList[T]) IndexOf(T) int

func (*ArrayList[T]) Push

func (list *ArrayList[T]) Push(x T)

Push adds an element to the list

func (*ArrayList[T]) Remove

func (list *ArrayList[T]) Remove(item T)

Remove removes an element from the list.

func (*ArrayList[T]) RemoveAt

func (list *ArrayList[T]) RemoveAt(i int)

RemoveAt removes an element from the list by index.

func (*ArrayList[T]) ToQueryable

func (list *ArrayList[T]) ToQueryable() Queryable[T]

ToQueryable from ArrayList[T] to Queryable[T

func (*ArrayList[T]) ToSlice

func (list *ArrayList[T]) ToSlice() []T

ToSlice from ArrayList[T] to []T

type BTree added in v0.2.0

type BTree[K, V any] struct {
	// contains filtered or unexported fields
}

BTree a B-tree data structures

func NewBTree added in v0.2.0

func NewBTree[K, V any](less g.LessFn[K]) *BTree[K, V]

NewBTree New returns an empty B-tree.

func (*BTree[K, V]) Each added in v0.2.0

func (t *BTree[K, V]) Each(fn func(key K, val V))

Each calls 'fn' on every BTreeNode in the tree in order.

func (*BTree[K, V]) Get added in v0.2.0

func (t *BTree[K, V]) Get(key K) (V, bool)

Get returns the value associated with 'key'.

func (*BTree[K, V]) Put added in v0.2.0

func (t *BTree[K, V]) Put(key K, val V)

Put associates 'key' with 'val'.

func (*BTree[K, V]) Remove added in v0.2.0

func (t *BTree[K, V]) Remove(key K)

Remove removes the value associated with 'key'.

func (*BTree[K, V]) Size added in v0.2.0

func (t *BTree[K, V]) Size() int

Size returns the number of elements in the tree.

type BTreeNode added in v0.2.0

type BTreeNode[K, V any] struct {
	// contains filtered or unexported fields
}

type ICollection

type ICollection[T any] interface {
	Push(T)
	RemoveAt(int)
	Remove(T)
	Count() int
	Contains(T) bool
	IndexOf(T) int
	GetEnumerator() IEnumerator[T]
}

type IEnumerator

type IEnumerator[T any] interface {
	Next() bool
	Reset()
	Value() (T, bool)
}

type LinkedList

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

LinkedList is double linked list data structure

func NewLinkedList

func NewLinkedList[T any]() *LinkedList[T]

NewLinkedList creates a new linked list of empty.

func NewLinkedListOf

func NewLinkedListOf[T any](array []T) *LinkedList[T]

NewLinkedListOf creates a new linked list of array.

func (*LinkedList[T]) Contains

func (lst *LinkedList[T]) Contains(elem T) bool

Contains return bool,that element is in the list.

func (*LinkedList[T]) Count

func (lst *LinkedList[T]) Count() int

Count return a number, that's list elements count.

func (*LinkedList[T]) GetEnumerator

func (lst *LinkedList[T]) GetEnumerator() IEnumerator[T]

GetEnumerator get enumerable object

func (*LinkedList[T]) IndexOf

func (lst *LinkedList[T]) IndexOf(elem T) int

func (*LinkedList[T]) IsEmpty

func (lst *LinkedList[T]) IsEmpty() bool

IsEmpty list is empty?

func (*LinkedList[T]) Push

func (lst *LinkedList[T]) Push(v T)

Push adds an element to the list

func (*LinkedList[T]) Remove

func (lst *LinkedList[T]) Remove(data T)

Remove removes an element from the list.

func (*LinkedList[T]) RemoveAt

func (lst *LinkedList[T]) RemoveAt(index int)

RemoveAt removes an element from the list by index.

func (*LinkedList[T]) ToQueryable

func (lst *LinkedList[T]) ToQueryable() Queryable[T]

ToQueryable from LinkedList[T] to Queryable[T

func (*LinkedList[T]) ToSlice

func (lst *LinkedList[T]) ToSlice() []T

ToSlice from ArrayList[T] to []T

type LinkedListEnumerable

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

LinkedListEnumerable 支持遍历链表元素。

func (*LinkedListEnumerable[T]) Next

func (it *LinkedListEnumerable[T]) Next() bool

Next 移动迭代器到下一个元互。 如果已经到达尾部则返回 false。

func (*LinkedListEnumerable[T]) Reset

func (it *LinkedListEnumerable[T]) Reset()

func (*LinkedListEnumerable[T]) Value

func (it *LinkedListEnumerable[T]) Value() (T, bool)

Value 返回当前元素内容。 如果元素为空 bool 值为 false。

type ListEnumerable

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

func (*ListEnumerable[T]) Next

func (e *ListEnumerable[T]) Next() bool

func (*ListEnumerable[T]) Reset

func (e *ListEnumerable[T]) Reset()

func (*ListEnumerable[T]) Value

func (e *ListEnumerable[T]) Value() (T, bool)

type Number

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

type Ordered

type Ordered interface {
	Number | string
}

type Queryable

type Queryable[T any] []T

Queryable query collection

func From

func From[T any](items []T) Queryable[T]

func Just

func Just[T any](items ...T) Queryable[T]

func Map

func Map[T1, T2 any](from Queryable[T1], f func(T1) T2) Queryable[T2]

Map uses the mapping function to convert Queryable[T1] into Queryable[T2]

func Range

func Range[T Number](n T, m T) Queryable[T]

func (Queryable[T]) All

func (query Queryable[T]) All(predicate func(T) bool) bool

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

func (Queryable[T]) Any

func (query Queryable[T]) Any(predicate func(T) bool) bool

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

func (Queryable[T]) Count

func (query Queryable[T]) Count() int

Count this queryable collection's count.

func (Queryable[T]) CountIf

func (query Queryable[T]) CountIf(predicate func(T) bool) int

CountIf count of collection satisfy a condition.

func (Queryable[T]) Distinct

func (query Queryable[T]) Distinct() Queryable[T]

Distinct return collection for duplicate removal.

func (Queryable[T]) First

func (query Queryable[T]) First() (T, error)

First return the first element of a collection.

func (Queryable[T]) FirstIf

func (query Queryable[T]) FirstIf(predicate func(T) bool) (T, error)

FirstIf return first element of collection satisfy a condition.

func (Queryable[T]) ForEach

func (query Queryable[T]) ForEach(action func(int, T))

ForEach performs the specified action on each element of a collection.

func (Queryable[T]) GetEnumerator

func (query Queryable[T]) GetEnumerator() IEnumerator[T]

GetEnumerator get enumerable object

func (Queryable[T]) Skip

func (query Queryable[T]) Skip(n int) Queryable[T]

Skip [n:], n times and take elements for collection

func (Queryable[T]) Sort

func (query Queryable[T]) Sort(cmp func(T, T) bool)

Sort return the sort element of a collection by compare function.

func (Queryable[T]) Take

func (query Queryable[T]) Take(n int) Queryable[T]

Take [:n], n times for elements in collection.

func (Queryable[T]) ToChannel

func (query Queryable[T]) ToChannel(result chan<- T)

func (Queryable[T]) ToSlice

func (query Queryable[T]) ToSlice() []T

ToSlice Queryable to slice

func (Queryable[T]) Where

func (query Queryable[T]) Where(predicate func(T) bool) Queryable[T]

Where filters a collection of values based on a predicate.

type Stack

type Stack[T any] []T

func NewStack

func NewStack[T any]() *Stack[T]

func NewStackOf

func NewStackOf[T any](from []T) *Stack[T]

func (*Stack[T]) Count

func (s *Stack[T]) Count() int

func (*Stack[T]) GetEnumerator

func (s *Stack[T]) GetEnumerator() IEnumerator[T]

GetEnumerator get enumerable object

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() *T

func (*Stack[T]) Push

func (s *Stack[T]) Push(elem T)

func (*Stack[T]) ToQueryable

func (s *Stack[T]) ToQueryable() Queryable[T]

func (*Stack[T]) ToSlice

func (s *Stack[T]) ToSlice() []T

func (*Stack[T]) Top

func (s *Stack[T]) Top() *T

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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