mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-29 08:26:04 +03:00
concurrency/generator: implement generator pattern
This commit is contained in:
parent
90f5b5b1a5
commit
d05638adac
@ -67,7 +67,7 @@ A curated collection of idiomatic design & application patterns for Go language.
|
|||||||
| [Bounded Parallelism](/concurrency/bounded_parallelism.md) | Completes large number of independent tasks with resource limits | ✔ |
|
| [Bounded Parallelism](/concurrency/bounded_parallelism.md) | Completes large number of independent tasks with resource limits | ✔ |
|
||||||
| [Broadcast](/concurrency/broadcast.md) | Transfers a message to all recipients simultaneously | ✘ |
|
| [Broadcast](/concurrency/broadcast.md) | Transfers a message to all recipients simultaneously | ✘ |
|
||||||
| [Coroutines](/concurrency/coroutine.md) | Subroutines that allow suspending and resuming execution at certain locations | ✘ |
|
| [Coroutines](/concurrency/coroutine.md) | Subroutines that allow suspending and resuming execution at certain locations | ✘ |
|
||||||
| [Generators](/concurrency/generator.md) | Yields a sequence of values one at a time | ✘ |
|
| [Generators](/concurrency/generator.md) | Yields a sequence of values one at a time | ✔ |
|
||||||
| [Reactor](/concurrency/reactor.md) | Demultiplexes service requests delivered concurrently to a service handler and dispatches them syncronously to the associated request handlers | ✘ |
|
| [Reactor](/concurrency/reactor.md) | Demultiplexes service requests delivered concurrently to a service handler and dispatches them syncronously to the associated request handlers | ✘ |
|
||||||
| [Parallelism](/concurrency/parallelism.md) | Completes large number of independent tasks | ✔ |
|
| [Parallelism](/concurrency/parallelism.md) | Completes large number of independent tasks | ✔ |
|
||||||
| [Producer Consumer](/concurrency/producer_consumer.md) | Separates tasks from task executions | ✘ |
|
| [Producer Consumer](/concurrency/producer_consumer.md) | Separates tasks from task executions | ✘ |
|
||||||
|
6
concurrency/generator.md
Normal file
6
concurrency/generator.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# Generator Pattern
|
||||||
|
|
||||||
|
[Generator](https://en.wikipedia.org/wiki/Generator_(computer_programming)) is a special routine that can be used to control the iteration behavior of a loop.
|
||||||
|
|
||||||
|
# Implementation and Example
|
||||||
|
With Go language, we can implement generator in two ways: channel and closure. Fibonacci number generation example can be found in [generators.go](generators.go).
|
43
concurrency/generators.go
Normal file
43
concurrency/generators.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
//FibonacciClosure implements fibonacci number generation using closure
|
||||||
|
func FibonacciClosure() func() int {
|
||||||
|
a, b := 0, 1
|
||||||
|
return func() int {
|
||||||
|
a, b = b, a+b
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//FibonacciChan implements fibonacci number generation using channel
|
||||||
|
func FibonacciChan(n int) chan int {
|
||||||
|
c := make(chan int)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
a, b := 0, 1
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
c <- b
|
||||||
|
a, b = b, a+b
|
||||||
|
}
|
||||||
|
close(c)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
//closure
|
||||||
|
nextFib := FibonacciClosure()
|
||||||
|
for i := 0; i < 20; i++ {
|
||||||
|
fmt.Println(nextFib())
|
||||||
|
}
|
||||||
|
|
||||||
|
//channel
|
||||||
|
for i := range FibonacciChan(20) {
|
||||||
|
fmt.Println(i)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user