Skip to content

Commit

Permalink
Update algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
shaardie committed Jul 24, 2020
1 parent 4d4ba88 commit a45232e
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 61 deletions.
4 changes: 2 additions & 2 deletions README.md
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).
23 changes: 0 additions & 23 deletions base/fibonacci.go

This file was deleted.

82 changes: 55 additions & 27 deletions channel/fibonacci.go
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)
}
}
40 changes: 40 additions & 0 deletions combination/fibonacci.go
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ package main
import "fmt"

type fibonacci struct {
current, next, limit int
a, b, limit int
}

func (f *fibonacci) Next() *int {
if f.limit == 0 {
return nil
}
if f.current != 0 || f.next != 0 {
f.next, f.current = f.current+f.next, f.next
} else {
f.next = 1
}
r := f.a
f.a, f.b = f.b, f.a+f.b
f.limit--
return &f.current
return &r
}

func main() {
f := &fibonacci{limit: 10}
f := fibonacci{0, 1, 20}
for {
r := f.Next()
if r == nil {
return
}
fmt.Println(*r)
fmt.Printf("%v ", *r)
}
}
20 changes: 20 additions & 0 deletions native-approach/native-approach.go
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())
}
}

0 comments on commit a45232e

Please sign in to comment.