-
Notifications
You must be signed in to change notification settings - Fork 441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider adding scan #25
Comments
@fwgreen could you elaborate on the functionality |
Its a let nums = [0,1,2,3,4,5].scan(+)
print(nums) //[0, 1, 3, 6, 10, 15]
let chars = ["0","1","2","3","4","5"].scan(+)
print(chars) //["0", "01", "012", "0123", "01234", "012345"]
let range = (1..<5).scan(0, +)
print(range) //[0, 1, 3, 6, 10] I use an extension that I've cobbled together from Slashdot and other places, but it would be good if there was a correct (and performant) version available. extension Sequence {
func scan(_ combine: (Element, Element) throws -> Element) rethrows -> [Element] {
var iterator = makeIterator()
guard let initial = iterator.next() else {
return []
}
var firstIteration = true
var accumulator = initial
return try map { element in
if firstIteration == true {
firstIteration = false
return initial
} else {
accumulator = try combine(accumulator, element)
return accumulator
}
}
}
func scan(_ initial: Element, _ combine: (Element, Element) throws -> Element) rethrows -> [Element] {
var accumulator = initial
let sequence = [initial] + self
return try sequence.map { element in
accumulator = try combine(accumulator, element)
return accumulator
}
}
} |
+1 on the idea. I'd rather see the latter function with the following signature, allowing for a different result type and aligning with extension Sequence {
func scan<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> [Result]
} Also a |
I think The term scan is established e.g. in Haskell, but in Haskell what we call reduce is known by a different name too: it's a fold. |
As someone unfamiliar with the prior art, |
Thanks, @fwgreen — this looks like a good addition, especially since it's easy to get the implementation wrong. 👏👏 Let's go with |
I remember
scan(_:combine:)
not making the cut on the grounds of offering low utility. Is it possible for it to find a place here?The text was updated successfully, but these errors were encountered: