-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48263f5
commit 9f346d2
Showing
5 changed files
with
102 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |