flow

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2024 License: MIT Imports: 6 Imported by: 2

README

flow

Action Report Card codecov Lines of code godoc License

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

❗ Prerequirement

  • Go 1.23+
  • Not dependent on other libraries

🚀 Install

go get github.com/xuender/flow@latest

💡 Usage

Chain
package main

import (
  "fmt"

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

func main() {
  start := time.Now()

  for num :=range flow.Chain(
    seq.Range(100),
    flow.Filter(func(num int) bool { return num%3 == 0 }),
    flow.Skip[int](5),
    flow.Limit[int](4),
    flow.Peek(func(num int) {
      fmt.Println("peek", num)
      time.Sleep(time.Second)
    }),
    flow.Reverse[int](),
  ) {
    fmt.Println(num)
  }

  fmt.Printf("Chain: %v\n", time.Since(start))
}

Output:

peek 15
peek 18
peek 21
peek 24
24
21
18
15
Chain: 4s

[play]

Parallel
package main

import (
  "fmt"

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

func main() {
  start := time.Now()

  for num := range flow.Parallel(3,
    seq.Range(100),
    flow.Filter(func(num int) bool { return num%3 == 0 }),
    flow.Skip[int](5),
    flow.Limit[int](4),
    flow.Peek(func(num int) {
      fmt.Println("peek", num)
      time.Sleep(time.Second)
    }),
    flow.Reverse[int](),
  ) {
    fmt.Println(num)
  }

  fmt.Printf("Parallel: %v\n", time.Since(start))
}

Output:

peek 15
peek 18
peek 21
peek 24
24
21
18
15
Parallel: 2s

[play]

seq.Range
package main

import (
  "fmt"

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

func main() {
  fmt.Println("seq.Range(3)")

  for num := range seq.Range(3) {
    fmt.Println(num)
  }

  fmt.Println("\nseq.Range(-2, -6)")

  for num := range seq.Range(-2, -6) {
    fmt.Println(num)
  }

  fmt.Println("\nseq.Range(2, 10, 3)")

  for num := range seq.Range(2, 10, 3) {
    fmt.Println(num)
  }

  fmt.Println("\nseq.Range(3, 7, 0)")

  for num := range seq.Range(3, 7, 0) {
    fmt.Println(num)
  }
}

Output:

seq.Range(3)
0
1
2

seq.Range(-2, -6)
-2
-3
-4
-5

seq.Range(2, 10, 3)
2
5
8

seq.Range(3, 7, 0)
3
3
3
3

[play]

Flow Functions
iter.Seq[V] iter.Seq2[K,V]
Chain Chain2
Parallel Parallel2
Intermediate Functions
iter.Seq[V] iter.Seq2[K,V] Note
Append Append2
CenteredMovingAvg
Concat Concat2
Distinct Distinct2
DropWhile DropWhile2
Filter Filter2 Parallel
Limit Limit2
Map Map2 Parallel, Cannot change type
Merge Merge2
MovingAvg
Peek Peek2 Parallel
Prepend Prepend2
Repeat Repeat2
Reverse Reverse2 Collecting
Shuffle Shuffle2 Collecting
Skip Skip2
Sort Sort2 Collecting
SortFunc SortFunc2 Collecting
SortStableFunc SortStableFunc2 Collecting
TakeWhile TakeWhile2
Terminal Functions
iter.Seq[V] iter.Seq2[K,V]
seq.AllMatch seq.AllMatch2
seq.AnyMatch seq.AnyMatch2
seq.Clone seq.Clone2
seq.Chunk seq.Chunk2
seq.Count seq.Count2
seq.Each seq.Each2
seq.Emit seq.Emit2
seq.First seq.First2
seq.Last seq.Last2
seq.Join
seq.Max seq.Max2
seq.Min seq.Min2
seq.NoneMatch seq.NoneMatch2
seq.Reduce seq.Reduce2
seq.Sum
Other Seq Functions
iter.Seq[V] iter.Seq2[K,V] Note
seq.Append seq.Append2
seq.CenteredMovingAvg Boundary effect at end
seq.Chan seq.Chan2
seq.Concat seq.Concat2
seq.Collect seq.Collect2
seq.Distinct seq.Distinct2
seq.DropWhile seq.DropWhile2
seq.Filter seq.Filter2
seq.FlatMap
seq.Keys
seq.Limit seq.Limit2
seq.Map seq.Map2
seq.Merge seq.Merge2
seq.MovingAvg Boundary effect at start
seq.Partition seq.Partition2 Block
seq.Peek seq.Peek2
seq.Prepend seq.Prepend2
seq.Range seq.Range2
seq.Reduce seq.Reduce2
seq.Repeat seq.Repeat2
seq.Reverse seq.Reverse2
seq.Send seq.Send2
seq.Seq seq.Seq2
seq.Shuffle seq.Shuffle2
seq.Skip seq.Skip2
seq.Sorted seq.Sorted2
seq.SortedFunc seq.SortedFunc2
seq.SortedStableFunc seq.SortedStableFunc2
seq.TakeWhile seq.TakeWhile2
seq.ToChans seq.ToChans2
seq.Tuples
seq.Values
seq.Window seq.Window2
seq.Zip
Custom Step[V] / Step2[K,V] function

Refer to Append, Sored, Filter, etc.

ps: Attention Parallel.

👤 Contributors

Contributors

📝 License

© ender, 2024~time.Now

MIT LICENSE

Documentation

Overview

Package flow implements functionality similar to Java 8 Streams.

It uses Go 1.23's iterator and generic type support.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

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

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

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

Play: https://go.dev/play/p/JydmjWYw9rw

Example
package main

import (
	"fmt"

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

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

}
Output:

0
6
12
18
Example (MapChangeType)
package main

import (
	"fmt"

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

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

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

}
Output:

num:0
num:2

func Chain2 added in v0.0.5

func Chain2[K, V any](input iter.Seq2[K, V], steps ...Step2[K, V]) iter.Seq2[K, V]

Chain2 applies multiple Step2 functions sequentially to the input sequence.

It returns the final sequence after applying all steps.

Example
package main

import (
	"fmt"

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

func main() {
	for key, val := range flow.Chain2(
		seq.Range2(100),
		flow.Filter2(func(key, _ int) bool { return key%2 == 0 }),
		flow.Limit2[int, int](2),
	) {
		fmt.Println(key, val)
	}

}
Output:

0 0
2 2

func Parallel

func Parallel[V any](num int, input iter.Seq[V], steps ...Step[V]) iter.Seq[V]

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

It returns a new sequence with 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](8),
		flow.Filter(func(num int) bool { return num%3 == 0 }),
		flow.Sort[int](),
	) {
		if num > 3 {
			break
		}

		fmt.Println(num)
	}

}
Output:

0
3

func Parallel2 added in v0.0.5

func Parallel2[K, V any](num int, input iter.Seq2[K, V], steps ...Step2[K, V]) iter.Seq2[K, V]

Parallel2 processes the input sequence in parallel using multiple workers.

It returns a new sequence with the processed results.

Example
package main

import (
	"fmt"

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

func main() {
	for key, val := range flow.Parallel2(2,
		seq.Range2(100),
		flow.Limit2[int, int](8),
		flow.Filter2(func(key, _ int) bool { return key%3 == 0 }),
		flow.Sort2[int, int](),
	) {
		if key > 3 {
			break
		}

		fmt.Println(key, val)
	}

}
Output:

0 0
3 3

Types

type Step

type Step[V any] struct {
	Next     func(iter.Seq[V]) iter.Seq[V]
	Parallel bool
}

Step represents a processing step in an iterator.

It includes a Next function to perform the next operation on a sequence, and a Parallel boolean indicating whether it can be executed in parallel.

func Append added in v0.0.5

func Append[V any](items ...V) Step[V]

Append adds multiple elements to the end of a sequence.

It takes a variable number of elements and appends them to the input sequence.

Example
package main

import (
	"fmt"

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

func main() {
	for num := range flow.Chain(
		seq.Range(2),
		flow.Append(9),
	) {
		fmt.Println(num)
	}

}
Output:

0
1
9

func CenteredMovingAvg added in v0.0.10

func CenteredMovingAvg[V stats.Number](size int) Step[V]

CenteredMovingAvg creates a step that calculates the centered moving average of a sequence.

It computes the average of `size` consecutive elements, centered when possible.

Example
package main

import (
	"fmt"

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

func main() {
	for num := range flow.Chain(
		seq.Range(5),
		flow.CenteredMovingAvg[int](3),
	) {
		fmt.Println(num)
	}

}
Output:

1
2
3
3
3

func Concat added in v0.0.14

func Concat[V any](seqs ...iter.Seq[V]) Step[V]

Concat combines multiple sequence iterators into a single sequence iterator.

It returns a Step[V] that concatenates the provided sequences with an input sequence.

Example
package main

import (
	"fmt"

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

func main() {
	for num := range flow.Chain(
		seq.Range(3),
		flow.Concat(seq.Seq(7, 8)),
	) {
		fmt.Println(num)
	}

}
Output:

0
1
2
7
8

func Distinct added in v0.0.3

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

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

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

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 DropWhile added in v0.0.8

func DropWhile[V any](predicate func(V) bool) Step[V]

It returned Step[V] can be used in a pipeline to process sequences.

Example
package main

import (
	"fmt"

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

func main() {
	for num := range flow.Chain(
		seq.Range(4),
		flow.DropWhile(func(num int) bool { return num < 2 }),
	) {
		fmt.Println(num)
	}

}
Output:

2
3

func Filter

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

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

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

Play: https://go.dev/play/p/JydmjWYw9rw

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[V any](limit int) Step[V]

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

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

Play: https://go.dev/play/p/JydmjWYw9rw

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 Map added in v0.0.9

func Map[V any](maper func(V) V) Step[V]

Map creates a new step that applies a transformation function to each element of an input sequence.

It returns a Step that can be used in a pipeline to map over the input sequence.

Note:

Cannot change type.
Example
package main

import (
	"fmt"

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

func main() {
	for num := range flow.Chain(
		seq.Range(3),
		flow.Map(func(num int) int { return num * 2 }),
	) {
		fmt.Println(num)
	}

}
Output:

0
2
4

func Merge added in v0.0.5

func Merge[V any](seqs ...iter.Seq[V]) Step[V]

Merge combines multiple sequences into a single sequence.

It merges the input sequence with additional sequences.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(seq.Count(flow.Chain(
		seq.Range(3),
		flow.Merge(seq.Range(4), seq.Range(2)),
	)))

	fmt.Println(seq.Count(flow.Chain(
		seq.Range(3),
		flow.Merge[int](),
	)))

}
Output:

9
3

func MovingAvg added in v0.0.10

func MovingAvg[V stats.Number](size int) Step[V]

MovingAvg creates a step that calculates the moving average of a sequence.

It computes the average of every `size` consecutive elements in the input.

Example
package main

import (
	"fmt"

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

func main() {
	for num := range flow.Chain(
		seq.Range(5),
		flow.MovingAvg[int](3),
	) {
		fmt.Println(num)
	}

}
Output:

0
0
1
2
3

func Peek

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

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

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

Play: https://go.dev/play/p/0YiWlMdzwMk

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 Prepend added in v0.0.8

func Prepend[V any](items ...V) Step[V]

Prepend creates a step that adds items to the beginning of a sequence.

It function is useful for creating a prepending step in a pipeline.

Example
package main

import (
	"fmt"

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

func main() {
	for num := range flow.Chain(
		seq.Range(2),
		flow.Prepend(9),
	) {
		fmt.Println(num)
	}

}
Output:

9
0
1

func Repeat added in v0.0.6

func Repeat[V any](count int) Step[V]

Repeat creates a new step that repeats the input sequence a specified number of times.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(seq.Count(flow.Chain(
		seq.Range(3),
		flow.Repeat[int](4),
	)))

}
Output:

12

func Reverse

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

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

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

Play: https://go.dev/play/p/JydmjWYw9rw

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 Shuffle added in v0.0.9

func Shuffle[V any]() Step[V]

Shuffle creates a step that shuffles the input sequence.

It returns a Step that, when applied, yields elements in a random order.

Example
package main

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

func main() {
	seq.Emit(flow.Chain(
		seq.Range(3),
		flow.Shuffle[int](),
	))

}
Output:

func Skip

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

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

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

Play: https://go.dev/play/p/JydmjWYw9rw

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[V cmp.Ordered]() Step[V]

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

It returns a `Step` function 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[V any](cmp func(V, V) int) Step[V]

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

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

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

func SortStableFunc added in v0.0.5

func SortStableFunc[V any](cmp func(V, V) int) Step[V]

SortStableFunc sorts the elements in the sequence using a custom comparison function with stable sorting.

It returns a `Step` function 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.SortStableFunc(func(num1, num2 int) int {
			return num2 - num1
		}),
	)))

}
Output:

99 true

func TakeWhile added in v0.0.8

func TakeWhile[V any](predicate func(V) bool) Step[V]

TakeWhile creates a Step[V] that filters elements from 'input' using 'predicate'.

It yields elements until the predicate is false for the first time.

The returned Step[V] can be used in a pipeline to process sequences.

Example
package main

import (
	"fmt"

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

func main() {
	for num := range flow.Chain(
		seq.Range(4),
		flow.TakeWhile(func(num int) bool { return num < 2 }),
	) {
		fmt.Println(num)
	}

}
Output:

0
1

type Step2 added in v0.0.5

type Step2[K, V any] struct {
	Next     func(iter.Seq2[K, V]) iter.Seq2[K, V]
	Parallel bool
}

Step2 represents a processing step in an iterator for sequences with two types K and V.

It includes a Next function to perform the next operation on a sequence, and a Parallel boolean indicating whether it can be executed in parallel.

func Append2 added in v0.0.5

func Append2[K, V any](items ...seq.Tuple[K, V]) Step2[K, V]

Append2 appends additional (key, value) tuples to the input sequence.

It returns a new sequence with the appended tuples.

Example
package main

import (
	"fmt"

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

func main() {
	for key, val := range flow.Chain2(
		seq.Range2(2),
		flow.Append2(seq.T(7, 8)),
	) {
		fmt.Println(key, val)
	}

}
Output:

0 0
1 1
7 8

func Concat2 added in v0.0.14

func Concat2[K, V any](seqs ...iter.Seq2[K, V]) Step2[K, V]

Concat2 combines multiple key-value sequence iterators into a single step.

It returns a Step2[K, V] that concatenates the provided sequences with an input sequence.

Example
package main

import (
	"fmt"

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

func main() {
	for key, val := range flow.Chain2(
		seq.Range2(3),
		flow.Concat2(seq.Seq2(seq.T(7, 70), seq.T(8, 80))),
	) {
		fmt.Println(key, val)
	}

}
Output:

0 0
1 1
2 2
7 70
8 80

func Distinct2 added in v0.0.5

func Distinct2[K comparable, V any]() Step2[K, V]

Distinct2 removes duplicate (key, value) pairs from the sequence.

It returns a `Step2` function that filters duplicates.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(seq.Count2(flow.Chain2(
		seq.Range2(3),
		flow.Append2(seq.T(2, 7), seq.T(8, 4)),
		flow.Distinct2[int, int](),
	)))

}
Output:

4

func DropWhile2 added in v0.0.8

func DropWhile2[K, V any](predicate func(K, V) bool) Step2[K, V]

DropWhile2 creates a Step2[K, V] that skips key-value pairs from 'input' as long as 'predicate' returns true.

Once the predicate is false, it starts yielding pairs.

It returned Step2[K, V] can be used in a pipeline to process sequences of key-value pairs.

Example
package main

import (
	"fmt"

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

func main() {
	for key, val := range flow.Chain2(
		seq.Range2(4),
		flow.DropWhile2(func(key, _ int) bool { return key < 2 }),
	) {
		fmt.Println(key, val)
	}

}
Output:

2 2
3 3

func Filter2 added in v0.0.5

func Filter2[K, V any](predicate func(K, V) bool) Step2[K, V]

Filter2 filters (key, value) pairs based on a predicate function.

It returns a new sequence with filtered pairs.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(seq.Count2(flow.Chain2(
		seq.Range2(10),
		flow.Filter2(func(key, _ int) bool {
			return key%3 == 0
		}),
	)))

}
Output:

4

func Limit2 added in v0.0.5

func Limit2[K, V any](limit int) Step2[K, V]

Limit2 limits the number of (key, value) pairs in the sequence.

It returns a new sequence with a maximum number of pairs.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(seq.Count2(flow.Chain2(
		seq.Range2(10),
		flow.Limit2[int, int](4),
	)))

}
Output:

4

func Map2 added in v0.0.9

func Map2[K, V any](maper func(K, V) (K, V)) Step2[K, V]

Map2 creates a new step that applies a transformation function to each pair of elements in an input sequence.

It returns a Step2 that can be used in a pipeline to map over pairs in the input sequence.

Note:

Cannot change type.
Example
package main

import (
	"fmt"

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

func main() {
	for key, val := range flow.Chain2(
		seq.Range2(3),
		flow.Map2(func(key, val int) (int, int) { return key * 2, val * 3 }),
	) {
		fmt.Println(key, val)
	}

}
Output:

0 0
2 3
4 6

func Merge2 added in v0.0.5

func Merge2[K, V any](seqs ...iter.Seq2[K, V]) Step2[K, V]

Merge2 merges multiple sequences of (key, value) pairs.

It returns a new sequence containing all pairs.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(seq.Count2(flow.Chain2(
		seq.Range2(3),
		flow.Merge2(seq.Range2(4), seq.Range2(2)),
	)))

	fmt.Println(seq.Count2(flow.Chain2(
		seq.Range2(3),
		flow.Merge2[int, int](),
	)))

}
Output:

9
3

func Peek2 added in v0.0.5

func Peek2[K, V any](action func(K, V)) Step2[K, V]

Peek2 applies an action to each (key, value) pair in the sequence.

It returns a new sequence with the same pairs.

Example
package main

import (
	"fmt"

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

func main() {
	seq.Emit2(flow.Chain2(
		seq.Range2(3),
		flow.Peek2(func(key, val int) {
			fmt.Println(key, val)
		}),
	))

}
Output:

0 0
1 1
2 2

func Prepend2 added in v0.0.8

func Prepend2[K, V any](items ...seq.Tuple[K, V]) Step2[K, V]

Prepend2 creates a step that adds tuples to the beginning of a sequence.

It function is useful for creating a prepending step in a pipeline for tuples.

Example
package main

import (
	"fmt"

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

func main() {
	for key, val := range flow.Chain2(
		seq.Range2(2),
		flow.Prepend2(seq.T(7, 8)),
	) {
		fmt.Println(key, val)
	}

}
Output:

7 8
0 0
1 1

func Repeat2 added in v0.0.6

func Repeat2[K, V any](count int) Step2[K, V]

Repeat2 creates a new step that repeats each element of the input sequence a specified number of times.

Tt returns a `Step2` that repeats each element of the input sequence `count` times.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(seq.Count2(flow.Chain2(
		seq.Range2(3),
		flow.Repeat2[int, int](4),
	)))

}
Output:

12

func Reverse2 added in v0.0.5

func Reverse2[K, V any]() Step2[K, V]

Reverse2 reverses the order of (key, value) pairs in the sequence.

It returns a `Step2` function that reverses the sequence.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(seq.First2(flow.Chain2(
		seq.Range2(100),
		flow.Reverse2[int, int](),
	)))

}
Output:

99 99 true

func Shuffle2 added in v0.0.9

func Shuffle2[K, V any]() Step2[K, V]

Shuffle2 creates a step that shuffles pairs in the input sequence.

It returns a Step2 that, when applied, yields pairs in a random order.

Example
package main

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

func main() {
	seq.Emit2(flow.Chain2(
		seq.Range2(3),
		flow.Shuffle2[int, int](),
	))

}
Output:

func Skip2 added in v0.0.5

func Skip2[K, V any](num int) Step2[K, V]

Skip2 skips the first 'num' (key, value) pairs in the sequence.

It returns a `Skip2` with the remaining pairs.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(seq.First2(flow.Chain2(
		seq.Range2(100),
		flow.Skip2[int, int](99),
	)))

}
Output:

99 99 true

func Sort2 added in v0.0.5

func Sort2[K cmp.Ordered, V any]() Step2[K, V]

Sort2 sorts the (key, value) pairs in the sequence.

It returns a `Step2` function that sorts the sequence.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(seq.First2(flow.Chain2(
		seq.Range2(100),
		flow.Reverse2[int, int](),
		flow.Sort2[int, int](),
	)))

}
Output:

0 0 true

func SortFunc2 added in v0.0.5

func SortFunc2[K, V any](cmp func(seq.Tuple[K, V], seq.Tuple[K, V]) int) Step2[K, V]

SortFunc2 sorts (key, value) pairs using a custom comparison function.

It returns a `Step2` function that sorts the sequence.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(seq.First2(flow.Chain2(
		seq.Range2(100),
		flow.SortFunc2(func(num1, num2 seq.Tuple[int, int]) int {
			return num2.K - num1.K
		}),
	)))

}
Output:

99 99 true

func SortStableFunc2 added in v0.0.5

func SortStableFunc2[K, V any](cmp func(seq.Tuple[K, V], seq.Tuple[K, V]) int) Step2[K, V]

SortStableFunc2 sorts (key, value) pairs using a custom comparison function with stable sorting.

It returns a `Step2` function that sorts the sequence.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(seq.First2(flow.Chain2(
		seq.Range2(100),
		flow.SortStableFunc2(func(num1, num2 seq.Tuple[int, int]) int {
			return num2.K - num1.K
		}),
	)))

}
Output:

99 99 true

func TakeWhile2 added in v0.0.8

func TakeWhile2[K, V any](predicate func(K, V) bool) Step2[K, V]

TakeWhile2 creates a Step2[K, V] that filters key-value pairs from 'input' using 'predicate'.

It yields pairs until the predicate is false for the first time.

The returned Step2[K, V] can be used in a pipeline to process sequences of key-value pairs.

Example
package main

import (
	"fmt"

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

func main() {
	for key, val := range flow.Chain2(
		seq.Range2(4),
		flow.TakeWhile2(func(key, _ int) bool { return key < 2 }),
	) {
		fmt.Println(key, val)
	}

}
Output:

0 0
1 1

Directories

Path Synopsis
_examples
map
Package seq provides functions for handling the iter.Seq interface in Go 1.23.
Package seq provides functions for handling the iter.Seq interface in Go 1.23.
Package stats contains statistical functions such as Sum.
Package stats contains statistical functions such as Sum.

Jump to

Keyboard shortcuts

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