高阶函数

高阶函数的定义:至少满足以下两个条件中的一个: (1)、函数可以作为参数被传递;(2)、函数可以作为返回值输出

Javascript语言中的函数满足高阶函数的条件。高阶函数的应用场景:

1、函数作为参数传递:a、回调函数

上面的代码在页面中创建 100个 div 节点,然后将这些节点都隐藏; 当节点创建好的时候,appendDiv 会执行之前传入的回调函数,将隐藏的操作和主函数分离开,可复用性更高。

b、Array.prototype.sort 的使用

Array.prototype.sort 接受一个函数作为参数,这个函数封装了数组元素的排序规则。

从大到小的数组排序:

不同的浏览器对sort 方法的实现有不同的 算法:

浏览器使用的 JavaScript 引擎排序算法源码地址
Google ChromeV8插入排序和快速排序暂无
Mozilla FirefoxSpiderMonkey归并排序暂无
SafariNitro(JavaScriptCore )归并排序和桶排序暂无
Microsoft Edge 和 IE(9+)Chakra快速排序暂无

就贴一下 webkit 的源码:(想着把这些排序算法都过一下,但是Google发现连桶排序都不是我当初看的那么简单,先挖个坑,等我把js 基础过了一遍一定总结)

webkit 源码  :

function sort(comparator) {
        "use strict";

        function min(a, b) {
            return a < b ? a : b;
        }

        function stringComparator(a, b) {
            let aString = a.string;
            let bString = b.string;

            let aLength = aString.length;
            let bLength = bString.length;
            let length = min(aLength, bLength);

            for (let i = 0; i < length; ++i) {
                let aCharCode = aString.@charCodeAt(i);
                let bCharCode = bString.@charCodeAt(i);

                if (aCharCode == bCharCode)
                    continue;

                return aCharCode - bCharCode;
            }

            return aLength - bLength;
        }
    }

2、函数作为返回值输出:应用场景更多,更能体现函数式编程的奥妙。

a、判断数据的类型:

减少代码的重复,提高代码的简洁性:

 

3、函数柯里化(  function currying)

currying 定义可以查看维基百科,但是维基百科翻译出来的中文语义不通,截取部分维基百科的英文语句:

 currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument.  (柯里化就是一项将有多个参数的函数求值问题转化为只有一个函数参数的函数求值的技术)

 For example, a function that takes two arguments, one from X and one from Y, and produces outputs in Z, by currying is translated into a function that takes a single argument from X and produces as outputs functions from Y to Z.

(举个例子,一个函数有两个参数 X 和 Y ,得到的函数值为Z ,该函数柯里化就是将该函数转化为 只有参数 X 和 得到函数值为Y与 Z 相关的函数,就是函数值为Y与Z 的关系式)

Currying is related to, but not the same as, partial application. (柯里化和部分应用有关,但并不是一样)

partial application: 

In computer science, partial application refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. Given a function  , we might fix the first argument, producing a function of type  . Evaluation of this function might be represented as  . Note that the result of partial function application in this case is a function that takes two arguments. Partial application is sometimes incorrectly called currying, which is a related, but distinct concept.

(大概意思是部分应用是固定一个函数参数不变,产生一个相似的但是函数参数更少的方法,那些函数式子是演示,自行理解。。。 部分应用有时会错误地被当作柯里化,但是它们虽然相关,但是是不同的概念)

个人理解,就是柯里化就是无论多少函数参数最终都会转化为一个函数参数,中间难免就会不断的函数嵌套;部分应用虽然也是减少参数,但是有点控制变量法的意味,保持感兴趣的参数不变,转化为参数更少的相似函数(参数也不一定是一个)。

在JavaScript中或许就是函数的连续调用,直到最终才求值。

当调用totalSum() 时,如果明确地带上一些参数,此时并不进行真正的求值计算,而是把这些参数保存起来,函数返回另一个函数,只有不带参数地执行totalSum() 时,才会利用前面保存的参数,得到最终的结果。

4、uncurrying

维基定义:Uncurrying is the dual transformation to currying, and can be seen as a form of defunctionalization. It takes a function {\displaystyle f}f whose return value is another function {\displaystyle g}g, and yields a new function {\displaystyle f'}f'that takes as parameters the arguments for both {\displaystyle f}f and {\displaystyle g}g, and returns, as a result, the application of {\displaystyle f}f and subsequently, {\displaystyle g}g, to those arguments. The process can be iterated.

Uncurrying 是currying 的对偶转换,。。。实在翻译不下去了

个人理解:uncurrying 的作用在与扩大函数的适用性,使本来作为特定对象所拥有的功能的函数可以被任意对象所用。因为在JavaScript中调用对象的某个方法时,不用去关心该对象是否被设计为拥有该方法,是动态语言的一般特点。也是鸭子类型的特点。鸭子类型:

程序设计中,鸭子类型(英语:Duck typing)是动态类型和某些静态语言的一种对象推断风格。(来源百度百科)

Uncurrying 的实现方式之一:

 

uncurrying 的另一种实现方式:

 (这恐怕是我写的最久的博文了,信息量巨大,要多消化)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值