A good way to look back arbitrary number of items in the Array.Fold in F#

Posted by tk on Stack Overflow See other posts from Stack Overflow or by tk
Published on 2010-05-30T05:22:15Z Indexed on 2010/05/30 5:32 UTC
Read the original article Hit count: 185

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about F#

Related posts about functional-programming