ysq

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2023 License: MIT Imports: 6 Imported by: 4

README

ysq

Build Status License Go Report Card Coverage Status

Go Version

go≥1.19

Install

go get -v github.com/yeungsean/ysq

Example Usage

package main

import (
    "fmt"

    "github.com/yeungsean/ysq"
    "github.com/yeungsean/ysq/pkg/delegate"
)

func main() {
}

func castInterface() {
    slice := []int64{1,2,3,4,5,6}
    interfaceSlice := ysq.FromSlice(slice).CastToInterface().ToSlice()
    printArgs := func(args []interface{}) {
        fmt.Printf("%#v\n", args)
    }
    printArgs(interfaceSlice)
}

func getTop3Element() {
    slice := []int64{1,2,3,4,5,6,7,8,9,10}
    res := ysq.FromSlice(slice).Take(3).ToSlice()
    fmt.Println(res) // [1,2,3]
}

func pager() {
    res := ysq.FromSequence(1, 20).Skip(10).Take(5).ToSlice(5)
    fmt.Println(res) // [11,12,13,14,15]
}

func sequence() {
    res1 := ysq.FromSequence(1, 10)
    fmt.Println(res1) // [1,2,3,4,5,6,7,8,9]

    res2 := ysq.FromSequence(1, 10, 2)
    fmt.Println(res2) // [1,3,5,7,9]
}

func filter() {
    // or Where
    res := ysq.FromSequence(1, 20).Filter(func(i int) bool {
        return i < 10
    }).ToSlice(10)
    fmt.Println(res) // [1, 2, 3, 4, 5, 6, 7, 8, 9]
}

func contains() {
    // or In
    res := ysq.FromSequence(1, 100).Contains(func(i int) bool {
        return i%2 == 0
    })
    fmt.Println(res) // true

    res = ysq.FromSequence(1, 100).In(func(i int) bool {
        return i == 10
    })
    fmt.Println(res) // true

    res = ysq.FromSequence(1, 100).Contains(func(i int) bool {
        return i > 1000
    })
    fmt.Println(res) // false
}

func all() {
    res := ysq.FromSequence(1, 100).All(func(i int) bool {
        return i < 1000
    })
    fmt.Println(res) // true

    res = ysq.FromSlice([]int{1,3,5,7,9}).All(func(i int) bool {
        return i%2 == 0
    })
    fmt.Println(res) // false
}

func mapReduce() {
    func() {
        res := ysq.FromSequence(1, 11).Select(func(v int) int {
            return v + 1
        }).Reduce(0, func(total, current int) int {
            return total + current
        })
        fmt.Println(res) // 65
    }()

    func() {
        res := ysq.FromSequence(1, 11).Select(func(v int) int {
            return v + 1
        }).SumToInt(func(current int) int {
            return current
        })
        fmt.Println(res) // 65
    }()
}

// like python
func partial() {
    tmpAction2 := func(arg1, arg2 int) {
        fmt.Println(arg1, arg2)
    }
    func() {
        var fa delegate.Action2[int, int] = tmpAction2
        delayCall := fa.Partial(5)
        delayCall(10) // print 5, 10
        delayCall(100) // print 5, 100
    }()

    tmpSumFunc2 := func(arg1, arg2 int) int {
        return arg1 + arg2
    }
    func() {
        var ff2 delegate.Func2[int, int, int] = tmpSumFunc2
        delayCall := ff2.Partial(5)
        res := delayCall(10)
        fmt.Println(res) // 15

        res = delayCall(-10)
        fmt.Println(res) // -5
    }()
}

Documentation

Overview

Package ysq ...

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDataNotfound 数据未找到
	ErrDataNotfound = errors.New(`not found`)
)

Functions

func CastToString

func CastToString[T any]() func(T) string

CastToString 转string的helper

func NumberComparer

func NumberComparer[T constraints.Integer | constraints.Float](prev, current T) int

NumberComparer ...

func Reduce

func Reduce[T, TResult any](q *Query[T], initializer TResult, f delegate.Func2[TResult, T, TResult]) TResult

Reduce reduces a []T1 to a single value using a reduction function.

func Sum

func Sum[T any, R constraints.Integer | constraints.Float](q *Query[T], selector func(T) R) R

Sum ...

func ToMap

func ToMap[T any, TKey comparable](q *Query[T], keySelector delegate.Func1[T, TKey]) map[TKey]T

ToMap 从 Query<T> 创建一个 map[TKey]T

Types

type ChanResult

