[英]how to write a sum function with infinite number of arguments, using currying in javascript?
[英]How to implement infinite currying sum values with a callback function with result in javascript?
我有一個測試,我需要在這種情況下實現一個稱為 sum 的高階 function ,它滿足以下要求:
sum(result => {console.log("-> ", result)});// -> prints: -> 0
sum(1)(result => {console.log("-> ", result)});// -> prints: -> 1
sum(1)(2)(result => {console.log("-> ", result)});// -> prints: -> 3
sum(1)(2)(4)(result => {console.log("-> ", result)});// -> prints: -> 7
我做了一個 function 以通過遞歸獲得無限柯里化,但我對回調函數有點迷失了。
let sum = a => !a ? 0 : b => b ? sum(a+b) : a
console.log(sum()) -> 0
console.log(sum(1)()) -> 1
console.log(sum(1)(2)()) -> 3
console.log(sum(1)(2)(4)()) -> 7
使用閉包來累積總和。 當檢測到參數是 function 時,內部 function 將使用總和調用回調。 否則它將在調整總和后返回:
function sum(arg) { let total = 0; function inner(arg) { if (typeof arg === "function") { arg(total); } else { total += arg; return inner; } } return inner(arg); } sum(result => {console.log("-> ", result)});// -> prints: -> 0 sum(1)(result => {console.log("-> ", result)});// -> prints: -> 1 sum(1)(2)(result => {console.log("-> ", result)});// -> prints: -> 3 sum(1)(2)(4)(result => {console.log("-> ", result)});// -> prints: -> 7
如果不打算讓 function 維護 state,那么您可以使用this
參數傳遞總數:
"use strict"; function sum(arg) { function inner(arg) { if (typeof arg === "function") { arg(+this); } else { return inner.bind(this + arg); } } return inner.call(0, arg); } sum(result => {console.log("-> ", result)});// -> prints: -> 0 sum(1)(result => {console.log("-> ", result)});// -> prints: -> 1 sum(1)(2)(result => {console.log("-> ", result)});// -> prints: -> 3 sum(1)(2)(4)(result => {console.log("-> ", result)});// -> prints: -> 7 const foo = sum(0); foo(100)(console.log); // 100 foo(1)(console.log); // 1
這里建議使用嚴格模式,但它也可以在草率模式下工作。 在這種情況下, this
參數將被裝箱並再次拆箱。
為簡單起見,您可以有一個輔助程序 function 接受兩個 arguments。 第一個參數是一個數字。 第二個參數將是一個數字或 function 並且根據類型它會做不同的事情:
通過這種方式,您可以設置一個連續的柯里化執行鏈,當 function 作為參數傳遞時終止。
實際的sum()
function 可以使用輔助函數並通過將第一個參數設置為0
(加法運算的中性元素)來初始化鏈的開始。
const helper = total => arg => { if (typeof arg === "function") arg(total); else if (typeof arg === "number") return helper(total + arg); else throw new Error("invalid argument"); } const sum = x => helper(0)(x); sum(result => {console.log("-> ", result)});// -> prints: -> 0 sum(1)(result => {console.log("-> ", result)});// -> prints: -> 1 sum(1)(2)(result => {console.log("-> ", result)});// -> prints: -> 3 sum(1)(2)(4)(result => {console.log("-> ", result)});// -> prints: -> 7
為簡單起見, helper()
function 是咖喱的。
這是一個更簡潔的版本:
sum()
的定義已減少。 const helper = total => arg => (typeof arg === "function")? arg(total): helper(total + arg); const sum = helper(0); sum(result => {console.log("-> ", result)});// -> prints: -> 0 sum(1)(result => {console.log("-> ", result)});// -> prints: -> 1 sum(1)(2)(result => {console.log("-> ", result)});// -> prints: -> 3 sum(1)(2)(4)(result => {console.log("-> ", result)});// -> prints: -> 7
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.