Suppose we have a pair of input arrays, or a list of (key, value) tuples if you prefer. What's an elegant and performant way to combine values that have indices falling in a certain interval? For example, if the interval (or 'bin') size is 10 then the values of all indices from
0 < x <= 10 would be combined, as would the values of indices from
10 < x <= 20 and so on. I want:
let interval = 10 let index = [| 6; 12; 18; 24 |] let value = [| a; b; c; d |] result = [| a; b + c; d |]
The crudest way to do this would be to use a whole lot of if, else if statements (the index range has a defined upper limit). I got close with
for i = 0 to index.Length do result.[Math.Floor(index.[i]/10] += value.[Math.Floor(index.[i]/10]
but this is doing
0 <= x < 10, not
0 < x <= 10.
I also tried assuming the indices are ordered and evenly spaced, with
for i = 1 : ( index.Length - 1 ) / valuesPerBin valueRange = ((i-1)*valuesPerBin + 1) : i*valuesPerBin ) result(i) = sum(value(valueRange))
which is nice but obviously breaks if there is a non integer number of values per bin.
What's the best way of doing this in F#? Is there a name or an existing function for what I'm trying to do?