Skip to content

Commit

Permalink
Support Go 1.22 and 1.23
Browse files Browse the repository at this point in the history
  • Loading branch information
BooleanCat committed Jun 28, 2024
1 parent 48263f5 commit 9f346d2
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 33 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
env: { GOEXPERIMENT: rangefunc }
- uses: codecov/codecov-action@v4
env: { CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}" }
test:
test-123:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
Expand All @@ -31,3 +31,16 @@ jobs:
go-version: 1.23.0-rc.1
cache: false
- run: make test
test-122:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.22"
cache: false
- run: make test
env: { GOEXPERIMENT: rangefunc }
23 changes: 8 additions & 15 deletions future/maps/maps.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
//go:build go1.23

// Package maps provides early implementations of map-related functions
// available in Go 1.23+.
package maps

import "iter"
import (
"iter"
"maps"
)

// All returns an iterator over key-value pairs from m. The iteration order is
// not specified and is not guaranteed to be the same from one call to the
// next.
func All[Map ~map[K]V, K comparable, V any](m Map) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for key, value := range m {
if !yield(key, value) {
return
}
}
}
return maps.All(m)
}

// Collect collects key-value pairs from seq into a new map and returns it.
func Collect[K comparable, V any](seq iter.Seq2[K, V]) map[K]V {
m := make(map[K]V)

for key, value := range seq {
m[key] = value
}

return m
return maps.Collect(seq)
}
37 changes: 37 additions & 0 deletions future/maps/maps_1.22.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//go:build go1.22 && rangefunc

// Package maps provides early implementations of map-related functions
// available in Go 1.23+.
//
// If using Go 1.22, the rangefunc experimental feature must be enabled.
//
// If using Go 1.23 or later, prefer the functions from the standard library.
//
// This package will be removed in Go 1.24.
package maps

import "iter"

// All returns an iterator over key-value pairs from m. The iteration order is
// not specified and is not guaranteed to be the same from one call to the
// next.
func All[Map ~map[K]V, K comparable, V any](m Map) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for key, value := range m {
if !yield(key, value) {
return
}
}
}
}

// Collect collects key-value pairs from seq into a new map and returns it.
func Collect[K comparable, V any](seq iter.Seq2[K, V]) map[K]V {
m := make(map[K]V)

for key, value := range seq {
m[key] = value
}

return m
}
25 changes: 8 additions & 17 deletions future/slices/slices.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
// Package slices provides early implementations of slice-related functions
// available in Go 1.23+.
//go:build go1.23

package slices

import "iter"
import (
"iter"
"slices"
)

// Values returns an iterator over the slice elements, starting with s[0].
func Values[Slice ~[]E, E any](slice Slice) iter.Seq[E] {
return func(yield func(E) bool) {
for _, value := range slice {
if !yield(value) {
return
}
}
}
return slices.Values(slice)
}

// Collect collects values from seq into a new slice and returns it.
func Collect[E any](seq iter.Seq[E]) []E {
slice := make([]E, 0)

for item := range seq {
slice = append(slice, item)
}

return slice
return slices.Collect(seq)
}
35 changes: 35 additions & 0 deletions future/slices/slices_1.22.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//go:build go1.22 && rangefunc

// Package slices provides early implementations of slice-related functions
// available in Go 1.23+.
//
// If using Go 1.22, the rangefunc experimental feature must be enabled.
//
// If using Go 1.23 or later, prefer the functions from the standard library.
//
// This package will be removed in Go 1.24.
package slices

import "iter"

// Values returns an iterator over the slice elements, starting with s[0].
func Values[Slice ~[]E, E any](slice Slice) iter.Seq[E] {
return func(yield func(E) bool) {
for _, value := range slice {
if !yield(value) {
return
}
}
}
}

// Collect collects values from seq into a new slice and returns it.
func Collect[E any](seq iter.Seq[E]) []E {
slice := make([]E, 0)

for item := range seq {
slice = append(slice, item)
}

return slice
}

0 comments on commit 9f346d2

Please sign in to comment.