Re: More elegance than a long if/else

2017-03-11 Thread Quincey Morris
On Mar 11, 2017, at 06:30 , Pascal Bourguignon  wrote:
> 
> My argument is that algebraic expressions are clearer than switches.

And it’s a *convincing* argument when you switch to a purely arithmetic problem 
(such as: how to add two numbers).

Seriously, though, I apologize if I let anyone think I failed to see the 
argument for — the advantages of — a concise numerical expression. Someone 
(Jeff) had already given that part of the answer. Yet there may well be 
multiple answers for which there is a valid argument.

Finally, before stepping out of this thread permanently, I’ll point out that 
although Jeff knew the (or, a) right answer, he inadvertently used the wrong 
operator. On top of that, there was a second correction to handle the edge case 
at upper end of the range, and the lower end edge case (if the battery level 
value is negative) still isn’t handled as the original code would. Finding an 
arithmetic solution to a logical problem is a non-trivial exercise, and “it can 
be argued that” for a reader of the code, decoding the resulting non-trivial 
arithmetic expression carries a non-trivial conceptual load.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: More elegance than a long if/else

2017-03-11 Thread Pascal Bourguignon

> On 11 Mar 2017, at 09:57, Quincey Morris 
>  wrote:
> 
> On Mar 10, 2017, at 17:35 , Pascal Bourguignon  > wrote:
>> 
>> this is much clearer in intent than return x+y.  So much clearer…
> 
> My argument is not that the original solution was clearer because longer, but 
> that it was longer because clearer.
> 


My argument is that algebraic expressions are clearer than switches.


-- 
__Pascal J. Bourguignon__



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: More elegance than a long if/else

2017-03-11 Thread Quincey Morris
On Mar 10, 2017, at 17:35 , Pascal Bourguignon  wrote:
> 
> this is much clearer in intent than return x+y.  So much clearer…

My argument is not that the original solution was clearer because longer, but 
that it was longer because clearer.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: More elegance than a long if/else

2017-03-10 Thread Pascal Bourguignon

> On 10 Mar 2017, at 23:32, Quincey Morris 
>  wrote:
> 
> On Mar 10, 2017, at 08:24 , Bryan Vines  wrote:
>> 
>> Would integer division work better than the modulus operator?
> 
> It would certainly work better in the sense that division is the right 
> operator and modulus is the wrong one!
> 
> Regarding the original question, I would add that there’s a decent argument 
> to be made that keeping a long series of cases is clearer in intent and 
> methodology than a concise but somewhat obscure calculation.


Of course.  

switch(x){
case 0: return y;
case 1: y++; return y;
case 2: y++; y++; return y;
case 3: y++; y++; y++ ; return y;
…
}

this is much clearer in intent than return x+y.  So much clearer…



-- 
__Pascal J. Bourguignon__



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: More elegance than a long if/else

2017-03-10 Thread Quincey Morris
On Mar 10, 2017, at 08:24 , Bryan Vines  wrote:
> 
> Would integer division work better than the modulus operator?

It would certainly work better in the sense that division is the right operator 
and modulus is the wrong one!

Regarding the original question, I would add that there’s a decent argument to 
be made that keeping a long series of cases is clearer in intent and 
methodology than a concise but somewhat obscure calculation. In that case, 
though, I would just set an imageName variable inside the “if” statement, and 
retrieve the UIImage afterwards. (That keeps the contents of the “if” to simple 
statements that the compiler can easily optimize, whereas creating the image in 
each case is a method call that blocks some optimizations.)

Also, a “switch” statement would be cleaner than an “if”, since it has less 
boilerplate, and this kind of range testing can be done in Swift, though the 
mechanism isn’t obvious till you see it:

> switch (batteryLevel)
> {
> case _ where batteryLevel >= 90: imageName = "10"
> case _ where batteryLevel >= 80: imageName = “9"
> …
> default: imageName = "0”
> }
> let image = UIImage (named: imageName)

My guess is that the Swift compiler can optimize code like this very nicely.

Finally, I’d point out that anyone who cared about numerical correctness might 
say that the entire solution is “wrong” because the cutoff points are in the 
wrong place. If you’re trying to indicate battery level in 10% increments, it 
seems more correct to represent 89 and 90 values both as 90%, rather than as 
90% and 100% respectively, as the code seems to suggest. But that’s a different 
issue.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: More elegance than a long if/else

2017-03-10 Thread Bryan Vines
Would integer division work better than the modulus operator?

batteryIcon.image = UIImage(named:"\(min(10, (Int(_myBatteryLevel) / 10) + 1))")

--
Bryan Vines

> On Mar 10, 2017, at 9:54 AM, Jeff Kelley  wrote:
> 
> I realized after sending that 100 won’t be correct, so you’ll need
> something like this:
> 
> batteryIcon.image = UIImage(named: "\(min(10, (_myBatteryLevel % 10) + 1))")
> 
> 
> Jeff Kelley
> 
> slauncha...@gmail.com | @SlaunchaMan  |
> jeffkelley.org


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: More elegance than a long if/else

2017-03-10 Thread Jeff Kelley
I realized after sending that 100 won’t be correct, so you’ll need
something like this:

batteryIcon.image = UIImage(named: "\(min(10, (_myBatteryLevel % 10) + 1))")


Jeff Kelley

slauncha...@gmail.com | @SlaunchaMan  |
jeffkelley.org

On Fri, Mar 10, 2017 at 10:50 AM, Eric E. Dolecki 
wrote:

> Thank you!
>
> On Fri, Mar 10, 2017 at 10:48 AM Jeff Kelley 
> wrote:
>
>> Something like this should work:
>>
>> batteryIcon.image = UIImage(named: "\((_myBatteryLevel % 10) + 1)")
>>
>>
>> Jeff Kelley
>>
>> slauncha...@gmail.com | @SlaunchaMan  |
>> jeffkelley.org
>>
>> On Fri, Mar 10, 2017 at 10:41 AM, Eric E. Dolecki 
>> wrote:
>>
>> I have this super simple code, but I'd like to whittle it down to
>> something
>> a lot smaller - basically looking for multiples of 10 (100-0) for a value.
>> I need coffee, what's a great way to do this in Swift 3?
>>
>> if _myBatteryLevel >= 90 {
>> batteryIcon.image = UIImage(named: "10")
>> } else if _myBatteryLevel >= 80 {
>> batteryIcon.image = UIImage(named: "9")
>> } else if _myBatteryLevel >= 70 {
>> batteryIcon.image = UIImage(named: "8")
>> } else if _myBatteryLevel >= 60 {
>> batteryIcon.image = UIImage(named: "7")
>> } else if _myBatteryLevel >= 50 {
>> batteryIcon.image = UIImage(named: "6")
>> } else if _myBatteryLevel >= 40 {
>> batteryIcon.image = UIImage(named: "5")
>> } else if _myBatteryLevel >= 30 {
>> batteryIcon.image = UIImage(named: "4")
>> } else if _myBatteryLevel >= 20 {
>> batteryIcon.image = UIImage(named: "3")
>> } else if _myBatteryLevel >= 10 {
>> batteryIcon.image = UIImage(named: "2")
>> } else if _myBatteryLevel >= 0 {
>> batteryIcon.image = UIImage(named: "1")
>> }
>>
>> Thanks for thinking about my lame code.
>>
>> ___
>>
>> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>> https://lists.apple.com/mailman/options/cocoa-dev/slaunchaman%40gmail.com
>>
>> This email sent to slauncha...@gmail.com
>>
>>
>>
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: More elegance than a long if/else

2017-03-10 Thread Eric E. Dolecki
Thank you!

On Fri, Mar 10, 2017 at 10:48 AM Jeff Kelley  wrote:

