[swift-evolution] For... else

Andrew Bennett cacoyi at gmail.com
Mon Jan 25 22:14:23 CST 2016


This may do what you want:

    let values: [Int] = [1,2,3,4]

    if let sequence = nonEmptySequence(values) {

        for element in sequence {

            print("element: \(element)")

        }

    }

    else {

        print("empty!")

    }


It's also more flexible, you can do work after the for loop (that work
won't execute in the else), you can also use PeekSequence directly to get
the first value. This is handy for something like an average (reduce) where
you don't necessarily have a zero value.


The implementation can be found here:
https://github.com/therealbnut/PeekSequence


On Tue, Jan 26, 2016 at 12:59 AM, Matthew Johnson via swift-evolution <
swift-evolution at swift.org> wrote:

>
>
> Sent from my iPad
>
> On Jan 25, 2016, at 3:41 AM, Howard Lovatt via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> It would be easy enough to add:
>
>
>    - func forEach(
>    -     @noescape elements body: (Element) throws -> Void,
>    -     @noescape otherwise otherwise: () throws -> Void
>    - ) rethrows
>
>
> to the standard library or write it yourself.
>
>
> Not unless / until we get the ability for a noescape closure to influence
> control flow (break, return, etc).  Even then this is clunkier than adding
> else to the language would be.
>
>
> On 23 Jan 2016, at 12:59 PM, Félix Cloutier via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> I frequently need something like that and I'd like to have it in Swift too.
>
> Félix
>
> Le 22 janv. 2016 à 13:22:45, Ben Langmuir via swift-evolution <
> swift-evolution at swift.org> a écrit :
>
> Previous discussion here:
>
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001110.html
>
>
> On Jan 22, 2016, at 8:22 AM, Lee M via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> This has likely been discussed already, but I haven't been able to find it
> anywhere.
> Example:
>
>    let items: Int = []
>
>    for item in items {
>        print(item)
>    }
>    else {
>        print("No items")
>    }
> _______________________________________________
> 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
>
>
> _______________________________________________
> 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
>
>
> _______________________________________________
> 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/20160126/8a3819c4/attachment.html>


More information about the swift-evolution mailing list