介绍

如果说函数是程序中的基本模块,代码段,那高阶函数就是函数的高阶(级)版本,其基本定义如下:
  • 函数自身接受一个或多个函数作为输入。
  • 函数自身能输出一个函数,即函数生产函数。
满足其中一个条件就可以称为高阶函数。高阶函数在函数式编程中大量应用,c#在3.0推出Lambda表达式后,也开始逐渐使用了。

阅读目录

  1. 接受函数
  2. 输出函数
  3. Currying(科里化)

接受函数

为了方便理解,都用了自定义。

代码中TakeWhileSelf 能接受一个函数,可称为高阶函数。

 //自定义委托
public delegate TResult Function<in T, out TResult>(T arg); //定义扩展方法
public static class ExtensionByIEnumerable
{
public static IEnumerable<TSource> TakeWhileSelf<TSource>(this IEnumerable<TSource> source, Function<TSource, bool> predicate)
{
foreach (TSource iteratorVariable0 in source)
{
if (!predicate(iteratorVariable0))
{
break;
}
yield return iteratorVariable0;
}
}
}
class Program
{
//定义个委托 static void Main(string[] args)
{
List<int> myAry = new List<int> { , , , , , , , , , }; Function<int, bool> predicate = (num) => num < ; //定义一个函数 IEnumerable<int> q2 = myAry.TakeWhileSelf(predicate); // foreach (var item in q2)
{
Console.WriteLine(item);
}
/*
* output:
* 1
* 2
* 3
*/
}
}

输出函数

代码中OutPutMehtod函数输出一个函数,供调用。

   var t = OutPutMehtod();  //输出函数
bool result = t(); /*
* output:
* true
*/ static Function<int, bool> OutPutMehtod()
{
Function<int, bool> predicate = (num) => num < ; //定义一个函数 return predicate;
}

Currying(科里化)

一位数理逻辑学家(Haskell Curry)推出的,连Haskell语言也是由他命名的。然后根据姓氏命名Currying这个概念了。

上面例子是一元函数f(x)=y 的例子。

那Currying如何进行的呢? 这里引下园子兄弟的片段。

假设有如下函数:f(x, y, z) = x / y +z. 要求f(4,2, 1)的值。

首先,用4替换f(x, y, z)中的x,得到新的函数g(y, z) = f(4, y, z) = 4 / y + z

然后,用2替换g(y, z)中的参数y,得到h(z) = g(2, z) = 4/2 + z

最后,用1替换掉h(z)中的z,得到h(1) = g(2, 1) = f(4, 2, 1) = 4/2 + 1 = 3

很显然,如果是一个n元函数求值,这样的替换会发生n次,注意,这里的每次替换都是顺序发生的,这和我们在做数学时上直接将4,2,1带入x / y + z求解不一样。

在这个顺序执行的替换过程中,每一步代入一个参数,每一步都有新的一元函数诞生,最后形成一个嵌套的一元函数链。

于是,通过Currying,我们可以对任何一个多元函数进行化简,使之能够进行Lambda演算。

用C#来演绎上述Currying的例子就是:

var fun=Currying();
Console.WriteLine(fun(6)(2)(1));
/*
* output:
* 4
*/ static Function<int, Function<int, Function<int, int>>> Currying()
{
return x => y => z => x / y + z;
}

参考 http://www.cnblogs.com/fox23/archive/2009/10/22/intro-to-Lambda-calculus-and-currying.html

最新文章

  1. navicat连接虚拟机(centos)中的mysql
  2. Google的分布式关系型数据库F1和Spanner
  3. HDU 2577 How to Type(dp题)
  4. CentOS6.5 yum安装 apache+svn安装配置
  5. leetcode6 Reverse Words in a String 单词取反
  6. jQuery.Autocomplete实现自动完成功能
  7. 如何使用JAVA语言抓取某个网页中的邮箱地址
  8. setuptools,easy_install使用
  9. 330. Patching Array
  10. linux下vi编辑某文件时,操作出现 错误提示: E325: ATTENTION 2, Found a swap file by the name &quot;.p1.c.swp&quot;
  11. 用Portable.BouncyCastle来进行加解密的代码demo
  12. iOS开发简记(4):录音AVAudioRecorder
  13. [NOI2012]美食节(费用流)
  14. ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接
  15. Java环境下shiro的测试-认证与授权
  16. Centos7使用pxe安装KVM虚拟机
  17. Minimum Window Substring LT76
  18. 为什么transform对行内元素不生效
  19. 初识HDFS原理及框架
  20. mysql主从延时临时解决办法

热门文章

  1. Ubuntu(Linux系统)虚拟机工具vmtools详细说明
  2. JsonHelper developed by using Newtonsoft.Json.NET, Deserialize to &lt;T&gt; object , XmlToJson/JsonToXml, QuoteName by using JToken Path.
  3. linux中redis的php扩展安装
  4. 各大IT技术博客排行榜
  5. WebADNuke整理
  6. 【转】SSL协议、SET协议、HTTPS简介
  7. Spark性能优化-coalesce(n)
  8. vim配置及快捷键
  9. bing的简单英文字典工具
  10. js获取cookie中存储的值