From 964661e335a9291d92f223f39975b7ec279dea69 Mon Sep 17 00:00:00 2001 From: legendtkl Date: Tue, 6 Sep 2016 15:45:18 +0800 Subject: [PATCH 1/3] Add concurrency patterns:generators --- README.md | 2 +- concurrency/generator.md | 6 ++++++ concurrency/generators.go | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 concurrency/generator.md create mode 100644 concurrency/generators.go diff --git a/README.md b/README.md index dfec8f7..c5abfaa 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ __Concurrency Patterns__: | [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: [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 | | [Parallelism](concurrency/parallelism.md) | Completes large number of independent tasks | | TODO: [Producer Consumer](concurrency/producer_consumer.md) | Separates tasks from task executions | diff --git a/concurrency/generator.md b/concurrency/generator.md new file mode 100644 index 0000000..3871e0a --- /dev/null +++ b/concurrency/generator.md @@ -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). \ No newline at end of file diff --git a/concurrency/generators.go b/concurrency/generators.go new file mode 100644 index 0000000..504f89f --- /dev/null +++ b/concurrency/generators.go @@ -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) + } +} From 4ec77fe1193a00c53ec82406c83f45261fb4c31b Mon Sep 17 00:00:00 2001 From: legendtkl Date: Wed, 7 Sep 2016 00:20:30 +0800 Subject: [PATCH 2/3] fix typo and function comments --- concurrency/generator.md | 4 ++-- concurrency/generators.go | 25 ++++++++++++------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/concurrency/generator.md b/concurrency/generator.md index 3871e0a..853e4f7 100644 --- a/concurrency/generator.md +++ b/concurrency/generator.md @@ -1,6 +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. +[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 Implemente generator in two ways: channel and closure. Fibnacci example can be found in [generators.go](generators.go). \ No newline at end of file +With Go language, we can implement generator in two ways: channel and closure. Fibnacci example can be found in [generators.go](generators.go). \ No newline at end of file diff --git a/concurrency/generators.go b/concurrency/generators.go index 504f89f..603dfcd 100644 --- a/concurrency/generators.go +++ b/concurrency/generators.go @@ -4,41 +4,40 @@ import ( "fmt" ) -//implement generator by closure -func FibnacciClosure() func() (ret int) { +//FibonacciClosure implements fibonacci number generatoin using closure +func FibonacciClosure() func() int { a, b := 0, 1 - return func() (ret int) { - ret = b + return func() int { a, b = b, a+b - return + return a } } -//implement generator by channel -func FibnacciChan(n int) chan int { - ret := make(chan int) +//FibonacciChan implements fibonacci number generatoin using channel +func FibonacciChan(n int) chan int { + c := make(chan int) go func() { a, b := 0, 1 for i := 0; i < n; i++ { - ret <- b + c <- b a, b = b, a+b } - close(ret) + close(c) }() - return ret + return c } func main() { //closure - nextFib := FibnacciClosure() + nextFib := FibonacciClosure() for i := 0; i < 20; i++ { fmt.Println(nextFib()) } //channel - for i := range FibnacciChan(20) { + for i := range FibonacciChan(20) { fmt.Println(i) } } From 8e6c437754ac3f829e9102ef0033d627a8f31a78 Mon Sep 17 00:00:00 2001 From: legendtkl Date: Thu, 8 Sep 2016 08:16:42 +0800 Subject: [PATCH 3/3] fix typo --- concurrency/generator.md | 2 +- concurrency/generators.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/concurrency/generator.md b/concurrency/generator.md index 853e4f7..2161248 100644 --- a/concurrency/generator.md +++ b/concurrency/generator.md @@ -3,4 +3,4 @@ [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. Fibnacci example can be found in [generators.go](generators.go). \ No newline at end of file +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). \ No newline at end of file diff --git a/concurrency/generators.go b/concurrency/generators.go index 603dfcd..5b2b8b2 100644 --- a/concurrency/generators.go +++ b/concurrency/generators.go @@ -4,7 +4,7 @@ import ( "fmt" ) -//FibonacciClosure implements fibonacci number generatoin using closure +//FibonacciClosure implements fibonacci number generation using closure func FibonacciClosure() func() int { a, b := 0, 1 return func() int { @@ -13,7 +13,7 @@ func FibonacciClosure() func() int { } } -//FibonacciChan implements fibonacci number generatoin using channel +//FibonacciChan implements fibonacci number generation using channel func FibonacciChan(n int) chan int { c := make(chan int)