So, it depends on what you consider more functional, but here is the version without "var"
def myProcessorFunctional(items: List[Int]) {
def myProcessorHelper(items: List[Int], numProcessed: Int) {
if (numProcessed < items.size) {
val processedSoFar = items.size - numProcessed
myProcessorHelper(items,
numProcessed + processNextBlockOfItems(items, processedSoFar))
}
}
myProcessorHelper(items, 0)
}
(making it an Ints list for simplicity only, it would be easy to get it to work with a shared list)
, , - , .
, , processNextBlockOfItems , . , ( ) . , , processNextBlockOfItems...
:
- , , processNextBlockOfItems , , , ( List, , ).
- :
def myProcessorMoreFunctional(items: List[Int]) {
if (!items.isEmpty) {
myProcessorMoreFunctional(processNextBlockOfItems(items))
}
}