在Javascript、Python等语言里,MapFilterReduce是数组的常用方法,可以让你在实现一些数组操作时告别循环,具有很高的实用价值。它们三个的意义大家应该都清楚,有一个十分形象的解释如下:

然而,支持lambda表达式的C#也有类似的方法,但不是这样命名的。实现IEnumerable接口的类(如List、HashSet、继承Array的类等)都有如下等价方法:

  1. “Map” => Select方法
  2. “Filter” => Where方法
  3. “Reduce” => Aggregate方法

用法示例如下:

class SuperClass
{
public int Value { get; set; }
public SuperClass(int value)
{
this.Value = value;
}
} class ClassA : SuperClass
{
public ClassA(int value) : base(value) { }
} static class Test
{
static void test()
{
var list1 = new List<SuperClass>()
{
new SuperClass(), //元素0
new SuperClass(), //元素1
new SuperClass(), //元素2
}; //Select,结果为{元素0, 元素1, 元素2},类型为List<ClassA>
List<ClassA> list2 = list1.Select(e => e as ClassA).ToList(); //Where,结果为{元素1, 元素2},类型为List<ClassA>
List<ClassA> list3 = list2.Where(e => e.Value > ).ToList(); //Aggregate,结果为3,类型为int
int sum = list3.Aggregate(, (acc, e) => acc += e.Value);
}
}

C#数组的Map、Filter、Reduce操作的更多相关文章

  1. Js数组的map,filter,reduce,every,some方法

    var arr=[1,2,3,4,5,6]; res = arr.map(function(x){return x*x}) [1, 4, 9, 16, 25, 36] res = arr.filter ...

  2. 数组的高阶方法map filter reduce的使用

    数组中常用的高阶方法: foreach    map    filter    reduce    some    every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...

  3. python 内置函数 map filter reduce lambda

    map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...

  4. 如何在python3.3用 map filter reduce

    在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0  ...

  5. Swift map filter reduce 使用指南

    转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...

  6. python常用函数进阶(2)之map,filter,reduce,zip

    Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...

  7. 数组去重,排序,重复次数,两个数组合并,两个数组去重,map(),filter(),reduce()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 高阶函数map,filter,reduce的用法

    1.filter filter函数的主要用途是对数组元素进行过滤,并返回一个符合条件的元素的数组 let nums = [10,20,30,111,222,333] 选出nums中小于100的数: l ...

  9. 聊一聊数组的map、reduce、foreach等方法

    聊聊数组遍历方法 JS 数组的遍历方法有好几个: every some filter foreach map reduce 接下来我们来一个个地交流下. every() arr.every(callb ...

  10. lambda,map,filter,reduce

    lambda 编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数.返回一个函数对象. func = lambda x,y:x+y fu ...

随机推荐

  1. Angular2 HttpClient (一)

    @angular/common/http 中的 HttpClient 类为 Angular 应用程序提供了一个简化的 API 来实现 HTTP 客户端功能.它基于浏览器提供的 XMLHttpReque ...

  2. iOS 8出色的跨应用通信效果:解读Action扩展

    本文转载至 http://mobile.51cto.com/iphone-464809.htm 用程序扩展最初于WWDC 2014大会上正式亮相,这是一种将iOS应用程序功能扩展至系统其它组成部分的途 ...

  3. ZOJ 3605 Find the Marble(dp)

    Find the Marble Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice and Bob are playing a game. ...

  4. 修改mysql root的秘密

    修改mysql root的秘密 ');

  5. linux 将同后缀名的文件统一移至另一个文件夹

    w

  6. IE8数组不支持indexOf方法的解决办法

    在使用indexof方法之前加上以下代码就可以了. if (!Array.prototype.indexOf){           Array.prototype.indexOf = functio ...

  7. 针对Quant的Python快速入门指南

    作者:用Python的交易员 (原创文章,转载请注明出处) 最近有越来越多的朋友在知乎或者QQ上问我如何学习入门Python,就目前需求来看,我需要写这么一篇指南. 针对整个vn.py框架的学习,整体 ...

  8. 荣誉墙项目day28 django常用函数

    1.在网页上渲染字符串from django.http import HttpResponsereturn HttpResponse(u"hello world") 2.渲染网页f ...

  9. HTTP API 设计指南(转)

    add by zhj (2014-12-16): 今天才知道,原画HeroKu是国外一个很有名的PaaS提供商,公司很可能会将app迁移到他们那里 英文原文: HTTP API Design Guid ...

  10. Linux的概念与体系(转)

    学linux就用它了 http://www.cnblogs.com/vamei/archive/2012/10/10/2718229.html