开发者

Help with LINQ Expression

How to write a LINQ Expression (method call syntax pr开发者_StackOverfloweferred) that gives a list of fibonacci numbers lying within a certain range, say 1 to 1000 ?


OK; for a more "FP" answer:

using System;
using System.Collections.Generic;
using System.Linq;
static class Program
{
    static void Main()
    {
        Func<long, long, long, IEnumerable<long>> fib = null;
        fib = (n, m, cap) => n + m > cap ? Enumerable.Empty<long>()
            : Enumerable.Repeat(n + m, 1).Concat(fib(m, n + m, cap));

        var list = fib(0, 1, 1000).ToList();
    }
}

Note that in theory this can be written as a single lambda, but that is very hard.


Using the iterator-block answer from here:

    foreach (long i in Fibonacci()
           .SkipWhile(i => i < 1)
           .TakeWhile(i => i <= 1000)) {
        Console.WriteLine(i);
    }

or for a list:

var list = Fibonacci().SkipWhile(i => i < 1).TakeWhile(i => i <= 1000)
                 .ToList();

Output:

1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987


Here is enumerator base solution. Its a lazy evaluation. So next number is generated when MoveNext() is done.

   foreach (int k in Fibonacci.Create(10))
       Console.WriteLine(k);


    class Fibonacci : IEnumerable<int>
    {
        private FibonacciEnumertor fibEnum;
        public Fibonacci(int max) {
            fibEnum = new FibonacciEnumertor(max);
        }
        public IEnumerator<int> GetEnumerator() {
            return fibEnum;
        }


        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return GetEnumerator();
        }
        public static IEnumerable<int> Create(int max) {
            return new Fibonacci(max);
        }

        private class FibonacciEnumertor : IEnumerator<int>
        {
            private int a, b, c, max;
            public FibonacciEnumertor(int max) {
                this.max = max;
                Reset();
            }
            // 1 1 2 3 5 8
            public int Current {
                get {

                    return c;
                }
            }
            public void Dispose() {

            }

            object System.Collections.IEnumerator.Current {
                get { return this.Current; }
            }

            public bool MoveNext() {

                c = a + b;
                if (c == 0)
                    c = 1;
                a = b;
                b = c;
                ;
                return max-- > 0;
            }

            public void Reset() {
                a = 0;
                b = 0;
            }
        }
    }


not very performant:

val fibonacci = Enumerable
                  .Range(0, 1000)
                  .Aggregate(new List<int>{1,0}, (b,j)=>{
                                b.Insert(0,b[0]+b[1]);
                                return b; });


late, but a fast version with the "yield" keyword :-)

IEnumerable<int> Fibonacci(int limit)
{
 int number = 1, old = 0;
 while (number < limit)
 {
  yield return number;
  int tmp = number; number += old; old = tmp;
 }
}

var list = Fibonacci(1000).ToList();


Easiest way to print fibonacci using linq

        List<int> lst = new List<int> { 0, 1 };

        for (int i = 0; i <= 10; i++)
        {
            int num = lst.Skip(i).Sum();
            lst.Add(num);

            foreach (int number in lst)
                Console.Write(number + " ");
            Console.WriteLine();
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