type ChanResult[T any] struct {
	sync.Once
	Ch chan T
	// contains filtered or unexported fields
}

ChanResult ...

func (*ChanResult[T]) Close

func (cr *ChanResult[T]) Close()

Close 关闭channel

func (*ChanResult[T]) CloseWithTimeout

func (cr *ChanResult[T]) CloseWithTimeout(ts ...time.Duration)

CloseWithTimeout 带超时设定的关闭channel

func (*ChanResult[T]) GetClosed

func (cr *ChanResult[T]) GetClosed() bool

GetClosed 获取closed的标识位

func (*ChanResult[T]) GetWaitClose

func (cr *ChanResult[T]) GetWaitClose() bool

GetWaitClose 获取关闭标识位

func (*ChanResult[T]) SetWaitClose

func (cr *ChanResult[T]) SetWaitClose(v bool)

SetWaitClose 设置关闭标识位

type Comparable

type Comparable interface {
	CompareTo(Comparable) int
}

Comparable 比较器

type Comparer

type Comparer[T any] func(prev, current T) int

Comparer 大小比较器

type GetHashCoder

type GetHashCoder[T any] func(T) int64

GetHashCoder 获取hash值

type IterContinue

type IterContinue bool

IterContinue 终止迭代标记

const (
	// IterContinueYes 继续迭代
	IterContinueYes IterContinue = true
	// IterContinueNo 停止迭代
	IterContinueNo IterContinue = false
)

type Iterable

type Iterable[T any] interface {
	Next() Iterator[T]
}

Iterable ...

type Iterator

type Iterator[T any] func() (item T, ok bool)

Iterator 迭代器

type KeyListPair

type KeyListPair[K, V any] struct {
	Key  K
	List []V
}

KeyListPair 键列表对

type KeyValuePair

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

KeyValuePair 键值对

type Query

type Query[T any] struct {
	Next func() Iterator[T]
}

Query 查询器

func Cast

func Cast[T, TResult any](q *Query[T], caster func(T) TResult) *Query[TResult]

Cast 类型转换

func FromChan

func FromChan[T any](c chan T) *Query[T]

FromChan chan -> Query

func FromElement

func FromElement[T any](source ...T) *Query[T]

FromElement element list -> Query

func FromMap

func FromMap[K comparable, V any](m map[K]V) *Query[KeyValuePair[K, V]]

FromMap map -> Query

func FromSequence

func FromSequence[T constraints.Integer](start, end T, stepE ...int) *Query[T]

FromSequence sequence -> Query [start, end)

func FromSequenceChan

func FromSequenceChan[T constraints.Integer](start, end T, stepE ...int) *Query[T]

FromSequenceChan sequence chan -> Query [start, end)

func FromSlice

func FromSlice[T any](source []T) *Query[T]

FromSlice slice -> Query

func FromString

func FromString(s string) *Query[rune]

FromString string -> Query

func GroupBy

func GroupBy[T any, TKey comparable](
	q *Query[T],
	keySelector func(T) TKey,
) *Query[KeyListPair[TKey, T]]

GroupBy 分组归类,不统计

func GroupByWithC

func GroupByWithC[T any, TKey comparable, TResult any](
	q *Query[T],
	keySelector func(T) TKey,
	resultSelector func(TKey, []T) TResult,
) *Query[KeyValuePair[TKey, TResult]]

GroupByWithC 根据自定义逻辑分组统计

func Select

func Select[T, TResult any](q *Query[T], selector func(T) TResult) *Query[TResult]

Select 将序列中的每个元素投影到新表单

func SelectMany

func SelectMany[T, TResult any](q *Query[T], selector func(T) *Query[TResult]) *Query[TResult]

SelectMany 将序列的每个元素投影并将结果序列合并为一个序列

func Zip

func Zip[T, TSecond, TResult any](
	q *Query[T],
	list []TSecond,
	resultSelector func(T, TSecond) TResult,
) *Query[TResult]

Zip 将指定函数应用于两个序列的对应元素,以生成结果序列

func ZipQ

func ZipQ[T, TSecond, TResult any](
	q *Query[T],
	qs *Query[TSecond],
	resultSelector func(T, TSecond) TResult,
) *Query[TResult]

ZipQ 将指定函数应用于两个序列的对应元素,以生成结果序列

func (*Query[T]) All

func (q *Query[T]) All(predicate delegate.FuncTBool[T]) bool

All 确定序列中的所有元素是否都满足条件

func (*Query[T]) Any

func (q *Query[T]) Any() bool

Any 序列是否不为空

func (*Query[T]) CastToInt32By

