stream

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 26, 2021 License: Apache-2.0 Imports: 4 Imported by: 4

README

stream

Build status Go Report Card

stream是一个支持多种聚合操作的数据序列,支持转化处理的数据有:

类型 说明
slice go切片
list go list.List

支持的方法如下:

方法 说明
Count 获得元素的总数
FindFirst 获得第一个元素
FindLast 获得最后一个元素
FindAny 获得一个随机元素
Filter 过滤元素,返回一个包括所有符合过滤条件的元素的流
Limit 返回一个不超过给定长度的流
Skip 返回一个扔掉了前n个元素的流
Distinct 返回一个去重的stream
Sort 返回一个排序后的stream
FlatMap 映射并扁平化为一个stream
Map 由一个类型映射到另一个类型
Foreach 迭代流中所有数据
Peek 获取中间计算的值,主要用于调试
AnyMatch 任意匹配一个则返回true,否则返回false
AllMatch 完全匹配返回true,否则返回false
Reduce 对stream中元素进行聚合求值
Collect 获得slice

惰性求值

stream的实现使用惰性求值(Lazy Evaluation)的计算方式以获取更高的性能。

安装

go get github.com/xfali/stream

使用

slice例子:

testSlice := []string{"0,1,3,2,2,4", "5,8,7,6,7,9"}
stream.Slice(testSlice).FlatMap(func(s string) []int {
    return stream.Slice(strings.Split(s, ",")).Map(func(s string) int {
        i, _ := strconv.Atoi(s)
        return i
    }).Collect().([]int)
}).Filter(func(i int) bool {
    return i != 5
}).Sort(func(a, b int) int {
    return a - b
}).Distinct(func(a, b int) bool {
    return a == b
}).Map(func(i int) string {
    return strconv.Itoa(i)
}).Foreach(func(s string) {
    t.Log(s)
})

list例子:

testList := list.New()
testList.PushBack("0,1,3,2,2,4")
testList.PushBack("5,8,7,6,7,9")
stream.List(testList).FlatMap(func(s string) []int {
    return stream.Slice(strings.Split(s, ",")).Map(func(s string) int {
        i, _ := strconv.Atoi(s)
        return i
    }).Collect().([]int)
}).Filter(func(i int) bool {
    return i != 5
}).Sort(func(a, b int) int {
    return a - b
}).Distinct(func(a, b int) bool {
    return a == b
}).Map(func(i int) string {
    return strconv.Itoa(i)
}).Foreach(func(s string) {
    t.Log(s)
})

collector

stream可以通过collect支持更多的操作,内置的操作有:

collector 说明
collector.ToSlice 收集为slice
collector.ToList 收集为*list.List
collector.ToMap 收集为map[KEY]VALUE
collector.GroupBy 分组收集为map[KEY][]VALUE

用法举例:

s := stream.Slice([]{1, 2, 3, 4, 5}).Filter(func(i int) bool {
		return i != 3
	}).Collect(collector.ToList()).(*list.List)

e := s.Front()
for e != nil {
    if e.Value.(int) == 3 {
        t.Fatal("cannot be 3")
    } else {
        t.Log(e.Value)
    }
    e = e.Next()
}

更多用法参考test

未完成项

  • 并行处理

Documentation

Index

Constants

This section is empty.

Variables

View Source
var None = &Option{nil}

Functions

func HandleError

func HandleError(err *error)

Types

type Option

type Option struct {
	// contains filtered or unexported fields
}

func CanNil

func CanNil(i interface{}) *Option

func Some

func Some(i interface{}) *Option

func (*Option) And

func (o *Option) And(other *Option) *Option

And returns None if this option is None, otherwise returns the option received.

func (*Option) AndThen

func (o *Option) AndThen(function interface{}) *Option

AndThen returns None if this option is None, otherwise calls the received function with the wrapped value and returns the result Option.

func (*Option) Bind

func (o *Option) Bind(function interface{}) *Option

func (*Option) Else

func (o *Option) Else(other *Option) *Option

Else returns current option if this option is not None, otherwise returns the option received.

func (*Option) Filter

func (o *Option) Filter(fn interface{}) *Option

fn func(TYPE) bool

func (*Option) Get

func (o *Option) Get() interface{}

If the Option is None, it will panic.

func (*Option) IfPresent

func (o *Option) IfPresent(fn func(interface{}))

func (*Option) IsNil

func (o *Option) IsNil() bool

func (*Option) IsPresent

func (o *Option) IsPresent() bool

func (*Option) Map

