Skip to content

Commit 6f07b8b

Browse files
authored
Merge pull request #877 from devlights/add-go123-iter-examples
2 parents 41357fb + 43f642c commit 6f07b8b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

examples/basic/iters/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
1818
m["iters_range_over_func_1"] = Go123RangeOverFunc1
1919
m["iters_range_over_func_2"] = Go123RangeOverFunc2
2020
m["iters_range_over_func_3"] = Go123RangeOverFunc3
21+
m["iters_seq"] = IterSeq
2122
}

examples/basic/iters/seq.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package iters
2+
3+
import (
4+
"iter"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// IterSeq は、Go 1.23 で正式導入となった iter.Seq のサンプルです。
10+
//
11+
// Range-Over-Func は、独自のイテレータを作成出来るようになる機能です。
12+
// 以下の関数パターンがサポートされています。
13+
//
14+
// - func(func() bool) : ループ変数に値を渡さないタイプ
15+
// - func(func(v) bool) : 1つのループ変数に値を渡すタイプ
16+
// - func(func(k, v) bool) : 2つのループ変数に値を渡すタイプ
17+
//
18+
// Go 1.23 にて追加された iter パッケージには以下の2つの型が定義されています。
19+
//
20+
// - iter.Seq[V any]
21+
// - iter.Seq2[K, V any]
22+
//
23+
// 上はそれぞれ func(v) bool と func(k, v) bool に対応しており、カスタムイテレータを
24+
// 戻り値や引数として指定したりする際に、イテレータであると利用者側に伝わりやすくなります。
25+
//
26+
// # REFERENCES
27+
// - https://tip.golang.org/doc/go1.23
28+
// - https://tip.golang.org/blog/range-functions
29+
// - https://tip.golang.org/ref/spec#For_range
30+
// - https://pkg.go.dev/iter@go1.23.3
31+
// - https://zenn.dev/koya_iwamura/articles/7e7482c7222e37
32+
// - https://tech.every.tv/entry/2023/12/09/1
33+
// - https://future-architect.github.io/articles/20240129a/
34+
func IterSeq() error {
35+
var (
36+
// 指定された値からのカウントダウンを行うイテレータ。
37+
countdown = func(v int) iter.Seq[int] {
38+
return func(yield func(v int) bool) {
39+
for {
40+
if v < 0 || !yield(v) {
41+
return
42+
}
43+
44+
v--
45+
}
46+
}
47+
}
48+
)
49+
50+
for i := range countdown(5) {
51+
output.Stdoutf("[countdown(5)]", "%d\n", i)
52+
}
53+
54+
return nil
55+
}

0 commit comments

Comments
 (0)