Re: [swift-users] Compact iteration of optional collection?

2016-07-29 Thread Quinn "The Eskimo!" via swift-users

On 28 Jul 2016, at 22:55, Rick Mann via swift-users  
wrote:

> I often call methods that return an optional collection.

Why do these methods return an optional collection rather an empty collection? 
Back in the day Cocoa code used to work that way because constructing empty 
collections was expensive.  These days I avoid optionality unless that 
optionality is signalling something relevant.  So I never write code like this:

if let container = someOptionalContainer {
for item in container {
[do stuff with item]
}
}

it’s always this:

for item in container {
[do stuff with item]
}

or this:

if let container = someOptionalContainer {
for item in container {
[do stuff with item]
}
} else {
[do other stuff]
}

Share and Enjoy
--
Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Compact iteration of optional collection?

2016-07-29 Thread Kerry Hazelgren via swift-users
Like Rick, I had also wondered about a simple way to do this. Perhaps this is a 
question for swift-evolution, but wouldn’t it be desirable if Swift supported:

 for item in someOptionalContainer?
 {
 }

which seems more natural and intuitive that the alternatives that have been 
suggested.

Kerry

> On Jul 28, 2016, at 5:18 PM, Zhao Xin via swift-users  
> wrote:
> 
> You can try container?.forEach(), like
> 
> let bb:[String:Int]? = ["aa":1, "bb":2, "cc":3]
> bb?.forEach { print($0) }
> /* 
> ("aa", 1)
> ("bb", 2)
> ("cc", 3)
> */
> 
> Zhaoxin
> 
> On Fri, Jul 29, 2016 at 6:14 AM, Saagar Jha via swift-users 
> > wrote:
> The nil check and creating an empty array have very similar performance, in 
> my naïve testing. 
> 
> Saagar Jha
> 
> 
> 
>> On Jul 28, 2016, at 14:59, Jacob Bandes-Storch via swift-users 
>> > wrote:
>> 
>> You should test it out — I'd guess there's a good chance it gets optimized 
>> out.
>> On Thu, Jul 28, 2016 at 2:58 PM Rick Mann > > wrote:
>> Yeah, I suppose that works. Feels a bit clunky, like the language lacks 
>> specific support for this (in that it provides specific support for so many 
>> other common constructs). But I guess I can make do with that.
>> 
>> I suppose there's a bit of a performance hit, in that constructing an empty 
>> array and iterating over it is more expensive than a simple nil check, but 
>> that's unlikely to cause issues in practice.
>> 
>> Thanks.
>> 
>> > On Jul 28, 2016, at 14:56 , Jacob Bandes-Storch > > > wrote:
>> >
>> > How about "for item in someOptionalContainer ?? []"  ?
>> >
>> > On Thu, Jul 28, 2016 at 2:55 PM, Rick Mann via swift-users 
>> > > wrote:
>> > I often call methods that return an optional collection. I then iterate 
>> > over it. The problem is, it's a bit cumbersome to write:
>> >
>> >  if let container = someOptionalContainer
>> > {
>> > for item in container
>> > {
>> > }
>> > }
>> >
>> > I wish I could just write
>> >
>> > for item in someOptionalContainer
>> > {
>> > }
>> >
>> > such that if the optional is nil, it just skips the iteration altogether.
>> >
>> > Is there a syntax for that (especially in Swift 3)?
>> >
>> >
>> > --
>> > Rick Mann
>> > rm...@latencyzero.com 
>> >
>> >
>> > ___
>> > swift-users mailing list
>> > swift-users@swift.org 
>> > https://lists.swift.org/mailman/listinfo/swift-users 
>> > 
>> >
>> 
>> 
>> --
>> Rick Mann
>> rm...@latencyzero.com 
>> 
>> 
>> ___
>> swift-users mailing list
>> swift-users@swift.org 
>> https://lists.swift.org/mailman/listinfo/swift-users 
>> 
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org 
> https://lists.swift.org/mailman/listinfo/swift-users 
> 
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Compact iteration of optional collection?

2016-07-28 Thread Zhao Xin via swift-users
You can try container?.forEach(), like

