mirror of
https://github.com/tmrts/go-patterns.git
synced 2025-02-22 15:23:12 +03:00
Add concurrency patterns:generators
This commit is contained in:
parent
befd460eeb
commit
964661e335
@ -67,7 +67,7 @@ __Concurrency Patterns__:
|
|||||||
| [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 |
|
||||||
| TODO: [Broadcast](concurrency/broadcast.md) | Transfers a message to all recipients simultaneously |
|
| TODO: [Broadcast](concurrency/broadcast.md) | Transfers a message to all recipients simultaneously |
|
||||||
| TODO: [Coroutines](concurrency/coroutine.md) | Subroutines that allow suspending and resuming execution at certain locations |
|
| TODO: [Coroutines](concurrency/coroutine.md) | Subroutines that allow suspending and resuming execution at certain locations |
|
||||||
| TODO: [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 |
|
||||||
| TODO: [Reactor](concurrency/reactor.md) | Demultiplexes service requests delivered concurrently to a service handler and dispatches them syncronously to the associated request handlers |
|
| TODO: [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 |
|
||||||
| TODO: [Producer Consumer](concurrency/producer_consumer.md) | Separates tasks from task executions |
|
| TODO: [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 iteratoin behavior of a loop.
|
||||||
|
|
||||||
|
# Implementation and Example
|
||||||
|
With Go language, we can Implemente generator in two ways: channel and closure. Fibnacci example can be found in [generators.go](generators.go).
|
44
concurrency/generators.go
Normal file
44
concurrency/generators.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
//implement generator by closure
|
||||||
|
func FibnacciClosure() func() (ret int) {
|
||||||
|
a, b := 0, 1
|
||||||
|
return func() (ret int) {
|
||||||
|
ret = b
|
||||||
|
a, b = b, a+b
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//implement generator by channel
|
||||||
|
func FibnacciChan(n int) chan int {
|
||||||
|
ret := make(chan int)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
a, b := 0, 1
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
ret <- b
|
||||||
|
a, b = b, a+b
|
||||||
|
}
|
||||||
|
close(ret)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
//closure
|
||||||
|
nextFib := FibnacciClosure()
|
||||||
|
for i := 0; i < 20; i++ {
|
||||||
|
fmt.Println(nextFib())
|
||||||
|
}
|
||||||
|
|
||||||
|
//channel
|
||||||
|
for i := range FibnacciChan(20) {
|
||||||
|
fmt.Println(i)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user