Link to Snippet

Seq.reduceBallanced function

The function has the same type as Seq.reduce. Instead of reducing elements from the left to the right, it splits the input into two halves, reduces each half separately and then aggregates the results using the given function. This means that the values are aggregated into a ballanced tree, which can save stack space.

moduleSeq=/// Reduces input sequence by splitting it into two halves,/// reducing each half separately and then aggregating the results /// using the given function. This means that the values are /// aggregated into a ballanced tree, which can save stack space.letreduceBallancedfinput=// Convert the input to an array (must be finite)letarr=input|>Array.ofSeqletrecreducest=ifs+1>=tthenarr.[s]
else// Aggregate two halves of the input separatelyletm= (s+t) /2f (reducesm) (reducemt)
reduce0arr.Length

Reduces input sequence by splitting it into two halves, reducing each half separately and then aggregating the results using the given function. This means that the values are aggregated into a ballanced tree, which can save stack space.