.net3.5下使用LINQ递归算法实现简洁代码
2009-03-29 12:18
274 查看
pre.hl { color:#ddbb00; background-color:#000000; font-size:10pt; font-family:'Verdana';}
.hl.line { color:#898989; }
.hl.kwa { color:#ffed8a; font-weight:bold; }
.hl.sym { color:#ababab; }
.hl.slc { color:#978345; font-style:italic; }
.hl.kwb { color:#ffed8a; }
.hl.kwd { color:#ddbb00; font-weight:bold; }
.hl.num { color:#ffffff; }
.parameter{font-family:Verdana,Arial,Helvetica,sans-serif;font-size:100%;font-style:italic;margin:0}
.net framework 3.5
有了Linq使得对委托有了更多的支持,下面让我们来看几个有趣的示例.通常情况下,我们实现一个递归算法要写一个函数,同时还有调用的几行代码.
现在来看使用Linq的如何实现简洁的代码,代码如下:
Factorial是阶乘,接着是Fibonacci数列.之后把这个定义为一个名叫Funcional类,其中包含一个static方法.Factorial2使用这个类再实现阶乘,是不是简单的多.接着是RecursionGetFiles一个实际的应用,递归遍历文件夹取得文件名列表.像树型结构算法都可以用它来实现,是不是很有趣?
其中几个关键方法可以参考:
Func<(Of <(T,
TResult>)>)
委托
封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。
Enumerable.SelectMany<(Of
<(TSource,
TResult>)>)
方法 (IEnumerable<(Of
<(TSource>)>),
Func<(Of <(TSource,
IEnumerable<(Of
<(TResult>)>)>)>))
将序列的每个元素投影到
IEnumerable<(Of
<(T>)>)
并将结果序列合并为一个序列。
Enumerable.Concat<(Of
<(TSource>)>)
方法
连接两个序列。
Author: Petter Liu http://wintersun.cnblogs.com
.hl.line { color:#898989; }
.hl.kwa { color:#ffed8a; font-weight:bold; }
.hl.sym { color:#ababab; }
.hl.slc { color:#978345; font-style:italic; }
.hl.kwb { color:#ffed8a; }
.hl.kwd { color:#ddbb00; font-weight:bold; }
.hl.num { color:#ffffff; }
.parameter{font-family:Verdana,Arial,Helvetica,sans-serif;font-size:100%;font-style:italic;margin:0}
.net framework 3.5
有了Linq使得对委托有了更多的支持,下面让我们来看几个有趣的示例.通常情况下,我们实现一个递归算法要写一个函数,同时还有调用的几行代码.
现在来看使用Linq的如何实现简洁的代码,代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using NUnit.Framework; 7 8 namespace WindowsFormsApplication1 9 { 10 /// <summary> 11 /// TestRecursionWithLINQ 12 /// </summary> 13 /// <remark>Author : PetterLiu 2009-03-29 11:28 http://wintersun.cnblogs.com </remark> 14 [TestFixture] 15 public class TestRecursionWithLINQ 16 { 17 18 /// <summary> 19 /// Factorials this instance. 20 /// </summary> 21 /// <remark>Author : PetterLiu 2009-03-29 11:28 http://wintersun.cnblogs.com </remark> 22 [Test] 23 public void Factorial() 24 { 25 Func<int, int> fib = null; 26 fib = n => (n == 1) ? 1 : fib(n - 1) * n; 27 Console.WriteLine(fib(5)); 28 } 29 30 /// <summary> 31 /// Fibonaccis this instance. 32 /// </summary> 33 /// <remark>Author : PetterLiu 2009-03-29 11:28 http://wintersun.cnblogs.com </remark> 34 [Test] 35 public void Fibonacci() 36 { 37 Func<int, int> fib = null; 38 fib = n => n > 1 ? fib(n - 1) + fib(n - 2) : n; 39 Console.WriteLine(fib(6)); 40 } 41 42 43 /// <summary> 44 /// Recursions the get files. 45 /// </summary> 46 /// <remark>Author : PetterLiu 2009-03-29 11:27 http://wintersun.cnblogs.com </remark> 47 [Test] 48 public void RecursionGetFiles() 49 { 50 var RecGetFiles = 51 Functional.Y<string, IEnumerable<string>> 52 (f => d => Directory.GetFiles(d).Concat(Directory.GetDirectories(d).SelectMany(f))); 53 54 foreach (var f in RecGetFiles(Directory.GetCurrentDirectory())) 55 Console.WriteLine(f); 56 57 } 58 59 /// <summary> 60 /// Factorial2s this instance. 61 /// </summary> 62 /// <remark>Author : PetterLiu 2009-03-29 11:28 http://wintersun.cnblogs.com </remark> 63 [Test] 64 public void Factorial2() 65 { 66 var dd = Functional.Y<int, int>(h => m => (m == 1) ? 1 : h(m - 1) * m); 67 Console.WriteLine(dd(5)); 68 } 69 } 70 71 /// <summary> 72 /// Functional 73 /// </summary> 74 /// <remark>Author : Wes Dyer</remark> 75 public class Functional 76 { 77 /// <summary> 78 ///delegate Func<A, R> 79 /// </summary> 80 private delegate Func<A, R> Recursive<A, R>(Recursive<A, R> r); 81 /// <summary> 82 /// Ys the specified f. 83 /// </summary> 84 /// <typeparam name="A"></typeparam> 85 /// <typeparam name="R"></typeparam> 86 /// <param name="f">The f.</param> 87 /// <returns></returns> 88 public static Func<A, R> Y<A, R>(Func<Func<A, R>, Func<A, R>> f) 89 { 90 Recursive<A, R> rec = r => a => f(r(r))(a); 91 return rec(rec); 92 } 93 } 94 }
Factorial是阶乘,接着是Fibonacci数列.之后把这个定义为一个名叫Funcional类,其中包含一个static方法.Factorial2使用这个类再实现阶乘,是不是简单的多.接着是RecursionGetFiles一个实际的应用,递归遍历文件夹取得文件名列表.像树型结构算法都可以用它来实现,是不是很有趣?
其中几个关键方法可以参考:
Func<(Of <(T,
TResult>)>)
委托
封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。
Enumerable.SelectMany<(Of
<(TSource,
TResult>)>)
方法 (IEnumerable<(Of
<(TSource>)>),
Func<(Of <(TSource,
IEnumerable<(Of
<(TResult>)>)>)>))
将序列的每个元素投影到
IEnumerable<(Of
<(T>)>)
并将结果序列合并为一个序列。
Enumerable.Concat<(Of
<(TSource>)>)
方法
连接两个序列。
Author: Petter Liu http://wintersun.cnblogs.com
相关文章推荐
- .net3.5下使用LINQ递归算法实现简洁代码
- Android仿ios微信左划条目删除、置顶的实现,代码简洁,更容易理解使用
- swift 中使用扩展实现代理,让程序猿省心,让代码简洁
- 一款简洁的纯css3代码实现的动画导航
- vue使用vue-i18n实现国际化的实现代码
- 不写代码,使用twistd实现ftp服务器
- 使用搜狗输入法实现对代码快捷注释
- 一个使用J2ME技术实现的计算器的代码
- jQuery实现的简洁下拉菜单导航效果代码
- 编写高质量代码改善C#程序的157个建议——建议15: 使用dynamic来简化反射实现
- JAVA中使用FTPClient实现文件上传下载实例代码
- 使用C#给PDF文档添加注释的实现代码
- Spring MVC代码实例系列-12:使用自带的validation实现自定义message表单校验
- 代码非常简洁且兼容多浏览器的拖动层实现代码
- (转)flex中使用swc实现更好的界面代码分离
- 一款简洁的纯css3代码实现的动画导航
- Android 使用Intent传递数据的实现思路与代码
- vue select组件的使用与禁用实现代码
- ARP源码实现及代码说明,可直接编译使用
- 使用阿里大于平台发送短信验证码java代码实现