pipe

package module
Version: v0.0.0-...-a134fe9 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2020 License: MIT Imports: 4 Imported by: 1

README

A golang library that makes operations on slice easilier

What can I do?

  • slice process
    • Map
    • Filter
    • Sort
    • Reverse
  • map process
    • Keys
    • Values
  • output (starting with "P" means parallel version)
    • ToSlice / PToSlice
    • ToMap / PToMap / ToMap2 / PToMap2
    • ToGroupMap / PToGroupMap / ToGroupMap2 / PToGroupMap2
    • Reduce / PReduce
    • Each / PEach
    • Some
    • Every

Installation

go get github.com/lennon-guan/pipe

Example

	src := []int{1, 2, 3}
	dst := pipe.NewPipe(src).
			Map(func(item int) string{ return fmt.Sprintf("#%d", item) }).
			ToSlice().([]string)
	// dst is []string{"#1", "#2", "#3"}
	src := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	dst := pipe.NewPipe(src).
			Filter(func(in int) bool { return in % 3 == 0 }).
			ToSlice().([]int)
	// dst is []int{3, 6, 9}
	src := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	sum := pipe.NewPipe(src).
			Reduce(0, func(s, item int) int{ return s + item }).(int)
	// sum == 55
	src := []int{3, 1, 4, 1, 5, 9}
	dst := pipe.NewPipe(src).
		Sort(func(a, b int) bool { return a < b }).
		ToSlice().([]int)
	// dst is []int{1, 1, 3, 4, 5, 9}
	src := []int{1, 2, 3}
	dst := pipe.NewPipe(src).
		Reverse().
		ToSlice().([]int)
	// dst is []int{3, 2, 1}
	src := []int{5, 4, 3, 2, 1}
	dst := pipe.NewPipe(src).
		ToMap(
			func(v int) string { return fmt.Sprintf("Key-%d", v) },
			func(v int) string { return fmt.Sprintf("Val-%d", v) },
		).(map[string]string)
	// dst is map[Key-1:Val-1 Key-5:Val-5 Key-4:Val-4 Key-3:Val-3 Key-2:Val-2]
	src := []int{5, 4, 3, 2, 1}
	dst := pipe.NewPipe(src).
		ToMap2(func(v int) (string, string) {
		return fmt.Sprintf("Key-%d", v), fmt.Sprintf("Val-%d", v)
	}).(map[string]string)
	// dst is map[Key-1:Val-1 Key-5:Val-5 Key-4:Val-4 Key-3:Val-3 Key-2:Val-2]
	src := []int{5, 4, 3, 2, 1}
	dst := NewPipe(src).
		ToGroupMap(
		func(v int) string {
			if v%2 != 0 {
				return "odd"
			} else {
				return "even"
			}
		},
		func(v int) int { return v },
	).(map[string][]int)
	// dst is map[odd:[5 3 1] even:[4 2]]
	src := []int{5, 4, 3, 2, 1}
	dst := pipe.NewPipe(src).
		ToGroupMap2(
		func(v int) (string, int) {
			if v%2 != 0 {
				return "odd", v
			} else {
				return "even", v
			}
		},
	).(map[string][]int)
	// dst is map[odd:[5 3 1] even:[4 2]]
	src := []int{1, 2, 3, 4, 5}
	dst := make([]int, 5)
	pipe.NewPipe(src).
		Map(func(i int) int { return i * i }).
		Each(func(item, index int) { dst[index] = item })
	if !intSliceEqual(dst, 1, 4, 9, 16, 25) {
		t.Error("values wrong")
	}
	src := []int{1, 2, 3, 4, 5}
	dst := make([]int, 5)
	pipe.NewPipe(src).
		Map(func(i int) int { return i * i }).
		PEach(func(item, index int) { dst[index] = item })
	if !intSliceEqual(dst, 1, 4, 9, 16, 25) {
		t.Error("values wrong")
	}

You can invoke map/filter function many times

	src := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	dst := pipe.NewPipe(src).
			Filter(func(in int) bool { return in % 3 == 0 }).
			Map(func(in int) int { return in * in }).
			ToSlice().([]int)
	// dst is []int{9, 36, 81}

More examples are avaliable in pipe_test.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMapPipe

func NewMapPipe(m interface{}) *_MapPipe

func NewPipe

func NewPipe(arr interface{}) *_Pipe

func Range

func Range(args ...int) *_Pipe

func Range1

func Range1(size int) *_Pipe

func Range2

func Range2(begin, end int) *_Pipe

func Range3

func Range3(begin, end, step int) *_Pipe

Types

type WaitIndex

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

func NewWaitIndex

func NewWaitIndex(total int) *WaitIndex

func (*WaitIndex) Done

func (wait *WaitIndex) Done(index int)

func (*WaitIndex) Wait

func (wait *WaitIndex) Wait(index int)

func (*WaitIndex) WaitAndClose

func (wait *WaitIndex) WaitAndClose()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL