diff --git a/examples.txt b/examples.txt index 909ed62..6f44bd5 100644 --- a/examples.txt +++ b/examples.txt @@ -22,7 +22,7 @@ Interfaces Enums Struct Embedding Generics -Range over Custom Types +Range over Iterators Errors Custom Errors Goroutines diff --git a/examples/range-over-custom-types/range-over-custom-types.hash b/examples/range-over-custom-types/range-over-custom-types.hash deleted file mode 100644 index 76c7492..0000000 --- a/examples/range-over-custom-types/range-over-custom-types.hash +++ /dev/null @@ -1,2 +0,0 @@ -3576a8e614e8e47a0541e800e017d4f9aa2504a3 -q2H-p_moUTy diff --git a/examples/range-over-custom-types/range-over-custom-types.go b/examples/range-over-iterators/range-over-iterators.go similarity index 73% rename from examples/range-over-custom-types/range-over-custom-types.go rename to examples/range-over-iterators/range-over-iterators.go index d578bc3..ea9041d 100644 --- a/examples/range-over-custom-types/range-over-custom-types.go +++ b/examples/range-over-iterators/range-over-iterators.go @@ -1,6 +1,6 @@ // Starting with version 1.23, Go has added support for // [iterators](https://go.dev/blog/range-functions), -// which lets us range over custom types. +// which lets us range over pretty much anything! package main @@ -51,6 +51,23 @@ func (lst *List[T]) All() iter.Seq[T] { } } +// Iteration doesn't require an underlying data structure, +// and doesn't even have to be finite! Here's a function +// returning an iterator over Fibonacci numbers: it keeps +// running as long as `yield` keeps returning `true`. +func genFib() iter.Seq[int] { + return func(yield func(int) bool) { + a, b := 1, 1 + + for { + if !yield(a) { + return + } + a, b = b, a+b + } + } +} + func main() { lst := List[int]{} lst.Push(10) @@ -69,4 +86,14 @@ func main() { // all its values into a slice. all := slices.Collect(lst.All()) fmt.Println("all:", all) + + for n := range genFib() { + + // Once the loop hits `break` or an early return, the `yield` function + // passed to the iterator will return `false`. + if n >= 10 { + break + } + fmt.Println(n) + } } diff --git a/examples/range-over-iterators/range-over-iterators.hash b/examples/range-over-iterators/range-over-iterators.hash new file mode 100644 index 0000000..ee781b4 --- /dev/null +++ b/examples/range-over-iterators/range-over-iterators.hash @@ -0,0 +1,2 @@ +375f830fbe82633900d572c9077302143463a2e3 +BayyagaCK83 diff --git a/examples/range-over-custom-types/range-over-custom-types.sh b/examples/range-over-iterators/range-over-iterators.sh similarity index 68% rename from examples/range-over-custom-types/range-over-custom-types.sh rename to examples/range-over-iterators/range-over-iterators.sh index cb58fdc..7fa2b59 100644 --- a/examples/range-over-custom-types/range-over-custom-types.sh +++ b/examples/range-over-iterators/range-over-iterators.sh @@ -2,4 +2,10 @@ 13 23 all: [10 13 23] +1 +1 +2 +3 +5 +8 diff --git a/public/errors b/public/errors index e1e0098..5830d24 100644 --- a/public/errors +++ b/public/errors @@ -12,7 +12,7 @@ } if (e.key == "ArrowLeft") { - window.location.href = 'range-over-custom-types'; + window.location.href = 'range-over-iterators'; } diff --git a/public/generics b/public/generics index 5758c6c..a0d8bb2 100644 --- a/public/generics +++ b/public/generics @@ -17,7 +17,7 @@ if (e.key == "ArrowRight") { - window.location.href = 'range-over-custom-types'; + window.location.href = 'range-over-iterators'; } } @@ -225,7 +225,7 @@ automatically.
- Next example: Range over Custom Types. + Next example: Range over Iterators.
diff --git a/public/index.html b/public/index.html index 40e33b1..21de726 100644 --- a/public/index.html +++ b/public/index.html @@ -81,7 +81,7 @@
Starting with version 1.23, Go has added support for iterators, -which lets us range over custom types. +which lets us range over pretty much anything! |
@@ -46,7 +46,7 @@ which lets us range over custom types. |
- ![]() ![]() ![]() ![]()
|
@@ -147,6 +147,39 @@ return value for a potential early termination.
+ |
+ Iteration doesn’t require an underlying data structure,
+and doesn’t even have to be finite! Here’s a function
+returning an iterator over Fibonacci numbers: it keeps
+running as long as |
+
+
+
+ |
+ ||
+ + | +
+
+
+ |
+ ||
@@ -163,8 +196,8 @@ return value for a potential early termination. | |||
- Since Since |
@@ -183,10 +216,36 @@ For example, Collect takes any iterator and collects
all its values into a slice.
|
- + |
+ |
+
+ + | +
+
+
+ |
+ ||
+ Once the loop hits |
+
+
+
|