Documentation
¶
Index ¶
- func Chain[E any](input iter.Seq[E], steps ...Step[E]) iter.Seq[E]
- func Parallel[E any](numWorkers int, input iter.Seq[E], steps ...Step[E]) iter.Seq[E]
- type Step
- func Distinct[E comparable]() Step[E]
- func Filter[E any](predicate func(E) bool) Step[E]
- func Limit[E any](limit int) Step[E]
- func Peek[E any](action func(E)) Step[E]
- func Reverse[E any]() Step[E]
- func Skip[E any](num int) Step[E]
- func Sort[E cmp.Ordered]() Step[E]
- func SortFunc[E any](cmp func(item1, item2 E) int) Step[E]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chain ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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