I’m embarrassed, and not just a little. I mean a lot.
[I made a blogpost][1] about the yield keyword and the fibonacci sequence and did not even check if I got the correct results. I just checked if the code ran.
I was wrong and I was stupid. I would never do this in production. My main application has over 9k tests before I let it go in production.
But for my blogposts I sometimes go to quickly.
It looks ok, hit Publish.
And then this happens.
Great explanation of the Yield keyword. However, there is a logic error in the Fibs function of your “less pretty” code. Here is the output …1,1,2,4,8,16,32, etc… It should be … 1,1,2,3,5,8,13,etc… to be a true Fibonacci series. I tried including my fix for that but had trouble attaching the code so I gave up.
Does this make me a Fibonazi? 😉
I am ashamed. I really am. And I say sorry to my readers. I should do better.
So I’ll make up for it.
Here are my tests.
Imports Nunit.FrameWork
Namespace DefaultNamespace
Public Class TestFibonaci
<Test>
Public sub IfFirstNumberIs1
Dim fibonacci = New Fibonacci().Result
Assert.AreEqual(1, fibonacci(0))
End Sub
<Test>
Public Sub IfSecondNumberIs1()
Dim fibonacci = New Fibonacci().Result
Assert.AreEqual(1, fibonacci(1))
End Sub
<Test>
Public Sub IfThirdNumberIs2()
Dim fibonacci = New Fibonacci().Result
Assert.AreEqual(2, fibonacci(2))
End Sub
<Test>
Public Sub IfFourthNumberIs3()
Dim fibonacci = New Fibonacci().Result
Assert.AreEqual(3, fibonacci(3))
End Sub
<Test>
Public Sub IfFifthNumberIs5()
Dim fibonacci = New Fibonacci().Result
Assert.AreEqual(5, fibonacci(4))
End Sub
<Test>
Public Sub IfSixthNumberIs8()
Dim fibonacci = New Fibonacci().Result
Assert.AreEqual(8, fibonacci(5))
End Sub
<Test>
Public Sub IfSeventhNumberIs13()
Dim fibonacci = New Fibonacci().Result
Assert.AreEqual(13, fibonacci(6))
End Sub
End Class
End Namespace```
And here is the class with the code I published.
```vbnet
Namespace DefaultNamespace
Public Class Fibonacci
Public Iterator Function Result() As IEnumerable(Of Integer)
Dim previous As Integer
Dim current As Integer
Yield 1
Yield 1
previous = 1
current = 1
While current < Integer.MaxValue - previous
current += previous
previous = current
Yield current
End While
End Function
End Class
End NameSpace```
And this is what ncrunch tells me I should have seen before posting that piece of crap.
<div class="image_block">
<a href="https://lessthandot.z19.web.core.windows.net/wp-content/uploads/users/chrissie1/yield/yield2.png?mtime=1348211659"><img alt="" src="https://lessthandot.z19.web.core.windows.net/wp-content/uploads/users/chrissie1/yield/yield2.png?mtime=1348211659" width="537" height="292" /></a>
</div>
Oh man, that is so wrong. Let’s fix it.
```vbnet
Namespace DefaultNamespace
Public Class Fibonacci
Public Iterator Function Result() As IEnumerable(Of Integer)
Dim previous As Integer
Dim current As Integer
Yield 1
Yield 1
previous = 1
current = 1
While current < Integer.MaxValue - previous
Dim tempprevious = current
current += previous
previous = tempprevious
Yield current
End While
End Function
End Class
End NameSpace```
And now I can be reasonably sure that it works as it should.
<div class="image_block">
<a href="https://lessthandot.z19.web.core.windows.net/wp-content/uploads/users/chrissie1/yield/yield3.png?mtime=1348212035"><img alt="" src="https://lessthandot.z19.web.core.windows.net/wp-content/uploads/users/chrissie1/yield/yield3.png?mtime=1348212035" width="529" height="289" /></a>
</div>
And now I can easily play with it and see that the shorter version gives me the same correct results.
```vbnet
Namespace DefaultNamespace
Public Class Fibonacci
Public Iterator Function Result() As IEnumerable(Of Integer)
Yield 1
Yield 1
For Each n In Me.Result.Zip(Me.Result.Skip(1), Function(x, y) x + y)
Yield n
Next
End Function
End Class
End NameSpace```
Lesson learned.
And once again, SORRY.
[1]: /index.php/DesktopDev/MSTech/new-in-vb11-the-yield