func (o *Option) Map(mapFunc interface{}) *Option

fn func(OLD_TYPE) NEW_TYPE

type PipelineStream added in v0.0.2

type PipelineStream struct {
	// contains filtered or unexported fields
}

func List added in v0.0.2

func List(list *list2.List) *PipelineStream

func New added in v0.0.2

func Slice

func Slice(slice interface{}) *PipelineStream

func (*PipelineStream) AllMatch added in v0.0.2

func (s *PipelineStream) AllMatch(fn interface{}) bool

func (*PipelineStream) AnyMatch added in v0.0.2

func (s *PipelineStream) AnyMatch(fn interface{}) bool

func (*PipelineStream) Collect added in v0.0.2

func (s *PipelineStream) Collect(collector interface{}) interface{}

func (*PipelineStream) Count added in v0.0.2

func (s *PipelineStream) Count() int

func (*PipelineStream) Distinct added in v0.0.2

func (s *PipelineStream) Distinct(fn interface{}) Stream

func (*PipelineStream) Filter added in v0.0.2

func (s *PipelineStream) Filter(fn interface{}) Stream

func (*PipelineStream) FindAny added in v0.0.2

func (s *PipelineStream) FindAny() *Option

func (*PipelineStream) FindFirst added in v0.0.2

func (s *PipelineStream) FindFirst() *Option

func (*PipelineStream) FindLast added in v0.0.2

func (s *PipelineStream) FindLast() *Option

func (*PipelineStream) FlatMap added in v0.0.2

func (s *PipelineStream) FlatMap(fn interface{}) Stream

func (*PipelineStream) Foreach added in v0.0.2

func (s *PipelineStream) Foreach(eachFn interface{})

func (*PipelineStream) Limit added in v0.0.2

func (s *PipelineStream) Limit(size int) Stream

func (*PipelineStream) Map added in v0.0.2

func (s *PipelineStream) Map(fn interface{}) Stream

func (*PipelineStream) Peek added in v0.0.2

func (s *PipelineStream) Peek(eachFn interface{}) Stream

func (*PipelineStream) Reduce added in v0.0.2

func (s *PipelineStream) Reduce(fn, initValue interface{}) interface{}

func (*PipelineStream) Skip added in v0.0.2

func (s *PipelineStream) Skip(size int) Stream

func (*PipelineStream) Sort added in v0.0.2

func (s *PipelineStream) Sort(fn interface{}) Stream

type Stream

type Stream interface {
	// 获得元素的总数
	Count() int

	// 获得第一个元素
	FindFirst() *Option

	// 获得最后一个元素
	// Deprecated: 在使用pipeline方式时间复杂度为O(n)
	FindLast() *Option

	// 获得一个随机元素
	FindAny() *Option

	// 过滤元素,返回一个包括所有符合过滤条件的元素的stream
	// 参数类型为:fn func(TYPE) bool
	Filter(fn interface{}) Stream

	// 返回一个不超过给定长度的stream
	Limit(size int) Stream

	// 返回一个扔掉了前n个元素的stream
	Skip(size int) Stream

	// 返回一个去重的stream
	// 参数类型为:fn func(t1, t2 TYPE) bool
	Distinct(fn interface{}) Stream

	// 返回一个排序后的stream
	// 参数类型为:fn func(t1, t2 TYPE) int
	Sort(fn interface{}) Stream

	// 映射并扁平化为一个stream
	// 参数类型为:fn func(OLD_TYPE) []NEW_TYPE
	FlatMap(fn interface{}) Stream

	// 由一个类型映射到另一个类型
	// 参数类型为:fn func(OLD_TYPE) NEW_TYPE
	Map(fn interface{}) Stream

	// 迭代流中所有数据
	// 参数类型为:fn func(TYPE)
	Foreach(fn interface{})

	// 迭代流中所有数据,并返回stream
	// 参数类型为:fn func(TYPE)
	Peek(fn interface{}) Stream

	// 任意匹配一个则返回true,否则返回false
	// 参数类型为:fn func(TYPE) bool
	AnyMatch(fn interface{}) bool

	// 完全匹配返回true,否则返回false
	// 参数类型为:fn func(o TYPE) bool
	AllMatch(fn interface{}) bool

	// 对stream中元素进行聚合求值
	// 参数类型为:fn func(out, in TYPE) interface{}
	Reduce(fn, initValue interface{}) interface{}

	// 获得slice
	// 参数类型为:collector.Collector
	Collect(collector interface{}) interface{}
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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