編寫一個可任意調用次數的 curried javascript 函數,該函數在最後一次函數調用時返回一個值 (Writing a curried javascript function that can be called an arbitrary number of times that returns a value on the very last function call)


問題描述

編寫一個可任意調用次數的 curried javascript 函數,該函數在最後一次函數調用時返回一個值 (Writing a curried javascript function that can be called an arbitrary number of times that returns a value on the very last function call)

我目前正在處理我個人時間的一個編程問題,要求我製作一個可以以這種方式調用的 javascript 函數。

add(1) // 1
add(1)(2) // 3
add(1)(2)(3); // 6
add(1)(2)(3)(4); // 10
add(1)(2)(3)(4)(5); // 15

我無法弄清楚如何使其在最後一次調用時返回一個值。

例如,為了使 add(1)(2) 工作,然後 add(1) 必須返回一個函數,但是根據指令 add(1) 自己調用時會返回 1.

I' m 假設您可以克服這個問題的一種方法是弄清楚 add 函數連續調用了多少次,但我想不出一種方法來實現這一點。有沒有人有任何提示可以為我指明正確的方向?

我已經閱讀了這兩篇文章(1, 2) 關於函數柯里化,我理解它們,但我不確定在處理可變數量的參數時如何進行柯里化。

com/2015/01/14/gettin‑freaky‑functional‑wcurried‑javascript/" rel="nofollow">2) 關於函數柯里化,我理解它們,但我不知道什麼時候做柯里化處理可變數量的參數。

com/2015/01/14/gettin‑freaky‑functional‑wcurried‑javascript/" rel="nofollow">2) 關於函數柯里化,我理解它們,但我不知道什麼時候做柯里化處理可變數量的參數。


參考解法

方法 1:

It is impossible to curry a variadic function with an unknown number of arguments.

Where add is a variadic function, you could do something like

var add5 = curryN(add, 5);
add5(1)(2)(3)(4)(5); //=> 15

var add3 = curryN(add, 3);
add3(1)(2)(3); //=> 6

There's simply no avoiding this tho because a curried function will continue to return a function until the last argument is received, at which point the computation is run.


The only other option is to create some way to "short‑circuit" the arguments and notify the function that the arguments are done being sent. That would require something like

var xadd = curryUntilUndefined(add);
xadd(1)(2)(3)(4)(5)(undefined); //=> 15

Here, the undefined signals the end of the variadic arguments. I don't really recommend this tho because of the other problems it can create for you. Not to mention, it's not particularly nice to look at.

方法 2:

It is not impossible, use valueOf().

function add(initNum) {
    var sum = initNum;
    var callback = function (num) {
        sum += num;
        return callback;
    };
    callback.valueOf = function () {
        return sum;
    };
    return callback;
};
console.log(add(1)(2)==3);            //true
console.log(add(1)(1)+1);             //3
console.log(add(1)(2)(3).valueOf());  //6

(by m0meniMulanepascarello)

參考文件

  1. Writing a curried javascript function that can be called an arbitrary number of times that returns a value on the very last function call (CC BY‑SA 2.5/3.0/4.0)

#functional-programming #javascript #currying






相關問題

有沒有辦法在 C 中進行柯里化? (Is there a way to do currying in C?)

Scala 的產量是多少? (What is Scala's yield?)

功能性 javascript 和網絡瀏覽器 javascript 版本 (Functional javascript and web browser javascript versions)

從元組列表中獲取唯一路徑 (Getting Unique Paths from list of tuple)

用函數作為參數定義函數 (Defining a function with a function as an argument)

如何使用函數 VB.NET 插入數據庫? (How to use Function VB.NET Insert To Database?)

Python中列表的模式匹配 (Pattern matching of lists in Python)

如何在haskell中顯示派生樹? (how can i show a derivation tree in haskell?)

編寫一個可任意調用次數的 curried javascript 函數,該函數在最後一次函數調用時返回一個值 (Writing a curried javascript function that can be called an arbitrary number of times that returns a value on the very last function call)

我應該總是給我的函數一個返回值嗎? (Should I always give a return value to my function?)

如何在 JavaScript 中實現動態回調參數 (How can I achieve dynamic callback arguments in JavaScript)

Haskell 是否有一個函數可以創建將函數應用於列表的每個變體 (Haskell Is there a function for creating every variation of applying a function to a list)







留言討論