[swift-evolution] else clause for loops like that in Python

Kametrixom Tikara kametrixom at icloud.com
Wed Dec 9 02:07:50 CST 2015


Someone asked such a question on SO recently, I found out if one doesn't want to evaluate the whole sequence, functional style doesn't work: http://stackoverflow.com/a/34032744/3443689

> On 09 Dec 2015, at 02:21, Donnacha Oisín Kidney via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Be careful with lazy.filter.first! It evaluates the whole sequence, usually.
> 
> http://swiftstub.com/122568159/
> 
>>> On 9 Dec 2015, at 01:13, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> What about searching for something in an array? The not found code would go in the nobreak clause.
>> 
>> For an array, I would almost always use `if let` with `lazy.filter.first` or `indexOf`. The if branch is “found”, else is “not found”.
>> 
>> If that wasn’t suitable for some reason, I’d probably put the search loop like that in its own function or method and return early from inside the loop. If you reach the code after the loop, the search has failed.
>> 
>> I’m not saying it’s a useless feature, just that *I* can’t imagine using it.
>> 
>> -- 
>> Brent Royal-Gordon
>> Architechies
>> 
>> _______________________________________________
>> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151209/abe97e8f/attachment.html>


More information about the swift-evolution mailing list