[swift-evolution] [Draft]: Introducing a striding(by:) method on 3.0 ranges

Nate Cook natecook at gmail.com
Mon Apr 11 18:57:53 CDT 2016


> On Apr 11, 2016, at 6:49 PM, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
> 
> on Mon Apr 11 2016, Ross O'Brien <swift-evolution at swift.org> wrote:
> 
>> I think I'd like to +1 a 'for x in loop(from: while: next:)'. (Possibly
>> 'iterate' rather than 'loop'?)
> 
> Maybe 'iterations'.  It should be a noun, I think.

There's a proposal awaiting review right now that would add an iterate function and takeWhile sequence method, so these could be written as:

for x in iterate(0.1, apply: { $0 + 2 }).takeWhile({ $0 < 10 }) {
    // ...
}

and

for view in iterate(startingSubview, apply: { $0.superview }).takeWhile({ $0 != nil }) {
    // ...
}

If the iterate function were overloaded with a while argument, this would be exactly what we're discussing. Perhaps this proposal should be a jumping off point?

https://github.com/apple/swift-evolution/blob/master/proposals/0045-scan-takewhile-dropwhile.md <https://github.com/apple/swift-evolution/blob/master/proposals/0045-scan-takewhile-dropwhile.md>

Nate


>> I've not missed the C-style for-loop so I've not argued to keep it, but recently
>> I was refactoring a function which started with a UIView and iterated up the
>> hierarchy through the superview property, and it occurred to me recently that
>> neither stride nor sequences/generators handle recursive iterations well.
>> 
>> So, I imagine that would look like this:
>> for view in loop(from: startingSubview, while: { $0 != nil }, next: { $0 =
>> $0.superview })
>> 
>> On Mon, Apr 11, 2016 at 11:31 PM, Dave Abrahams via swift-evolution
>> <swift-evolution at swift.org> wrote:
>> 
>>    on Mon Apr 11 2016, Michel Fortin <michel.fortin-AT-michelf.ca> wrote:
>> 
>>> Le 11 avr. 2016 à 14:36, Dave Abrahams
>>    <dabrahams at apple.com> a écrit :
>>> 
>>>> 3. The fact that we're migrating C-style for loops to
>>>> uses of stride, as noted in https://github.com/apple/swift/pull/2125,
>>>> has convinced me that, sadly, we may need an answer that doesn't
>>>> involve ranges. But maybe something like
>>>> 
>>>> for x in loop(from: 0.1, while: { $0 < 10 }, next: { $0 + .2 })
>>>> 
>>>> is sufficient for this purpose.
>>> 
>>> Please add that.
>> 
>>    Please write a proposal and ideally, submit a patch :-).
>> 
>>    Seriously, if this is something you believe in, we could really use the
>>    help.
>> 
>>> First, it would relieve `stride` from some of the pressure of
>>> excelling at replacing existing C-style for loops. But it would also
>>> become pretty easy to write custom sequences like this one:
>>> 
>>> func uniform(start: Double, end: Double, numberOfSteps totalSteps: Int) ->
>>    Sequence {
>>> var currentStep = 0
>>> return loop(from: start, while: { _ in
>>> currentStep < totalSteps
>>> }, next: { _ in
>>> currentStep += 1
>>> return start * (Double(totalSteps-currentStep) / Double(totalSteps)) +
>>> end * (Double(currentStep) / Double(totalSteps))
>>> })
>>> }
>> 
>>    Aside from the fact that you can't return Sequence, this seems like a
>>    much better way to do that in Swift 3.0:
>> 
>>    func uniform(
>>    start: Double, end: Double, numberOfSteps totalSteps: Int
>>    ) -> LazyMapRandomAccessCollection<CountableRange<Int>, Double> {
>>    return (0..<totalSteps).lazy.map {
>>    start * (Double(totalSteps-$0) / Double(totalSteps)) +
>>    end * (Double($0) / Double(totalSteps))
>>    }
>>    }
>> 
>>    --
>>    Dave
>> 
>>    _______________________________________________
>>    swift-evolution mailing list
>>    swift-evolution at swift.org
>>    https://lists.swift.org/mailman/listinfo/swift-evolution
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> -- 
> Dave
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160411/94b4b98c/attachment.html>


More information about the swift-evolution mailing list