0

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
6
  • 1
    as you can see 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 function sumColumn(lineCount) and it goes right into callback (lineCount->callback) , not(lineCount->lineNumber) Commented Aug 12, 2018 at 17:15
  • 1
    What does var itemSublist(current.getSublistValue)('item') mean? Commented Aug 12, 2018 at 17:16
  • I had my example wrong, I did call it that way. itemSublist(current.getSublistValue)('item') is just created a partial application of the function. They are just values necessary to execute the 3rd party API getSublistValue. There could be other "sublist" tables is why it is useful to have it curried like this Commented Aug 12, 2018 at 17:20
  • 1
    itemSublist('quantity') Returns a function right? and quantityColumn(5) // 2 work too? Commented Aug 12, 2018 at 17:23
  • 1
    please check out my solution Commented Aug 12, 2018 at 17:36

1 Answer 1

1
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)
}
sumColumn(quantityColumn, lineCount) // callback is not a function

In this line callback(lineNumber) + sumColumn(callback(lineNumber -1), lineNumber -1) You are calling the function callback (with arguments lineNumber-1) what you want is to pass it along

Sign up to request clarification or add additional context in comments.

1 Comment

Ah I see, I just need to pass the callback itself into the recursive call. Thanks @Anandu! I tested in environment and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.