flow

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2024 License: MIT Imports: 4 Imported by: 2

README

flow

Action Report Card codecov Lines of code godoc License

xuender/flow is a streaming programming library based on iterators for Go 1.23.

❗ Prerequirement

  • Go 1.23+

🚀 Install

go get github.com/xuender/flow@latest

💡 Usage

func main() {
	seq.Each(flow.Chain(
		seq.Range(100),
		flow.Filter(func(num int) bool { return num%3 == 0 }),
		flow.Skip[int](5),
		flow.Limit[int](4),
		flow.Reverse[int](),
	), func(num int) bool {
		fmt.Println(num)

		return true
	})
}

24
21
18
15
Flow Functions
  • Chain
  • Parallel
Intermediate Functions
  • Distinct
  • Filter
  • Limit
  • Peek
  • Reverse
  • Skip
  • Sort
  • SortFunc
Terminal Functions
  • seq.Count
  • seq.Each
  • seq.First
  • seq.Max
  • seq.Min
  • seq.Reduce
  • seq.Sum
  • seq.Join
  • seq.AnyMatch
  • seq.AllMatch
  • seq.NoneMatch
Seq Functions
  • seq.Chan
  • seq.Distinct
  • seq.Emit
  • seq.Range
  • seq.Filter
  • seq.FlatMap
  • seq.Limit
  • seq.Map
  • seq.Peek
  • seq.Reduce
  • seq.Reverse
  • seq.Skip
  • seq.Sort
  • seq.SortFunc
  • seq.ToChans

👤 Contributors

Contributors

📝 License

© ender, 2024~time.Now

MIT LICENSE

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

func Chain[E any](input iter.Seq[E], steps ...Step[E]) iter.Seq[E]

Chain applies a series of transformation steps to the input sequence sequentially.

This function takes an input sequence `input` and a variadic list of `steps`. Each step is applied sequentially to the input sequence.

Parameters:

input (iter.Seq[E]): The input sequence of elements.
steps (...Step[E]): A list of transformation steps to apply.

Returns:

iter.Seq[E]: The transformed sequence after applying all steps.
Example
package main

import (
	"fmt"

	"github.com/xuender/flow"
	"github.com/xuender/flow/seq"
)

func main() {
	fmt.Println(seq.Max(flow.Chain(
		seq.Range(100),
		flow.Limit[int](3),
		flow.Filter(func(num int) bool { return num%2 == 0 }),
	)))

}
Output:

2 true
Example (Map)
package main

import (
	"fmt"

	"github.com/xuender/flow"
	"github.com/xuender/flow/seq"
)

func main() {
	items := flow.Chain(
		seq.Range(100),
		flow.Limit[int](3),
		flow.Filter(func(num int) bool { return num%2 == 0 }),
	)

	fmt.Println(
		seq.Join(flow.Chain(
			seq.Map(items, func(num int) string { return fmt.Sprintf("num:%d", num) }),
			flow.Limit[string](2),
		), ","),
	)

}
Output:

num:0,num:2

func Parallel

func Parallel[E any](numWorkers int, input iter.Seq[E], steps ...Step[E]) iter.Seq[E]

Parallel processes the input sequence using multiple workers and applies a series of steps.

This function takes an input sequence `input` and a variadic list of `steps`. Each step is applied sequentially to the input sequence.

Parameters:

numWorkers (int): The number of worker goroutines to use for processing.
input (iter.Seq[E]): The input sequence of elements.
steps (...Step[E]): A list of transformation steps to apply.

Returns:

iter.Seq[E]: A new sequence containing the processed results.
Example
package main

import (
	"fmt"

	"github.com/xuender/flow"
	"github.com/xuender/flow/seq"
)

func main() {
	for num := range flow.Parallel(
		2,
		seq.Range(100),
		flow.Limit[int](3),
		flow.Filter(func(num int) bool { return num%3 == 0 }),
	) {
		fmt.Println(num)
	}

}
Output:

0
3

Types

type Step

type Step[E any] func(iter.Seq[E]) iter.Seq[E]

Step represents a transformation function that can be applied to a sequence.

This type defines a function that takes an input sequence of elements and returns a new sequence after applying some transformation.

Parameters:

E (any): The type of elements in the sequence.

Returns:

func(iter.Seq[E]) iter.Seq[E]: A transformation function.

func Distinct added in v0.0.3

func Distinct[E comparable]() Step[E]

Distinct returns a transformation step that filters out duplicate elements from a sequence.

This function returns a `Step` that can be used to create a new sequence containing only the distinct elements from the input sequence.

Parameters:

E (comparable): The type of elements in the sequence.

Returns:

Step[E]: A transformation step that filters duplicates.
Example
package main

import (
	"fmt"
	"slices"

	"github.com/xuender/flow"
	"github.com/xuender/flow/seq"
)

