Skip to content
/ flow Public

A streaming programming library based on iterators for Go 1.23

License

Notifications You must be signed in to change notification settings

xuender/flow

Repository files navigation

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