Commit f1a4d352 authored by Boris Mühmer (ADESTIS)'s avatar Boris Mühmer (ADESTIS) 💬
Browse files

restructured code

parent d7a90b68
Loading
Loading
Loading
Loading
+15 −7
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@ package iterators

import "math/big"

// https://en.wikipedia.org/wiki/Fibonacci_number

// FibonacciNumber ...
type FibonacciNumber struct {
	current *big.Int
@@ -16,14 +18,20 @@ func NewFibonacciNumberIterator() *FibonacciNumber {
	}
}

// Value ...
func (fn *FibonacciNumber) Value() *big.Int {
	return fn.current
}

// Next ...
func (f *FibonacciNumber) Next() *big.Int {
	r := f.current
func (fn *FibonacciNumber) Next() *big.Int {
	//r := f.current
	//n := big.NewInt(0)
	//n.Add(f.next, f.current)
	//f.current = f.next
	//f.next = n

	n := big.NewInt(0)
	n.Add(f.next, f.current)
	f.current = f.next
	f.next = n
	fn.current, fn.next = fn.next, fn.current.Add(fn.next, fn.current)

	return r
	return fn.current
}
+3 −2
Original line number Diff line number Diff line
@@ -6,7 +6,8 @@ import (

func TestIterators1(t *testing.T) {
	fn := NewFibonacciNumberIterator()
	for i := 0; i < 100; i++ {
		t.Logf("F(%d): %s", i, fn.Next())
	for i := 0; i < 20; i++ {
		t.Logf("F(%d): %s", i, fn.Value())
		fn.Next()
	}
}