-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
123 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# golang-fibonacci-generator | ||
# golang-fibonacci-generators | ||
|
||
Different generator for Fibonacci Numbers written in Golang | ||
Different generators for Fibonacci Numbers written in Golang. For more information read my corresponding blog entry [Generators in Go](https://blog.haardiek.org/generators-in-go). |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,72 @@ | ||
package main | ||
// package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
// import ( | ||
// "fmt" | ||
// ) | ||
|
||
type fibonacci struct { | ||
Iterator chan *int | ||
} | ||
// type fibonacci struct { | ||
// Iterator chan *int | ||
// } | ||
|
||
func (f *fibonacci) Next() *int { | ||
return <-f.Iterator | ||
} | ||
// func (f *fibonacci) Next() *int { | ||
// return <-f.Iterator | ||
// } | ||
|
||
func newFibonacci(limit int) *fibonacci { | ||
f := &fibonacci{ | ||
make(chan *int, 1), | ||
} | ||
// func newFibonacci(limit int) *fibonacci { | ||
// f := &fibonacci{ | ||
// make(chan *int, 1), | ||
// } | ||
// go func() { | ||
// var current, next int | ||
// for { | ||
// if limit == 0 { | ||
// close(f.Iterator) | ||
// return | ||
// } | ||
// if current != 0 || next != 0 { | ||
// next, current = current+next, next | ||
// } else { | ||
// next = 1 | ||
// } | ||
// curr := current | ||
// f.Iterator <- &curr | ||
// limit-- | ||
// } | ||
// }() | ||
// return f | ||
// } | ||
|
||
// func main() { | ||
// f := newFibonacci(10) | ||
// for r := range f.Iterator { | ||
// fmt.Println(*r) | ||
// } | ||
// } | ||
|
||
package main | ||
|
||
import "fmt" | ||
|
||
func fibonacci(limit int) chan int { | ||
c := make(chan int) | ||
a := 0 | ||
b := 1 | ||
go func() { | ||
var current, next int | ||
for { | ||
if limit == 0 { | ||
close(f.Iterator) | ||
close(c) | ||
return | ||
} | ||
if current != 0 || next != 0 { | ||
next, current = current+next, next | ||
} else { | ||
next = 1 | ||
} | ||
curr := current | ||
f.Iterator <- &curr | ||
c <- a | ||
a, b = b, a+b | ||
limit-- | ||
} | ||
}() | ||
return f | ||
return c | ||
} | ||
|
||
func main() { | ||
f := newFibonacci(10) | ||
for r := range f.Iterator { | ||
fmt.Println(*r) | ||
for r := range fibonacci(20) { | ||
fmt.Printf("%v ", r) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type fibonacciChan chan int | ||
|
||
func (f fibonacciChan) Next() *int { | ||
c, ok := <-f | ||
if !ok { | ||
return nil | ||
} | ||
return &c | ||
} | ||
|
||
func fibonacci(limit int) fibonacciChan { | ||
c := make(chan int) | ||
a := 0 | ||
b := 1 | ||
go func() { | ||
for { | ||
if limit == 0 { | ||
close(c) | ||
return | ||
} | ||
c <- a | ||
a, b = b, a+b | ||
limit-- | ||
} | ||
}() | ||
return c | ||
} | ||
|
||
func main() { | ||
f := fibonacci(20) | ||
fmt.Printf("%v ", *f.Next()) | ||
fmt.Printf("%v ", *f.Next()) | ||
for r := range f { | ||
fmt.Printf("%v ", r) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type fibonacci struct { | ||
a, b int | ||
} | ||
|
||
func (f *fibonacci) Next() int { | ||
r := f.a | ||
f.a, f.b = f.b, f.a+f.b | ||
return r | ||
} | ||
|
||
func main() { | ||
f := fibonacci{0, 1} | ||
for i := 0; i < 20; i++ { | ||
fmt.Printf("%v ", f.Next()) | ||
} | ||
} |