Help with LINQ Expression

2019-03-17 01:55发布

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

6条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-17 02:18

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;
            }
        }
    }
查看更多
甜甜的少女心
3楼-- · 2019-03-17 02:19

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.

查看更多
欢心
4楼-- · 2019-03-17 02:23

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();
查看更多
Summer. ? 凉城
5楼-- · 2019-03-17 02:24

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();
        }
查看更多
smile是对你的礼貌
6楼-- · 2019-03-17 02:30

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
查看更多
ら.Afraid
7楼-- · 2019-03-17 02:35

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; });
查看更多
登录 后发表回答