Skip to content

Commit

Permalink
Introduce new seq package (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokiat authored Nov 5, 2024
1 parent 40f3b60 commit 2a612a2
Show file tree
Hide file tree
Showing 13 changed files with 475 additions and 43 deletions.
10 changes: 2 additions & 8 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gog_test

import (
"cmp"
"fmt"
"slices"
"strconv"
Expand Down Expand Up @@ -187,14 +188,7 @@ func ExampleEntries() {
}
target := gog.Entries(source)
slices.SortFunc(target, func(first, second gog.KV[string, []int]) int {
switch {
case first.Key < second.Key:
return -1
case first.Key > second.Key:
return 1
default:
return 0
}
return cmp.Compare(first.Key, second.Key)
})
fmt.Printf("%+v\n", target)

Expand Down
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
module github.com/mokiat/gog

go 1.22
go 1.23

require (
github.com/onsi/ginkgo/v2 v2.17.1
github.com/onsi/gomega v1.32.0
github.com/onsi/ginkgo/v2 v2.21.0
github.com/onsi/gomega v1.35.1
)

require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20240402174815-29b9bb013b0f // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.19.0 // indirect
github.com/google/pprof v0.0.0-20241101162523-b92577c0c142 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/tools v0.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
47 changes: 22 additions & 25 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20240402174815-29b9bb013b0f h1:f00RU+zOX+B3rLAmMMkzHUF2h1z4DeYR9tTCvEq2REY=
github.com/google/pprof v0.0.0-20240402174815-29b9bb013b0f/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8=
github.com/onsi/ginkgo/v2 v2.17.1/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
github.com/onsi/gomega v1.32.0 h1:JRYU78fJ1LPxlckP6Txi/EYqJvjtMrDC04/MM5XRHPk=
github.com/onsi/gomega v1.32.0/go.mod h1:a4x4gW6Pz2yK1MAmvluYme5lvYTn61afQ2ETw/8n4Lg=
github.com/google/pprof v0.0.0-20241101162523-b92577c0c142 h1:sAGdeJj0bnMgUNVeUpp6AYlVdCt3/GdI3pGRqsNSQLs=
github.com/google/pprof v0.0.0-20241101162523-b92577c0c142/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/onsi/ginkgo/v2 v2.21.0 h1:7rg/4f3rB88pb5obDgNZrNHrQ4e6WpjonchcpuBRnZM=
github.com/onsi/ginkgo/v2 v2.21.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
github.com/onsi/gomega v1.35.1 h1:Cwbd75ZBPxFSuZ6T+rN/WCb/gOc6YgFBXLlZLhC7Ds4=
github.com/onsi/gomega v1.35.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14 changes: 14 additions & 0 deletions seq/collect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package seq

import (
"iter"
"slices"
)

// CollectCap collects values from src into a new slice with the given capacity
// preallocated and returns it.
func CollectCap[T any](src iter.Seq[T], cap int) []T {
result := make([]T, 0, cap)
result = slices.AppendSeq(result, src)
return result
}
25 changes: 25 additions & 0 deletions seq/collect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seq_test

import (
"slices"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/mokiat/gog/seq"
)

var _ = Describe("Collect", func() {

Describe("CollectCap", func() {

It("collects values from sequence into new slice with given capacity", func() {
iter := slices.Values([]int{1, 2, 3})
result := seq.CollectCap(iter, 5)
Expect(result).To(Equal([]int{1, 2, 3}))
Expect(result).To(HaveCap(5))
})

})

})
2 changes: 2 additions & 0 deletions seq/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package seq provides helpful iterator functions.
package seq
83 changes: 83 additions & 0 deletions seq/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package seq_test

import (
"fmt"

"github.com/mokiat/gog/seq"
)

func ExampleTimes() {
for v := range seq.Times(3) {
fmt.Println(v)
}

// Output:
// 0
// 1
// 2
}

func ExampleRange() {
for v := range seq.Range(1, 3) {
fmt.Println(v)
}

// Output:
// 1
// 2
// 3
}

func ExampleMap() {
source := seq.Times(4)
target := seq.Map(source, func(v int) string {
return fmt.Sprintf("item %d", v)
})
for v := range target {
fmt.Println(v)
}

// Output:
// item 0
// item 1
// item 2
// item 3
}

func ExampleCollectCap() {
source := seq.Times(4)
target := seq.CollectCap(source, 12)
for _, v := range target {
fmt.Println(v)
}
fmt.Println()
fmt.Println(cap(target))

// Output:
// 0
// 1
// 2
// 3
//
// 12
}

func ExampleBatchSlice() {
source := []string{
"1Hello", "1World",
"2This", "2Is", "2Longer",
"3Yes",
}
eqFn := func(a, b string) bool {
return a[0] == b[0]
}

for batch := range seq.BatchSlice(source, eqFn, 0) {
fmt.Printf("%#v\n", batch)
}

// Output:
// []string{"1Hello", "1World"}
// []string{"2This", "2Is", "2Longer"}
// []string{"3Yes"}
}
40 changes: 40 additions & 0 deletions seq/range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package seq

import "iter"

// None returns an empty sequence.
func None[T any]() iter.Seq[T] {
return func(yield func(T) bool) {}
}

// Times returns a sequence of integers from 0 to count.
func Times(count int) iter.Seq[int] {
return func(yield func(int) bool) {
for i := range count {
if !yield(i) {
return
}
}
}
}

// Range returns a sequence of integers from from (inclusive) to to (inclusive).
//
// If from is greater than to, the sequence will be in descending order.
func Range(from, to int) iter.Seq[int] {
return func(yield func(int) bool) {
if from < to {
for i := from; i <= to; i++ {
if !yield(i) {
return
}
}
} else {
for i := from; i >= to; i-- {
if !yield(i) {
return
}
}
}
}
}
44 changes: 44 additions & 0 deletions seq/range_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package seq_test

import (
"slices"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/mokiat/gog/seq"
)

var _ = Describe("Range", func() {

Describe("None", func() {
It("yields no values", func() {
iter := seq.None[int]()
result := slices.Collect(iter)
Expect(result).To(BeEmpty())
})
})

Describe("Times", func() {
It("yields the correct sequence of numbers", func() {
iter := seq.Times(3)
result := slices.Collect(iter)
Expect(result).To(Equal([]int{0, 1, 2}))
})
})

Describe("Range", func() {
It("yields the correct sequence of numbers", func() {
iter := seq.Range(1, 3)
result := slices.Collect(iter)
Expect(result).To(Equal([]int{1, 2, 3}))
})

It("works in reverse as well", func() {
iter := seq.Range(3, 1)
result := slices.Collect(iter)
Expect(result).To(Equal([]int{3, 2, 1}))
})
})

})
13 changes: 13 additions & 0 deletions seq/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package seq_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestSeq(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Sequence Suite")
}
Loading

0 comments on commit 2a612a2

Please sign in to comment.