let bb:[String:Int]? = ["aa":1, "bb":2, "cc":3]

bb?.forEach { print($0) }

/*

("aa", 1)

("bb", 2)

("cc", 3)

*/


Zhaoxin

On Fri, Jul 29, 2016 at 6:14 AM, Saagar Jha via swift-users <
swift-users@swift.org> wrote:

> The nil check and creating an empty array have very similar performance,
> in my naïve testing.
>
> Saagar Jha
>
>
>
> On Jul 28, 2016, at 14:59, Jacob Bandes-Storch via swift-users <
> swift-users@swift.org> wrote:
>
> You should test it out — I'd guess there's a good chance it gets optimized
> out.
> On Thu, Jul 28, 2016 at 2:58 PM Rick Mann  wrote:
>
>> Yeah, I suppose that works. Feels a bit clunky, like the language lacks
>> specific support for this (in that it provides specific support for so many
>> other common constructs). But I guess I can make do with that.
>>
>> I suppose there's a bit of a performance hit, in that constructing an
>> empty array and iterating over it is more expensive than a simple nil
>> check, but that's unlikely to cause issues in practice.
>>
>> Thanks.
>>
>> > On Jul 28, 2016, at 14:56 , Jacob Bandes-Storch 
>> wrote:
>> >
>> > How about "for item in someOptionalContainer ?? []"  ?
>> >
>> > On Thu, Jul 28, 2016 at 2:55 PM, Rick Mann via swift-users <
>> swift-users@swift.org> wrote:
>> > I often call methods that return an optional collection. I then iterate
>> over it. The problem is, it's a bit cumbersome to write:
>> >
>> >  if let container = someOptionalContainer
>> > {
>> > for item in container
>> > {
>> > }
>> > }
>> >
>> > I wish I could just write
>> >
>> > for item in someOptionalContainer
>> > {
>> > }
>> >
>> > such that if the optional is nil, it just skips the iteration
>> altogether.
>> >
>> > Is there a syntax for that (especially in Swift 3)?
>> >
>> >
>> > --
>> > Rick Mann
>> > rm...@latencyzero.com
>> >
>> >
>> > ___
>> > swift-users mailing list
>> > swift-users@swift.org
>> > https://lists.swift.org/mailman/listinfo/swift-users
>> >
>>
>>
>> --
>> Rick Mann
>> rm...@latencyzero.com
>>
>>
>> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
>
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Compact iteration of optional collection?

2016-07-28 Thread Saagar Jha via swift-users
The nil check and creating an empty array have very similar performance, in my 
naïve testing. 

Saagar Jha



> On Jul 28, 2016, at 14:59, Jacob Bandes-Storch via swift-users 
>  wrote:
> 
> You should test it out — I'd guess there's a good chance it gets optimized 
> out.
> On Thu, Jul 28, 2016 at 2:58 PM Rick Mann  > wrote:
> Yeah, I suppose that works. Feels a bit clunky, like the language lacks 
> specific support for this (in that it provides specific support for so many 
> other common constructs). But I guess I can make do with that.
> 
> I suppose there's a bit of a performance hit, in that constructing an empty 
> array and iterating over it is more expensive than a simple nil check, but 
> that's unlikely to cause issues in practice.
> 
> Thanks.
> 
> > On Jul 28, 2016, at 14:56 , Jacob Bandes-Storch  > > wrote:
> >
> > How about "for item in someOptionalContainer ?? []"  ?
> >
> > On Thu, Jul 28, 2016 at 2:55 PM, Rick Mann via swift-users 
> > > wrote:
> > I often call methods that return an optional collection. I then iterate 
> > over it. The problem is, it's a bit cumbersome to write:
> >
> >  if let container = someOptionalContainer
> > {
> > for item in container
> > {
> > }
> > }
> >
> > I wish I could just write
> >
> > for item in someOptionalContainer
> > {
> > }
> >
> > such that if the optional is nil, it just skips the iteration altogether.
> >
> > Is there a syntax for that (especially in Swift 3)?
> >
> >
> > --
> > Rick Mann
> > rm...@latencyzero.com 
> >
> >
> > ___
> > swift-users mailing list
> > swift-users@swift.org 
> > https://lists.swift.org/mailman/listinfo/swift-users 
> > 
> >
> 
> 
> --
> Rick Mann
> rm...@latencyzero.com 
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Compact iteration of optional collection?