func (q *Query[T]) CastToInt32By(caster func(T) int32) *Query[int32]

CastToInt32By 转int32

func (*Query[T]) CastToInt64By

func (q *Query[T]) CastToInt64By(caster func(T) int64) *Query[int64]

CastToInt64By 转int64

func (*Query[T]) CastToIntBy

func (q *Query[T]) CastToIntBy(caster func(T) int) *Query[int]

CastToIntBy 转int

func (*Query[T]) CastToInterface

func (q *Query[T]) CastToInterface() *Query[interface{}]

CastToInterface 转interface{}

func (*Query[T]) CastToStringBy

func (q *Query[T]) CastToStringBy(caster func(T) string) *Query[string]

CastToStringBy 转string

func (*Query[T]) Contains

func (q *Query[T]) Contains(predicate delegate.FuncTBool[T]) bool

Contains alias In

func (*Query[T]) Count

func (q *Query[T]) Count() int64

Count 返回序列中的元素数量

func (*Query[T]) CountBy

func (q *Query[T]) CountBy(predicate delegate.FuncTBool[T]) int64

CountBy 返回表示在指定的序列中满足条件的元素数量的数字

func (*Query[T]) Distinct

func (q *Query[T]) Distinct() *Query[T]

Distinct 去重

func (*Query[T]) DistinctBy

func (q *Query[T]) DistinctBy(getter GetHashCoder[T]) *Query[T]

DistinctBy 有条件去重

func (*Query[T]) ElementAt

func (q *Query[T]) ElementAt(index int) (T, error)

ElementAt 返回序列中指定索引处的元素

func (*Query[T]) ElementAtOr

func (q *Query[T]) ElementAtOr(index int, source T) T

ElementAtOr 返回序列中指定索引处的元素;如果索引超出范围,则返回默认值

func (*Query[T]) Except

func (q *Query[T]) Except(other []T) *Query[T]

Except 生成两个序列的差集

func (*Query[T]) ExceptQ

func (q *Query[T]) ExceptQ(otherQ *Query[T]) *Query[T]

ExceptQ 生成两个序列的差集

func (*Query[T]) ExpectBy

func (q *Query[T]) ExpectBy(other []T, getter GetHashCoder[T]) *Query[T]

ExpectBy 生成两个序列的差集

func (*Query[T]) Filter

func (q *Query[T]) Filter(predicate delegate.FuncTBool[T]) *Query[T]

Filter alias Where

func (*Query[T]) FilterN

func (q *Query[T]) FilterN(predicate delegate.FuncTIntBool[T]) *Query[T]

FilterN alias WhereN

func (*Query[T]) First

func (q *Query[T]) First() (T, error)

First 返回序列中第一个元素

func (*Query[T]) FirstBy

func (q *Query[T]) FirstBy(predicate delegate.FuncTBool[T]) (T, error)

FirstBy 返回序列中满足指定条件的第一个元素

func (*Query[T]) FirstOr

func (q *Query[T]) FirstOr(v T) T

FirstOr 返回序列中的第一个元素;如果未找到该元素,则返回默认值

func (*Query[T]) FirstOrBy

func (q *Query[T]) FirstOrBy(v T, predicate delegate.FuncTBool[T]) T

FirstOrBy 返回序列中满足条件的第一个元素;如果未找到这样的元素,则返回默认值

func (*Query[T]) ForEach

func (q *Query[T]) ForEach(action delegate.Action1[T])

ForEach 遍历

func (*Query[T]) ForEachE added in v1.0.2

func (q *Query[T]) ForEachE(f delegate.Func1[T, error]) (err error)

ForEachE 遍历,返回error就中断

func (*Query[T]) ForEachEN added in v1.0.2

func (q *Query[T]) ForEachEN(f delegate.Func2[T, int, error]) (err error)

ForEachEN 遍历,带数字,返回error就中断

func (*Query[T]) ForEachN

func (q *Query[T]) ForEachN(action delegate.Action2[T, int])

ForEachN 遍历,带数字

func (*Query[T]) ForEachx

func (q *Query[T]) ForEachx(action delegate.FuncTBool[T])

ForEachx 可中断的遍历

func (*Query[T]) ForEachxN

func (q *Query[T]) ForEachxN(action delegate.FuncTIntBool[T])

ForEachxN 可中断的遍历,带数字

func (*Query[T]) In

func (q *Query[T]) In(predicate delegate.FuncTBool[T]) bool

In 确定序列是否包含任何元素

func (*Query[T]) Intersect

