C#函数式编程风格-范型Filter,Map,Reduct函数的实现
早上看园友的一篇文章《lambda与闭包》,忽然间想起了以前刚学Python,刚接触FP时的高兴劲。对FP的no-side-effect的向往,对Declaration式编程的喜爱,让我对于编程,对于另一种程序设计的思想有了种转变。
还记得那时,看到Python中的built-in函数Filter,Map,Reduce,心想为什么.NET的BCL中怎么就没有呢。C#3.0出来以后,基本上已经可以DIY一个山寨版的Filter,Map,Reduce了,看代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace FuncPro {class Program{static T[] Filter<T>(Func<T, bool> func, IEnumerable<T> iSource){List<T> iResult = new List<T>();foreach (var item in iSource){if (func(item))iResult.Add(item);}return iResult.ToArray();}static T[] Map<T>(Func<T, T> func, IEnumerable<T> iSource){List<T> iResult = new List<T>();foreach (var item in iSource){iResult.Add(func(item));}return iResult.ToArray();}static T Reduce<T>(Func<T, T, T> func, IEnumerable<T> iSource){T sum = default(T);foreach (var item in iSource){sum = func(sum, item);}return sum;}static void PrintArray(int[] iSource){Console.Write("\t");iSource.ToList().ForEach(x => Console.Write("{0} ", x));Console.WriteLine();}static void Indent(string msg){Console.WriteLine("{0}:", msg);}static void Main(string[] args){int[] iSource = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };Indent("Source");PrintArray(iSource);int[] iFilterResult = Filter(x => x > 5, iSource);Indent("Filter, item more than 5");PrintArray(iFilterResult);int[] iMapResult = Map(x => x * 2, iSource);Indent("Map, multiple with 2");PrintArray(iMapResult);int iReduceResult = Reduce((x, y) => x + y, iSource);Indent("Reduce");Console.Write("\t{0}\n ", iReduceResult);Console.ReadKey();}} }
前些日子和一个朋友闲聊到.NET加入了很多函数式编程的元素,应该可以写出函数式编程风格的代码来,也应该算是支持函数式编程的语言了,今天随手写了代码感觉确实是这样了。
PS.
今天看《CLR via C# 3e》时看到Jefferey利用扩展方法(Extension Method)来提高代码可读性的例子,我将上面的代码重构了一下,以提高可读性:
static class ExtendMethod {public static void ShowItems<T>(this IEnumerable<T> collection){Console.Write("\t");collection.ToList().ForEach((x) => Console.Write("{0} ", x));Console.WriteLine();} } class Program {static T[] Filter<T>(Func<T, bool> func, IEnumerable<T> iSource){List<T> iResult = new List<T>();foreach (var item in iSource){if (func(item))iResult.Add(item);}return iResult.ToArray();}static T[] Map<T>(Func<T, T> func, IEnumerable<T> iSource){List<T> iResult = new List<T>();foreach (var item in iSource){iResult.Add(func(item));}return iResult.ToArray();}static T Reduce<T>(Func<T, T, T> func, IEnumerable<T> iSource){T sum = default(T);foreach (var item in iSource){sum = func(sum, item);}return sum;}static void PrintArray(int[] iSource){Console.Write("\t");iSource.ToList().ForEach(x => Console.Write("{0} ", x));Console.WriteLine();}static void Indent(string msg){Console.WriteLine("{0}:", msg);}static void Main(string[] args){int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };Indent("Source");source.ShowItems();int[] filterResult = Filter(x => x > 5, source);Indent("Filter, item more than 5");filterResult.ShowItems();int[] mapResult = Map(x => x * 2, source);Indent("Map, multiple with 2");mapResult.ShowItems();int reduceResult = Reduce((x, y) => x + y, source);Indent("Reduce");new[] { reduceResult }.ShowItems();Console.ReadKey();} }
转载于:https://www.cnblogs.com/Jerry-Chou/archive/2010/05/04/generic-of-filter-map-reduce-in-csharp.html
C#函数式编程风格-范型Filter,Map,Reduct函数的实现相关推荐
- 函数式编程是啥玩意?map() reduce()(reduce()函数将数字列表转换为x进制数字)闭包、装饰器、偏函数
反正看了百度百科的介绍我是没太看懂... 参考文章:函数式编程 看了这篇,不错,对python函数式编程有些许理解了: 变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数 ...
- python函数式编程 pdf-Python函数式编程指南(二):从函数开始
2. 从函数开始 2.1. 定义一个函数 如下定义了一个求和函数: def add(x, y): return x + y 关于参数和返回值的语法细节可以参考其他文档,这里就略过了. 使用lambda ...
- Scala函数式编程(三) scala集合和函数
前情提要: scala函数式编程(二) scala基础语法介绍 scala函数式编程(二) scala基础语法介绍 前面已经稍微介绍了scala的常用语法以及面向对象的一些简要知识,这次是补充上一章的 ...
- Python函数式编程简介(一)高阶函数
本文概括介绍Python函数式编程的一些概念及用法,详细请参考: https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df ...
- js高阶函数的使用-filter/map/reduct
通过案例来讲述这几个函数的使用 // filter函数的使用,过滤数组中小于50的元素const nums = [11, 22, 33, 44, 55, 111, 333];let num1 = nu ...
- 翻译连载 |《你不知道的JS》姊妹篇 |《JavaScript 轻量级函数式编程》- 第 4 章:组合函数...
原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...
- 2. Python函数式编程中的字符串,元组,函数的分类,高阶函数,一篇文章都介绍一遍
函数式编程中的字符串 在函数式编程中,经常用到 Python 字符串,因其是不可变数据结构. 字符串本身是一个对象,具备很多对象方法,与常识中函数的使用不太相同,例如下述代码 my_str = &qu ...
- java 柯里化_函数式编程(Java描述)——Java中的函数及其柯里化
本文继续上一篇的内容 在Java中,函数可以表现为一个普通的方法.一个lambda表达式,又或者方法引用,甚至是匿名类.本文不会介绍匿名类这种形式. 方法 Java中的方法,Java使用方法这一概念来 ...
- “主要的编程范型”及其语言特性关系(多图)
"主要的编程范型"(The principal programming paradigms)这幅图,其实出现得不算早,作者在2007年完成了该图的1.0版,到2008年更新至v1. ...
最新文章
- c语言程序与设计苏小红,c语言程序设计苏小红
- VSCODE常用开发环境配置----保存
- 数据库的设计经验(经典)
- Comperhend the OP-sizeof deeply!
- Springboot中使用jpa
- Py之turicreate:turicreate的简介、安装、使用方法之详细攻略
- Android 开发环境在 Windows7 下的部署安装
- 成功数据恢复一例LINUX EXT3 下误删除ORACLE数据库
- ECCV 2020 谷歌论文盘点—Poster 篇
- Kafka启动报错:Timed out waiting for connection while in state: CONNECTING
- 程序读取计算机设备管理器中各设备的状态(启用/禁用)?(转自大富翁)
- android异步工作,Android异步消息机制详解
- 实战 SQL!金融机构可疑支付交易的监测 | 原力计划
- (秒杀项目) 4.8 异步化扣减库存(核心)
- java 幂函数_java的math常用方法
- 【转载】士兵突击 经典语录
- 微信小程序 通过百度API接口实现汉译英翻译
- linux log4cxx 静态库,log4cxx的个人实践
- 中国社交产品十年记....
- 加拿大LMIA劳工批文有效期延长至18个月,为海外劳工在加拿大临时工作及移民创造便利条件
热门文章
- 混合云,让你看的清清楚楚明明白白真真切切
- 创业公司,老板说等公司做大了,给5%股权,建议你不要轻易相信
- Qt之使用GraphicsView框架实现思维导图功能
- 微信公众号平搜索排名,如何让公众号搜索排名靠前,公众号文章关键词排名规则
- oracle创建bigfile表空间,表空间管理之bigfile表空间设置
- day fit into much one too_PGone Talking too much歌词
- 马哈蒂尔:马云思维超前于时代,阿里巴巴能助力马来西亚重回正轨
- Markdown 字体颜色汇总表(简洁版)- 适用于所有需要颜色任务
- 关于ppt无法添加页码的解决办法
- vtk 曲线 样式_VTK笔记——拟合样条曲线(Parametric Spline)-Go语言中文社区