淮安做微信网站,苏州高新区核酸检测,品牌免费网站建设,上海平台推广的公司本文给出一个Functional Programming和Lazy Code的一个例子。跟着思路走#xff0c;关键的地方会有相应的说明。 我们想实现一个判断素数的小程序#xff0c;如下#xff1a; using System;namespace FunctionalProgramming
{class Program{static void Main(st…本文给出一个Functional Programming和Lazy Code的一个例子。跟着思路走关键的地方会有相应的说明。 我们想实现一个判断素数的小程序如下 using System;namespace FunctionalProgramming
{class Program{static void Main(string[] args){var number int.Parse(Console.ReadLine());var result true;for(long i2;inumber;i){if(number%i0){result false;break;}}Console.WriteLine(result);}}
} 上面的代码虽能完成功能但不够整洁。不是我们想要的代码。我们提取其中的方法重新实现如下 using System;namespace RefactorExample
{class Program{static void Main(string[] args){var number int.Parse(Console.ReadLine());var result IsPrime(number);;Console.WriteLine(result);}//Refactorprivate static bool IsPrime(int number){var resulttrue;for (long i 2; i number; i){if (number%i 0){result false;break;}}return result;}}
} 以此为基础借助扩展方法针对一个数组我们可以方便的改写上面的代码如下: using System;
using System.Collections.Generic;
using System.Linq;namespace AHigherCalling
{static class Program{static void Main(string[] args){var number new [] {3,5,7,9,11,13};foreach (var prime in number.Find(IsPrime).Take(2)){Console.WriteLine(prime);}Console.ReadKey();}private static IEnumerableint Find(this IEnumerableint values,Funcint,bool test ){var result new Listint();foreach (var value in values){if (test(value))result.Add(value);}return result;}private static bool IsPrime(int value){var result true;for (long i 2; i value; i){if (value % i 0){result false;break;}}return result;}//一些其他的类似IsPrime的方法private static bool IsEven(int value){return value%2 0;}private static bool IsOdd(int value){return value%2 ! 0;}}
} 这样做的目的是提供更清洁的语法并提高程序应对变化的能力。例如我们实现的两个类似IsPrime方法。 单看Find方法。貌似没有什么问题当然我们可以用ReShaper生成LINQ语句替换掉那个for循环。 View Code using System;
using System.Collections.Generic;
using System.Linq;namespace AHigherCalling
{static class Program{static void Main(string[] args){var number new [] {3,5,7,9,11,13};foreach (var prime in number.Find(IsPrime).Take(2)){Console.WriteLine(prime);}Console.ReadKey();}private static IEnumerableint Find(this IEnumerableint values,Funcint,bool test ){//LINQ替换掉了for循环return values.Where(value test(value)).ToList();}private static bool IsPrime(int value){var result true;for (long i 2; i value; i){if (value % i 0){result false;break;}}return result;}//一些其他的类似IsPrime的方法private static bool IsEven(int value){return value%2 0;}private static bool IsOdd(int value){return value%2 ! 0;}}
} 程序正常运行。貌似我们的程序没有什么问题了那是因为我们这个数组不够大我们把numbers变的很大如下: using System;
using System.Collections.Generic;
using System.Linq;namespace AHigherCalling
{static class Program{static void Main(string[] args){foreach (var prime in GetRandomNumbers().Find(IsPrime).Take(2)){Console.WriteLine(prime);}Console.ReadKey();}private static IEnumerableint GetRandomNumbers(){//yield return 3;//yield return 5;//yield return 7;//yield return 9;//yield return 11;//yield return 13;Random random new Random();while (true){yield return random.Next(90000, 100000);}}private static IEnumerableint Find(this IEnumerableint values,Funcint,bool test ){//LINQ替换掉了for循环return values.Where(value test(value)).ToList();}private static bool IsPrime(int value){var result true;for (long i 2; i value; i){if (value % i 0){result false;break;}}return result;}//一些其他的类似IsPrime的方法private static bool IsEven(int value){return value%2 0;}private static bool IsOdd(int value){return value%2 ! 0;}}
} 程序就也陷入死循环了而我这次想获取的只是其前2个而已。问题出在哪里 不难发现当然在Find这里 还记得前面我实现的Find方法吗如下 private static IEnumerableint Find(this IEnumerableint values,Funcint,bool test ){var result new Listint();foreach (var value in values){if (test(value))result.Add(value);}return result;} 使用ReShaper重构过如下 private static IEnumerableint Find(this IEnumerableint values,Funcint,bool test ){//LINQ替换掉了for循环return values.Where(value test(value)).ToList();} 简单分析问题所在后可以想到的解决方法如下 使用迭代器 private static IEnumerableint Find(this IEnumerableint values,Funcint,bool test ){foreach (var value in values){if (test(value))yield return value;}} 或是 private static IEnumerableint Find(this IEnumerableint values,Funcint,bool test ){return values.Where(value test(value));} 这样程序就按我既定的想法运行了。 从ReShaper生成的代码来看问题出在立即执行的ToList()那里但毕竟ReShaper只是个工具其翻译的是我们的代码不推卸责任的说问题出在我们因为使用迭代器更正后ReShaper不也生成了正确的代码了么 update: from: http://stackoverflow.com/questions/7062882/searching-a-tree-using-linq