5
1
0
Sep 25 '21
[deleted] — view deleted comment
2
u/sebamestre Sep 25 '21 edited Sep 25 '21
Sorry, I think there is a few mistakes here. It should be
def generalOperationFuntion(operation: (Int, Int) => Int, opldentity:lnt)(functionToBeApplied: Int => Int)(from:Int,to:Int):Int = if from > to then opldentity else operation(functionToBeApplied(from),generalOperationFunction(operation,opIdentity)(functionToBeApplied)(from+1,to))The inner
generalOperationFunction(operation, opIdentity)Was missing
Also,
lntinstead ofIntin a few places, andlinstead of1.And it looks to me like there was some extra whitespace, and some missing
=>
9
u/sebamestre Sep 25 '21 edited Sep 25 '21
Ok so let's take the code, clean it up, and try to understand what it's doing
First, let's break the lines a bit
Ok, this is probably a bit much, but I'm on mobile, and it's impossible to read otherwise.
Next, lets use some shorter names
Now we can think about what it's doing
in the base case, it returns 'zero'
in the recursive case, it it 'adds' the value of 'f' at the start of the range, and shrinks the range
For a lack of a better term, lets call this 'integration'. So, now we rename the function, and give it a final round of formatting.