Subscribe to The Madman Scribblings        RSS Feed

Fibonacci Sequence using LINQ

Icon 2 Comments
Just a quick blog post demonstrate a way generating the Fibonacci Sequence with LINQ.
Public Module FibExample
    Public Const ɸ As Decimal = 1.6180339887498949D
    Public Function Fib(n As Integer) As IEnumerable(Of Decimal)
        Dim v As Decimal = 1D
        Return Enumerable.Range(0, n).Select(Function(i) If(i < 2, i,If(i = 2, 1, v.SetValue(Math.Round(v * ɸ)))))
    End Function
    Public Function SetValue(Of T)(ByRef dest As T, value As T) As T
        dest = value
        Return value
    End Function
End Module

2 Comments On This Entry

Page 1 of 1


05 August 2012 - 07:37 PM
Neat example.

Ahhh LINQ, such a powerful concept I've been trying to incorporate into the work I've been doing since starting at a .NET shop six months ago. I try to use it where it makes sense although I admit I often struggle with the syntax, jumping from C# to VB all the time doesn't help either :)


07 August 2012 - 06:29 PM
It's kind of a cheat with it using mutable state. Since that state is local to the function makes that kinda OK.
Page 1 of 1

Search My Blog

Recent Entries

Recent Comments