打赏

函数柯里化

一、柯里化

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

二、函数柯里化例子

<!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>

 

添加柯里化应用的例子:https://www.jianshu.com/p/962368760069

posted @ 2018-10-23 14:18  孟繁贵  阅读(372)  评论(0编辑  收藏  举报
TOP