Skip to content

Commit

Permalink
Merge pull request #1334 from future-ryunosuketanai/articles/20240718…
Browse files Browse the repository at this point in the history
…a-fix-indent

Articles/20240718a fix code block contents
  • Loading branch information
ma91n authored Jul 19, 2024
2 parents 68eada1 + 3289e2d commit 37688c9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ for rows.Next() {

使い方とかはすでに詳しくわかりやすく解説してくれているブログ記事があったりするのでそちらを見てもらえればよいかな、と思います。

* [every Tech Blog: Go 1.22で追加予定のrange over intと、GOEXPERIMENT入り予定のrange over funcを触ってみる](https://qiita.com/kidach1/items/afa4c6c29a6eb6be487a)
* [every Tech Blog: Go 1.22で追加予定のrange over intと、GOEXPERIMENT入り予定のrange over funcを触ってみる](https://tech.every.tv/entry/2023/12/09/1)

これらがリリースされると、そのうち、次のように書けるようになるかと思います。まだExperimentalで変更もありえるので詳しくは説明しません。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,17 @@ Go1.22 で追加された `range over integer` を利用して書き換えてみ

```diff
func f(yield func() bool) {
- if !yield() {
- return
- }
- if !yield() {
- return
- }
+ for range 5 {
+ if !yield() {
+ return
+ }
+ }
- if !yield() {
- return
- }
- if !yield() {
- return
- }
+ for range 5 {
+ if !yield() {
+ return
+ }
+ }
}

// Output:
Expand Down Expand Up @@ -286,13 +286,13 @@ func main() {
}

func f(yield func(k, v string) bool) {
maps := map[string]string{
langs := map[string]string{
"go": "golang",
"py": "python",
"rb": "ruby",
"js": "javascript",
}
for k, v := range maps {
for k, v := range langs {
if !yield(k, v) {
return
}
Expand Down Expand Up @@ -371,20 +371,20 @@ import (
)

func main() {
maps := map[string]string{
langs := map[string]string{
"go": "golang",
"py": "python",
"rb": "ruby",
"js": "javascript",
}
for k, v := range showMaps(maps) {
for k, v := range showLangs(langs) {
fmt.Printf("k is: %s, v is: %s\n", k, v)
}
}

func showMaps(maps map[string]string) iter.Seq2[string, string] {
func showLangs(langs map[string]string) iter.Seq2[string, string] {
return func(yield func(string, string) bool) {
for k, v := range maps {
for k, v := range langs {
if k == "go" || k == "py" {
if !yield(k, v) {
return
Expand Down Expand Up @@ -421,7 +421,7 @@ func showMaps(maps map[string]string) iter.Seq2[string, string] {
* 複数のゴルーチンから同時に next(), stop() を呼び出すとエラーになる
* next()(または、stop())の呼び出し中にイテレータでパニックが発生した場合、next()(または、stop())自体も同じ値でパニックが発生する

`Pull2``next()` を利用して、`showMaps` を書き換えてみます。
`Pull2``next()` を利用して、`showLangs` を書き換えてみます。

```go
package main
Expand All @@ -432,14 +432,14 @@ import (
)

func main() {
maps := map[string]string{
langs := map[string]string{
"go": "golang",
"py": "python",
"rb": "ruby",
"js": "javascript",
}

next, _ := iter.Pull2(showMaps(maps))
next, _ := iter.Pull2(showLangs(langs))
for {
k, v, ok := next()
if !ok {
Expand All @@ -449,9 +449,9 @@ func main() {
}
}

func showMaps(maps map[string]string) iter.Seq2[string, string] {
func showLangs(langs map[string]string) iter.Seq2[string, string] {
return func(yield func(string, string) bool) {
for k, v := range maps {
for k, v := range langs {
if !yield(k, v) {
return
}
Expand All @@ -477,15 +477,15 @@ import (
)

func main() {
maps := map[string]string{
langs := map[string]string{
"go": "golang",
"py": "python",
"rb": "ruby",
"js": "javascript",
}

- next, _ := iter.Pull2(showMaps(maps))
+ next, stop := iter.Pull2(showMaps(maps))
- next, _ := iter.Pull2(showLangs(langs))
+ next, stop := iter.Pull2(showLangs(langs))
for {
k, v, ok := next()
if !ok {
Expand All @@ -498,9 +498,9 @@ func main() {
}
}

func showMaps(maps map[string]string) iter.Seq2[string, string] {
func showLangs(langs map[string]string) iter.Seq2[string, string] {
return func(yield func(string, string) bool) {
for k, v := range maps {
for k, v := range langs {
if !yield(k, v) {
return
}
Expand Down

0 comments on commit 37688c9

Please sign in to comment.