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>
  • 相关阅读:
    349、两个数组的交集 | JS集合
    JS集合Set
    JS里的队列和链表
    使用链表指针获取JSON的节点值
    141、环形链表 | JS-链表
    83、删除排序链表中的重复元素 | JS-链表
    2、两数相加 | JS-链表
    事件循环与任务队列
    933、最近的请求次数 | JS-队列
    栈JS实现
  • 原文地址:https://www.cnblogs.com/mengfangui/p/9765050.html
Copyright © 2011-2022 走看看