-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init support Go 1.23 Iterators (#104)
- Loading branch information
1 parent
d5c2ca3
commit 56c2927
Showing
2 changed files
with
63 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//go:build go1.23 | ||
// +build go1.23 | ||
|
||
package gocitiesjson | ||
|
||
import "iter" | ||
|
||
func All() iter.Seq[*City] { | ||
return func(yield func(*City) bool) { | ||
for _, city := range Cities { | ||
if !yield(city) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func Filter(filter func(*City) bool) iter.Seq[*City] { | ||
return func(yield func(*City) bool) { | ||
for _, city := range Cities { | ||
if filter(city) { | ||
if !yield(city) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} |
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.23 | ||
// +build go1.23 | ||
|
||
package gocitiesjson_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
gocitiesjson "github.com/ringsaturn/go-cities.json" | ||
) | ||
|
||
func ExampleAll() { | ||
c := 0 | ||
for city := range gocitiesjson.All() { | ||
_ = city | ||
c++ | ||
} | ||
fmt.Println(c > 10000) | ||
// Output: true | ||
} | ||
|
||
func ExampleFilter() { | ||
c := 0 | ||
|
||
bboxFilter := func(city *gocitiesjson.City) bool { | ||
return city.Lng > 0 && city.Lng < 10 && city.Lat > 0 && city.Lat < 10 | ||
} | ||
for city := range gocitiesjson.Filter(bboxFilter) { | ||
_ = city | ||
c++ | ||
} | ||
|
||
fmt.Println(c > 0) | ||
// Output: true | ||
} |