func main() {
	fmt.Println(seq.Sum(flow.Chain(
		slices.Values([]int{1, 2, 2, 3, 3}),
		flow.Distinct[int](),
	)))

}
Output:

6

func Filter

func Filter[E any](predicate func(E) bool) Step[E]

Filter returns a transformation step that filters elements based on a predicate.

This function returns a `Step` that can be used to create a new sequence containing only the elements that satisfy the given `predicate`.

Parameters:

predicate (func(E) bool): The predicate function to filter elements.

Returns:

Step[E]: A transformation step that filters elements.
Example
package main

import (
	"fmt"

	"github.com/xuender/flow"
	"github.com/xuender/flow/seq"
)

func main() {
	fmt.Println(seq.Sum(flow.Chain(
		seq.Range(10),
		flow.Filter(func(i int) bool {
			return i%3 == 0
		}),
	)))

}
Output:

18

func Limit

func Limit[E any](limit int) Step[E]

Limit returns a transformation step that limits the number of elements in a sequence.

This function returns a `Step` that creates a new sequence containing at most `limit` elements from the input sequence.

Parameters:

limit (int): The maximum number of elements to include.

Returns:

Step[E]: A transformation step that limits the sequence.
Example
package main

import (
	"fmt"

	"github.com/xuender/flow"
	"github.com/xuender/flow/seq"
)

func main() {
	fmt.Println(seq.Sum(flow.Chain(
		seq.Range(10),
		flow.Limit[int](4),
	)))

}
Output:

6

func Peek

func Peek[E any](action func(E)) Step[E]

Peek returns a transformation step that applies an action to each element in the sequence.

This function returns a `Step` that calls the `action` function on each element of the input sequence without modifying the sequence itself.

Parameters:

action (func(E)): The action to apply to each element.

Returns:

Step[E]: A transformation step that applies the action.
Example
package main

import (
	"fmt"

	"github.com/xuender/flow"
	"github.com/xuender/flow/seq"
)

func main() {
	seq.Emit(flow.Chain(
		seq.Range(3),
		flow.Peek(func(num int) {
			fmt.Println(num)
		}),
	))

}
Output:

0
1
2

func Reverse

func Reverse[E any]() Step[E]

Reverse returns a transformation step that reverses the order of elements in a sequence.

This function returns a `Step` that creates a new sequence with the elements in reverse order.

Returns:

Step[E]: A transformation step that reverses the sequence.
Example
package main

import (
	"fmt"

	"github.com/xuender/flow"
	"github.com/xuender/flow/seq"
)

func main() {
	fmt.Println(seq.First(flow.Chain(
		seq.Range(100),
		flow.Reverse[int](),
	)))

}
Output:

99 true

func Skip

func Skip[E any](num int) Step[E]

Skip returns a transformation step that skips the first `num` elements in a sequence.

This function returns a `Step` that creates a new sequence by skipping the first `num` elements of the input sequence.

Parameters:

num (int): The number of elements to skip.

Returns:

Step[E]: A transformation step that skips elements.
Example
package main

import (
	"fmt"

	"github.com/xuender/flow"
	"github.com/xuender/flow/seq"
)

func main() {
	fmt.Println(seq.First(flow.Chain(
		seq.Range(100),
		flow.Skip[int](99),
	)))

}
Output:

99 true

func Sort

func Sort[E cmp.Ordered]() Step[E]

Sort returns a transformation step that sorts the elements in a sequence.

This function returns a `Step` that creates a new sequence with the elements sorted.

Returns:

Step[E]: A transformation step that sorts the sequence.
Example
package main

import (
	"fmt"

	"github.com/xuender/flow"
	"github.com/xuender/flow/seq"
)

func main() {
	fmt.Println(seq.First(flow.Chain(
		seq.Range(100),
		flow.Reverse[int](),
		flow.Sort[int](),
	)))

}
Output:

0 true

func SortFunc

func SortFunc[E any](cmp func(item1, item2 E) int) Step[E]

SortFunc returns a transformation step that sorts the elements in a sequence using a custom comparison function.

This function returns a `Step` that creates a new sequence with the elements sorted according to the `cmp` function.

Parameters:

cmp (func(item1, item2 E) int): The comparison function for sorting.

Returns:

Step[E]: A transformation step that sorts the sequence.
Example
package main

import (
	"fmt"

	"github.com/xuender/flow"
	"github.com/xuender/flow/seq"
)

func main() {
	fmt.Println(seq.First(flow.Chain(
		seq.Range(100),
		flow.SortFunc(func(num1, num2 int) int {
			return num2 - num1
		}),
	)))

}
Output:

99 true

Directories

Path Synopsis
_examples
flow command
other command
parallel command
range1 command
range2 command
range3 command
range4 command

Jump to

Keyboard shortcuts

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