[swift-evolution] [swift-evolution-announce] [Review] SE-0045: Add scan, prefix(while:), drop(while:), and iterate to the stdlib
Kevin Ballard
kevin at sb.org
Fri Apr 29 16:56:29 CDT 2016
On Thu, Apr 28, 2016, at 12:30 PM, Mark Lacey via swift-evolution wrote:
> I haven’t read through the complete proposal in detail, but regarding
> the ‘scan’ operation I would like to point out that the definition
> given in the example matches the semantics of what is usually called
> ‘prescan' or 'exclusive scan', whereas ‘scan’ (aka ‘inclusive scan’ or
> ‘prefix sum’) would not include the identity element, and each
> position of the result would include applying the operator to the
> elements up to and including the element in the same position of the
> source array, e.g.:
>
> (1..<6).scan(combine: +) // [1, 3, 6, 10, 15, 21]
>
> Sources:
> https://www.cs.cmu.edu/~guyb/papers/Ble93.pdf
> https://en.wikipedia.org/wiki/Prefix_sum
Interesting, I was not aware of the distinction here. The `scan` method
as proposed here matches the behavior of Haskell's `scanl` method
(http://hackage.haskell.org/package/base-4.8.2.0/docs/Prelude.html#v:scanl)
.
I'm in favor of keeping the behavior as-is, because the "inclusive scan"
behavior can be recovered trivially by calling `.dropFirst()` on the
resulting sequence, whereas recovering prescan from inclusive scan is
not so trivial.
-Kevin Ballard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160429/e35a5b81/attachment.html>
More information about the swift-evolution
mailing list