I have these two recursive functions. The top one works but the when I try to make the quantityColumn function as a callback to second function, I get an error that callback is not a function. Any ideas what I am doing wrong?
var sumColumn = function(lineNumber) {
return lineNumber === 0
? quantityColumn(0)
: quantityColumn(lineNumber) + sumColumn(lineNumber -1)
}
sumColumn(lineCount) // returns 9
var sumColumn = function(callback, lineNumber) {
return lineNumber === 0
? callback(0)
: callback(lineNumber) + sumColumn(callback(lineNumber -1), lineNumber -1)
}
sumColumn(quantityColumn, lineCount) // callback is not a function
In case more code is required. Here is what the quantityColumn function is. Also of note, current.getSublistValue is a 3rd party API (NetSuite) that basically just returns the of the intersections of a line/row on a table.
var columnValue = R.curry(function(getSublistValue, sublistId, column, i) {
return getSublistValue({
sublistId: sublistId,
fieldId: column,
line: i
})
}
)
var itemSublist(current.getSublistValue)('item')
var quantityColumn = itemSublist('quantity')
var lineCount = current.getLineCount('item') - 1 // first index is 0
quantityColumn(5) // 2
quantityColumn(4) // 1
quantityColumn(3) ...
var sumColumn = function(lineNumber) {
return lineNumber === 0
? quantityColumn(0)
: quantityColumn(lineNumber) + sumColumn(lineNumber -1)
}
sumColumn(lineCount) // returns 9
var sumColumn = function(callback, lineNumber)expect parameter 1 to be ` callback` (and you need two arguments to call the function properly).You are calling the functionsumColumn(lineCount)and it goes right intocallback(lineCount->callback) , not(lineCount->lineNumber)var itemSublist(current.getSublistValue)('item')mean?itemSublist(current.getSublistValue)('item')is just created apartial applicationof the function. They are just values necessary to execute the 3rd party APIgetSublistValue. There could be other "sublist" tables is why it is useful to have it curried like thisitemSublist('quantity')Returns afunctionright? andquantityColumn(5) // 2work too?