Skip to content

Commit

Permalink
init support Go 1.23 Iterators (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringsaturn authored Aug 25, 2024
1 parent d5c2ca3 commit 56c2927
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
28 changes: 28 additions & 0 deletions iter.go
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
}
}
}
}
}
35 changes: 35 additions & 0 deletions iter_test.go
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
}

0 comments on commit 56c2927

Please sign in to comment.