Add accumulate/scan to the standard library

scan or accumulate is the operation that takes an array, and returns an array where each element is the sum (or some other operation) of that element, and all the previous elements. It could be spelled similar to reduce right now.

[1, 2, 3, 4, 5].scan(+) -> [1, 3, 6, 10, 15]

i find myself reaching for this a lot, and am always surprised it’s not in the standard library. a lot of other people reach for it too. does this clear the bar for inclusion in the standard library?

4 Likes

I have always called this operation a “prefix sum” (or more generally a prefix reduce), FWIW.

scan was already reviewed and rejected: swift-evolution/0045-scan-takewhile-dropwhile.md at master · apple/swift-evolution · GitHub. Do you think you can provide sufficient motivation to overcome the rationale for rejection?

3 Likes