D Recurrences

Ben Grabham Evil.Nebster at gmail.com
Thu Jun 9 20:30:53 PDT 2011


On 10/06/11 00:10, Ali Çehreli wrote:
> For what it's worth, here is a Fibonacci range. (Translated from
> D.ershane's "Aralıklar" (Ranges) chapter.)
>
> import std.stdio;
> import std.range;
>
> struct FibonacciRange
> {
>      int first = 0;
>      int second = 1;
>
>      enum empty = false;
>
>      @property int front() const
>      {
>          return first;
>      }
>
>      void popFront()
>      {
>          int next = first + second;
>          first = second;
>          second = next;
>      }
>
>      FibonacciRange save() const
>      {
>          return this;
>      }
> }
>
> void main()
> {
>      writeln(take(FibonacciRange(), 20));
> }
>
> Ali

Have a look at bearophiles answer above. It is a bit shorter.
Fibonacci was just an example, I don't actually use it anywhere :P

Thanks for another way though,
Nebster


More information about the Digitalmars-d mailing list