zoukankan      html  css  js  c++  java
  • c#语言-高阶函数

    介绍

    如果说函数是程序中的基本模块,代码段,那高阶函数就是函数的高阶(级)版本,其基本定义如下:
    • 函数自身接受一个或多个函数作为输入。
    • 函数自身能输出一个函数,即函数生产函数。
    满足其中一个条件就可以称为高阶函数。高阶函数在函数式编程中大量应用,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> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    
                Function<int, bool> predicate = (num) => num < 4;  //定义一个函数
    
                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(1);
    
                /*
                 * output:
                 * true
                 */
    
      static Function<int, bool> OutPutMehtod()
            {
                Function<int, bool> predicate = (num) => num < 4;  //定义一个函数 
    
                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

  • 相关阅读:
    sqlplus中var和print的使用
    sqlplus中break命令的使用
    android之OptionsMenu
    android之TabHost(下)
    android之TabHost(上)
    android之dialog
    android之listview
    android开发事件监听
    决策树ID3算法示例
    回溯法-设置碉堡
  • 原文地址:https://www.cnblogs.com/mushroom/p/4119866.html
Copyright © 2011-2022 走看看