Forum Discussion
Dicken
2 months agoPost Prodigy
Rolling accumulation
Hi, I have been trying various methods of a rolling accumultion or bucketiing; so buckets of 3 = {1..10} = { {1}, {1,2}, { 1,2,3}, { 2,3,4} , {3,4,5}, {4,5,6}, {5,,6.7}, { 6,7,8} , { 7,8,9}, ...
- 2 months ago
Dicken please try with:
let alist = {1 .. 15}, window = 3, pad = List.Repeat({0}, window - 1), // {0, 0} padded = List.Combine({pad, alist}), // {0,0,1,2,...,15} positions = {0 .. List.Count(alist) - 1}, // 0-based indices for final result buckets = List.Transform( positions, each List.Range(padded, _, window) // always take exactly 'window' items ) in buckets - 2 months ago
Dicken Another option could be with List.Accumulate:
let alist = {1 .. 15}, window = 3, buckets = List.Accumulate( {1 .. List.Count(alist)}, {}, (state, current) => state & { if current <= window then List.Range(alist, 0, current) else List.Range(alist, current - window, window) } ) in buckets
pcoley
2 months agoSuper User
Dicken also you can try with List.Generate:
let
alist = {1 .. 15},
windowSize = 3,
result = List.Generate(
() => [idx = 0, win = {}],
each [idx] < List.Count(alist),
each [
idx = [idx] + 1,
win = if [idx] < windowSize
then List.Range(alist, 0, [idx] + 1)
else List.Range(alist, [idx] - windowSize + 1, windowSize)
],
each [win]
)
in
resultDicken
2 months agoPost Prodigy
Thaanks all, that's enough to be going on with i shall go through them all.