> Something like this should work:
>
> batteryIcon.image = UIImage(named: "\((_myBatteryLevel % 10) + 1)")
>
>
> Jeff Kelley
>
> slauncha...@gmail.com | @SlaunchaMan  |
> jeffkelley.org
>
> On Fri, Mar 10, 2017 at 10:41 AM, Eric E. Dolecki 
> wrote:
>
> I have this super simple code, but I'd like to whittle it down to something
> a lot smaller - basically looking for multiples of 10 (100-0) for a value.
> I need coffee, what's a great way to do this in Swift 3?
>
> if _myBatteryLevel >= 90 {
> batteryIcon.image = UIImage(named: "10")
> } else if _myBatteryLevel >= 80 {
> batteryIcon.image = UIImage(named: "9")
> } else if _myBatteryLevel >= 70 {
> batteryIcon.image = UIImage(named: "8")
> } else if _myBatteryLevel >= 60 {
> batteryIcon.image = UIImage(named: "7")
> } else if _myBatteryLevel >= 50 {
> batteryIcon.image = UIImage(named: "6")
> } else if _myBatteryLevel >= 40 {
> batteryIcon.image = UIImage(named: "5")
> } else if _myBatteryLevel >= 30 {
> batteryIcon.image = UIImage(named: "4")
> } else if _myBatteryLevel >= 20 {
> batteryIcon.image = UIImage(named: "3")
> } else if _myBatteryLevel >= 10 {
> batteryIcon.image = UIImage(named: "2")
> } else if _myBatteryLevel >= 0 {
> batteryIcon.image = UIImage(named: "1")
> }
>
> Thanks for thinking about my lame code.
>
> ___
>
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/slaunchaman%40gmail.com
>
> This email sent to slauncha...@gmail.com
>
>
>
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: More elegance than a long if/else

2017-03-10 Thread Jeff Kelley
Something like this should work:

batteryIcon.image = UIImage(named: "\((_myBatteryLevel % 10) + 1)")


Jeff Kelley

slauncha...@gmail.com | @SlaunchaMan  |
jeffkelley.org

On Fri, Mar 10, 2017 at 10:41 AM, Eric E. Dolecki 
wrote:

> I have this super simple code, but I'd like to whittle it down to something
> a lot smaller - basically looking for multiples of 10 (100-0) for a value.
> I need coffee, what's a great way to do this in Swift 3?
>
> if _myBatteryLevel >= 90 {
> batteryIcon.image = UIImage(named: "10")
> } else if _myBatteryLevel >= 80 {
> batteryIcon.image = UIImage(named: "9")
> } else if _myBatteryLevel >= 70 {
> batteryIcon.image = UIImage(named: "8")
> } else if _myBatteryLevel >= 60 {
> batteryIcon.image = UIImage(named: "7")
> } else if _myBatteryLevel >= 50 {
> batteryIcon.image = UIImage(named: "6")
> } else if _myBatteryLevel >= 40 {
> batteryIcon.image = UIImage(named: "5")
> } else if _myBatteryLevel >= 30 {
> batteryIcon.image = UIImage(named: "4")
> } else if _myBatteryLevel >= 20 {
> batteryIcon.image = UIImage(named: "3")
> } else if _myBatteryLevel >= 10 {
> batteryIcon.image = UIImage(named: "2")
> } else if _myBatteryLevel >= 0 {
> batteryIcon.image = UIImage(named: "1")
> }
>
> Thanks for thinking about my lame code.
> ___
>
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/slaunchaman%40gmail.com
>
> This email sent to slauncha...@gmail.com
>
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


More elegance than a long if/else

2017-03-10 Thread Eric E. Dolecki
I have this super simple code, but I'd like to whittle it down to something
a lot smaller - basically looking for multiples of 10 (100-0) for a value.
I need coffee, what's a great way to do this in Swift 3?

if _myBatteryLevel >= 90 {
batteryIcon.image = UIImage(named: "10")
} else if _myBatteryLevel >= 80 {
batteryIcon.image = UIImage(named: "9")
} else if _myBatteryLevel >= 70 {
batteryIcon.image = UIImage(named: "8")
} else if _myBatteryLevel >= 60 {
batteryIcon.image = UIImage(named: "7")
} else if _myBatteryLevel >= 50 {
batteryIcon.image = UIImage(named: "6")
} else if _myBatteryLevel >= 40 {
batteryIcon.image = UIImage(named: "5")
} else if _myBatteryLevel >= 30 {
batteryIcon.image = UIImage(named: "4")
} else if _myBatteryLevel >= 20 {
batteryIcon.image = UIImage(named: "3")
} else if _myBatteryLevel >= 10 {
batteryIcon.image = UIImage(named: "2")
} else if _myBatteryLevel >= 0 {
batteryIcon.image = UIImage(named: "1")
}

Thanks for thinking about my lame code.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com