Help us shape the future of Go! Take the 2026 Go Developer Survey

gostream

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2019 License: Apache-2.0 Imports: 4 Imported by: 0

README

gostream

go Map Reduce Sorted Filter Limit stream , a simple library

进度

  • 支持并发流 已实现
  • 支持流之间的拼接。 已实现

GoDoc

Documentation here: https://godoc.org/github.com/felix0080/gostream

Documentation

Overview

Package gostream implements a stream slice.

Usage

调用BuildStream 方法,传入实现了sort 接口的slice,若不需要排序,则无需实现sort

//定义一个slice.填充元素
a:=[]interface{}{4,3,2,1}
//使用实现了sort的type 包装,并调用调用BuildStream
s:=BuildStream(IntSlice(a))
//定义一个slice.填充元素
a1:=[]interface{}{6,5,8,7}
//使用实现了sort的type 包装,并调用调用BuildStream
s1:=BuildStream(IntSlice(a1))
//将两个流合并为1个,并且以s为主流
value:=s.Combine(s1).
//排序,对流的排序,若没有实现sort接口,将不会进行排序
	Sorted().
//只保留前6个元素
	Limit(6).
//map 将函数作用到每一个流元素上
	Map(func(i interface{}) interface{} {
		return i.(int)+1
	}).
//Filter 过滤函数返回值为true的元素,形成新流
	Filter(func(i interface{}) bool {
		return i.(int)==7
	}).
//Reduce 将前两个元素计算后的结果和第三个元素计算,以此类推
Reduce(func(i interface{}, i2 interface{}) interface{} {
		return i.(int)+i2.(int)
	})//==20
//s.Limit(10)
fmt.Println("after arr: ", s.Collect())
fmt.Println("after value: ", value)

欢迎提Issues

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stream

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

定义一个流,内部为 实现了Sort 接口的 Slice(interfalce{}). 若没有实现Sort接口,sort方法将不可使用 使用方法类似于java8 stream 欢迎贡献,大家一起完善此库

func BuildStream

func BuildStream(array interface{}) *Stream

Example:

type IntSlice []interface{}

func (s IntSlice) Len() int { return len(s) }
func (s IntSlice) Swap(i, j int){ s[i], s[j] = s[j], s[i] }
func (s IntSlice) Less(i, j int) bool {
	return s[i].(int) < s[j].(int)
}
a:=[]interface{}{4,3,2,1}
s:=BuildStream(IntSlice(a))

func (*Stream) Collect

func (stream *Stream) Collect() interface{}

收集流,将流的内部数组返回。

func (*Stream) Combine

func (stream *Stream) Combine(anotherStream *Stream) *Stream

组合流,将两个stream 简单组合在一起

func (*Stream) Filter

func (stream *Stream) Filter(f func(interface{}) bool) *Stream

设置条件满足的被过滤

func (*Stream) Limit

func (stream *Stream) Limit(len int) *Stream

用户获取指定数量的流

func (*Stream) Map

func (stream *Stream) Map(f func(interface{}) interface{}) *Stream

用于映射每个元素到对应的结果

func (*Stream) MultipartMap

func (stream *Stream) MultipartMap(worknum int, f func(interface{}) interface{}) *Stream

用于映射每个元素到对应的结果

func (*Stream) Reduce

func (stream *Stream) Reduce(f func(interface{}, interface{}) interface{}) interface{}

当数组小于2时,返回Nil

func (*Stream) Sorted

func (stream *Stream) Sorted() *Stream

用户对流进行排序 需要自行实现sort接口才可使用此方法

Jump to

Keyboard shortcuts

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