2016-07-28 Thread Jacob Bandes-Storch via swift-users
You should test it out — I'd guess there's a good chance it gets optimized
out.
On Thu, Jul 28, 2016 at 2:58 PM Rick Mann  wrote:

> Yeah, I suppose that works. Feels a bit clunky, like the language lacks
> specific support for this (in that it provides specific support for so many
> other common constructs). But I guess I can make do with that.
>
> I suppose there's a bit of a performance hit, in that constructing an
> empty array and iterating over it is more expensive than a simple nil
> check, but that's unlikely to cause issues in practice.
>
> Thanks.
>
> > On Jul 28, 2016, at 14:56 , Jacob Bandes-Storch 
> wrote:
> >
> > How about "for item in someOptionalContainer ?? []"  ?
> >
> > On Thu, Jul 28, 2016 at 2:55 PM, Rick Mann via swift-users <
> swift-users@swift.org> wrote:
> > I often call methods that return an optional collection. I then iterate
> over it. The problem is, it's a bit cumbersome to write:
> >
> >  if let container = someOptionalContainer
> > {
> > for item in container
> > {
> > }
> > }
> >
> > I wish I could just write
> >
> > for item in someOptionalContainer
> > {
> > }
> >
> > such that if the optional is nil, it just skips the iteration altogether.
> >
> > Is there a syntax for that (especially in Swift 3)?
> >
> >
> > --
> > Rick Mann
> > rm...@latencyzero.com
> >
> >
> > ___
> > swift-users mailing list
> > swift-users@swift.org
> > https://lists.swift.org/mailman/listinfo/swift-users
> >
>
>
> --
> Rick Mann
> rm...@latencyzero.com
>
>
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Compact iteration of optional collection?

2016-07-28 Thread Rick Mann via swift-users
Yeah, I suppose that works. Feels a bit clunky, like the language lacks 
specific support for this (in that it provides specific support for so many 
other common constructs). But I guess I can make do with that.

I suppose there's a bit of a performance hit, in that constructing an empty 
array and iterating over it is more expensive than a simple nil check, but 
that's unlikely to cause issues in practice.

Thanks.

> On Jul 28, 2016, at 14:56 , Jacob Bandes-Storch  wrote:
> 
> How about "for item in someOptionalContainer ?? []"  ?
> 
> On Thu, Jul 28, 2016 at 2:55 PM, Rick Mann via swift-users 
>  wrote:
> I often call methods that return an optional collection. I then iterate over 
> it. The problem is, it's a bit cumbersome to write:
> 
>  if let container = someOptionalContainer
> {
> for item in container
> {
> }
> }
> 
> I wish I could just write
> 
> for item in someOptionalContainer
> {
> }
> 
> such that if the optional is nil, it just skips the iteration altogether.
> 
> Is there a syntax for that (especially in Swift 3)?
> 
> 
> --
> Rick Mann
> rm...@latencyzero.com
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
> 


-- 
Rick Mann
rm...@latencyzero.com


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Compact iteration of optional collection?

2016-07-28 Thread Jacob Bandes-Storch via swift-users
How about "for item in someOptionalContainer ?? []"  ?

On Thu, Jul 28, 2016 at 2:55 PM, Rick Mann via swift-users <
swift-users@swift.org> wrote:

> I often call methods that return an optional collection. I then iterate
> over it. The problem is, it's a bit cumbersome to write:
>
>  if let container = someOptionalContainer
> {
> for item in container
> {
> }
> }
>
> I wish I could just write
>
> for item in someOptionalContainer
> {
> }
>
> such that if the optional is nil, it just skips the iteration altogether.
>
> Is there a syntax for that (especially in Swift 3)?
>
>
> --
> Rick Mann
> rm...@latencyzero.com
>
>
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users