flow

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2024 License: MIT Imports: 1 Imported by: 0

README

flow: Helper Library for FP-Style Processing

Since Go 1.23, we have iter.Seq as the standard representation for a iterator / generator.

Although there also be a x/exp/xiter library for common operation, but it's seems lacking some critical building wheels. So here is this library.

See PkgSite for more information.

Documentation

Overview

Package flow is a helper library around "iter.Seq" types.

The library is intend to provide the lacking wheels from standard and/or "x/exp/xiter" library.

For example, "Empty" and "Pack" is provided to build a sequence of zero and one item, "Any" and "All" boolean short-circuit is also provided. But "Map", "Filter" and "Reduce" is not provided since that is planned to be in "x/exp/xiter". Also transformation from/to slice/map is in the standard library "slices" and "maps".

All function Xxx comes with a Xxx2 version to address the usage between "iter.Seq" and "iter.Seq2", if reasonable.

Function with immediate transformation, e.g. key, is not provided, since that users can already achieve with another "Map" operation.

Wish someday we can use tuple as primitive generic type, so we don't have to write these Xxx2 stuffs.

Example
type Node struct {
	val   int
	left  *Node
	right *Node
}

var traverse func(node *Node) iter.Seq[int]
traverse = func(node *Node) iter.Seq[int] {
	// Empty is useful as base case during recursive generator chaining.
	if node == nil {
		return Empty[int]()
	}
	// Pack is useful to promote a single value into a iterable for chaining.
	return Chain(
		traverse(node.left),
		Pack(node.val),
		traverse(node.right),
	)
}

root := &Node{
	val: 3,
	left: &Node{
		val:  2,
		left: &Node{val: 1},
	},
	right: &Node{
		val:  5,
		left: &Node{val: 4},
	},
}

var results []int
for val := range traverse(root) {
	results = append(results, val)
	if val == 4 {
		break
	}
}
fmt.Printf("%v\n", results)
Output:

[1 2 3 4]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(conditions iter.Seq[bool]) bool

Any return c1 && c2 && ... && cn, return true if input is empty.

func Any

func Any(conditions iter.Seq[bool]) bool

Any return c1 || c2 || ... || cn, return false if input is empty.

func Backward

func Backward(begin int, end int) iter.Seq[int]

Backward yields [start, end) in reversed order, yields no values if start >= end.

func Chain

func Chain[V any](sequences ...iter.Seq[V]) iter.Seq[V]

Chain chains multiple sequences together.

Only for demonstration, consider "x/exp/xiter.Concat" once the official one is available.

func Chain2

func Chain2[K, V any](sequences ...iter.Seq2[K, V]) iter.Seq2[K, V]

func Drop

func Drop[V any](sequence iter.Seq[V], count int) iter.Seq[V]

Drop drops first N items from the sequence, yield no item if exhausted.

func Drop2

func Drop2[K, V any](sequence iter.Seq2[K, V], count int) iter.Seq2[K, V]

func Empty

func Empty[V any]() iter.Seq[V]

Empty yield no value.

func Empty2

func Empty2[K, V any]() iter.Seq2[K, V]

func Forward

func Forward(begin int, end int) iter.Seq[int]

Forward yields [start, end) in normal order, yields no values if start >= end.

func Pack

func Pack[V any](v V) iter.Seq[V]

Pack yield one value.

func Pack2

func Pack2[K, V any](k K, v V) iter.Seq2[K, V]

func Since

func Since(start int) iter.Seq[int]

Since yields infinite sequence of numbers from "start".

func Take

func Take[V any](sequence iter.Seq[V], count int) iter.Seq[V]

Take takes first N items from the sequence, yield all items if exhausted.

func Take2

func Take2[K, V any](sequence iter.Seq2[K, V], count int) iter.Seq2[K, V]

Types

This section is empty.

Jump to

Keyboard shortcuts

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