zoukankan      html  css  js  c++  java
  • 函数柯里化

    一、柯里化

    柯里化是将函数和参数结合产生一个新的函数

    二、函数柯里化例子

    <!DOCTYPE html>
    <html lang="zh">
    
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <meta http-equiv="X-UA-Compatible" content="ie=edge" />
            <title>函数柯里化</title>
        </head>
    
        <body>
            <script type="text/javascript">
                function add(...values) {
                    let sum = 0;
    
                    for(var val of values) {
                        sum += val;
                    }
    
                    return sum;
                }
    
                Function.prototype.curry = function() {
                    let slice = Array.prototype.slice;
                    let defaultValue = slice.call(arguments);
                    let that = this;
                    return function() {
                        return that.apply(this, defaultValue.concat(slice.call(arguments)))
                    }
                }
                let add1 = add.curry(1,2);
                console.log(add1(5)); //8
                console.log(add1(3)); //6
            </script>
        </body>
    
    </html>

    3、一个有用的示例(数组排序)

    <!DOCTYPE html>
    <html lang="zh">
    
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <meta http-equiv="X-UA-Compatible" content="ie=edge" />
            <title>函数柯里化</title>
        </head>
    
        <body>
            <script type="text/javascript">
                Function.prototype.curry = function() {
                    let slice = Array.prototype.slice;
                    let defaultValue = slice.call(arguments);
                    let that = this;
                    return function() {
                        return that.apply(this, defaultValue.concat(slice.call(arguments)))
                    }
                }
    
                Array.prototype.sortDescending = Array.prototype.sort.curry((a, b) => b - a);
                let data = [1, 5, 2, 3, 7, 4];
                console.log(data.sortDescending())
            </script>
        </body>
    
    </html>

     4、函数柯里化 存钱的例子

    <!DOCTYPE html>
    <html lang="zh">
    
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <meta http-equiv="X-UA-Compatible" content="ie=edge" />
            <title>函数柯里化 存钱例子</title>
    
        </head>
    
        <body>
    
            <script type="text/javascript">
                function kelihua(fn) {
                    let arr = [];
    
                    function res() {
                        if(arguments.length == 0) {
                            return fn.apply(null, arr)
                        } else {
                            arr.push(...arguments)
                        }
                    }
                    return res;
                }
                let countMoney = kelihua(function() {
                    let s = 0
                    for(let i = 0; i < arguments.length; i++) {
                        s += arguments[i]
                    }
                    return s;
                })
    
                // 2018-01-01 存了1毛钱
    
                countMoney(1)
    
                // 2018-01-02 存了2毛钱
    
                countMoney(2)
    
                // 2018-01-03 存了3毛钱
    
                countMoney(3)
    
                // 2018-01-04 存了4毛钱
    
                countMoney(4)
    
                // 统计这笔巨额存款 输出结果为 10
    
                console.log(countMoney())
            </script>
        </body>
    
    </html>
  • 相关阅读:
    bug的约束
    bug管理规范
    FTP小教程
    测试需求,产品需求,项目需求
    如何理解客户需求、市场需求、产品需求、业务需求、特性、功能需求 ?(转)
    什么是测试需求?(转)
    绑定与非绑定,反射,内置方法
    面向对象之组合、封装、多态、property装饰器
    面向对象之继承与派生,属性查找
    面向对象基础
  • 原文地址:https://www.cnblogs.com/mengfangui/p/9765050.html
Copyright © 2011-2022 走看看