func (q *Query[T]) Intersect(other []T) *Query[T]

Intersect 生成两个序列的交集

func (*Query[T]) IntersectBy

func (q *Query[T]) IntersectBy(other []T, getter GetHashCoder[T]) *Query[T]

IntersectBy 生成两个序列的交集

func (*Query[T]) IntersectQ

func (q *Query[T]) IntersectQ(otherQ *Query[T]) *Query[T]

IntersectQ 生成两个序列的交集

func (*Query[T]) IsEmpty

func (q *Query[T]) IsEmpty() bool

IsEmpty 序列是否为空

func (*Query[T]) Iter

func (q *Query[T]) Iter(next Iterator[T], predicate func(T) IterContinue) (item T, ok bool)

Iter 迭代

func (*Query[T]) Max

func (q *Query[T]) Max(comparer Comparer[T]) T

Max 最大值

func (*Query[T]) Min

func (q *Query[T]) Min(comparer Comparer[T]) T

Min 最小值

func (*Query[T]) Reduce

func (q *Query[T]) Reduce(initializer T, f delegate.Func2[T, T, T]) T

Reduce reduces a []T1 to a single value using a reduction function.

func (*Query[T]) Reverse

func (q *Query[T]) Reverse(sliceCap ...int) *Query[T]

Reverse 反转

func (*Query[T]) Select

func (q *Query[T]) Select(selector func(T) T) *Query[T]

Select 将序列中的每个元素投影到新表单

func (*Query[T]) Skip

func (q *Query[T]) Skip(cnt int) *Query[T]

Skip 跳过序列中指定数量的元素,然后返回剩余的元素。

func (*Query[T]) SkipWhile

func (q *Query[T]) SkipWhile(predicate delegate.FuncTBool[T]) *Query[T]

SkipWhile 如果指定的条件为 true,则跳过序列中的元素,然后返回剩余的元素。

func (*Query[T]) SumToFloat64

func (q *Query[T]) SumToFloat64(selector func(T) float64) float64

SumToFloat64 ...

func (*Query[T]) SumToInt

func (q *Query[T]) SumToInt(selector func(T) int) int

SumToInt ...

func (*Query[T]) SumToInt32

func (q *Query[T]) SumToInt32(selector func(T) int32) int32

SumToInt32 ...

func (*Query[T]) SumToInt64

func (q *Query[T]) SumToInt64(selector func(T) int64) int64

SumToInt64 ...

func (*Query[T]) Take

func (q *Query[T]) Take(cnt uint) *Query[T]

Take 包含输入序列开头的指定数量的元素

func (*Query[T]) TakeWhile

func (q *Query[T]) TakeWhile(predicate delegate.FuncTBool[T]) *Query[T]

TakeWhile 只要指定的条件为 true,就会返回序列的元素

func (*Query[T]) TakeWhileN

func (q *Query[T]) TakeWhileN(predicate delegate.FuncTIntBool[T]) *Query[T]

TakeWhileN 只要指定的条件为 true,就会返回序列的元素。将在谓词函数的逻辑中使用元素的索引

func (*Query[T]) ToChan

func (q *Query[T]) ToChan(cr *ChanResult[T])

ToChan ...

func (*Query[T]) ToSet

func (q *Query[T]) ToSet(sliceCap ...int) []T

ToSet 返回去重后的序列

func (*Query[T]) ToSlice

func (q *Query[T]) ToSlice(sliceCap ...int) []T

ToSlice 返回切片

func (*Query[T]) Union

func (q *Query[T]) Union(other []T) *Query[T]

Union 生成两个序列的并集

func (*Query[T]) UnionBy

func (q *Query[T]) UnionBy(other []T, getter GetHashCoder[T]) *Query[T]

UnionBy 生成两个序列的并集

func (*Query[T]) UnionQ

func (q *Query[T]) UnionQ(otherQ *Query[T]) *Query[T]

UnionQ 生成两个序列的并集

func (*Query[T]) Where

func (q *Query[T]) Where(predicate delegate.FuncTBool[T]) *Query[T]

Where 基于谓词筛选值序列

func (*Query[T]) WhereN

func (q *Query[T]) WhereN(predicate delegate.FuncTIntBool[T]) *Query[T]

WhereN 基于谓词筛选值序列。 将在谓词函数的逻辑中使用每个元素的索引

Directories

Path Synopsis
pkg
delegate
Package delegate callback function
Package delegate callback function
set
Package set ...
Package set ...
slice
Package slice ...
Package slice ...

Jump to

Keyboard shortcuts

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