A good way to look back arbitrary number of items in the Array.Fold in F#
- by tk
In the folder function of the Array.Fold operation, i want to look back arbitrary number of items.
The only way i can figure out is like this.
let aFolder (dataArray, curIdx, result) (dataItem) =
let N = numItemsToLookBack(result, dataItem)
let recentNitems = dataArray.[curIdx - n .. curIdx - 1]
let result = aCalc(result, dataItem, recentNitems )
dataArray, curIdx + 1, result
myDataArray | Array.fold aFolder (myDataArray, 0, initResult)
As you can see, I passed the whole dataArray and index to the folder function to get the "recentNitems".
But this way allows the folder function to access not only preceding data, but also the following data.
Is there a better (or more functional) way?