Re: [go-nuts] Is it possible to simplify this snippet?

2019-05-01 Thread Gopal M
ternary can be there is Go, Since most of the programmers are used to it.
Readability is the issue of programmer who writes and reads it, I don't see
any point in restricting this..

On Thu, May 2, 2019 at 3:51 AM Andy Balholm  wrote:

> You could have a cleaner switch statement if you had a list of the keys
> that were pressed, instead of needing to check each one individually:
>
> func whichKeys(keysToCheck ...keyID) []keyID {
> var result []keyID
> for _, k := range keysToCheck {
> if rl.IsKeyDown(k) {
> result = append(result, k)
> }
> }
>
> return result
> }
>
> for _, k := range whichKeys(rl.KeyA, rl.KeyD, rl.KeyW, rl.KeyS) {
> switch k {
> case rl.KeyA:
> p.Rect.X -= 1
> case rl.KeyD:
> p.Rect.X += 1
> case rl.KeyW:
> p.Rect.Y -= 1
> case rl.KeyS:
> p.Rect.Y += 1
> }
> }
>
> Andy
>
>
> On May 1, 2019, at 2:51 PM, Burak Serdar  wrote:
>
> On Wed, May 1, 2019 at 2:31 PM  wrote:
>
>
> Great example of why future Go updates should include the ternary operator.
> Your code is mess-ey when written using keywords 'if' or 'switch'
> but  using '?' it becomes much cleaner
>
> p.Rect.X +=  rl.IsKeyDown(rl.KeyA) ? -1:0   +  (rl.IsKeyDown(rl.KeyD) ? 1
> : 0 )
> p.Rect.Y +=  rl.IsKeyDown(rl.KeyW) ? -1:0   +  (rl.IsKeyDown(rl.KeyS) ? 1
> : 0 )
>
>
> I don't think this is readable at all. I think the cascading-ifs
> version is much easier to read.
>
> You could do something like the following, but I think pointless
> unless there's more to the original snippet:
>
> checkKey:=func(k, v int) int {
>  if rl.IsKeyDown(k) {
>return v
>  }
> return 0
> }
> p.Rect.X+=checkKey(rl.KeyA,-1)+checkKey(rl.KeyD,1)
> p.Rect.Y+=checkKey(rl.KeyW,-1)+checkKey(,rl.KeyS,1)
>
>
> On Wednesday, May 1, 2019 at 8:38:10 AM UTC-4, гусь wrote:
>
>
> if rl.IsKeyDown(rl.KeyA) {
> p.Rect.X -= 1
> }
> if rl.IsKeyDown(rl.KeyD) {
> p.Rect.X += 1
> }
> if rl.IsKeyDown(rl.KeyW) {
> p.Rect.Y -= 1
> }
> if rl.IsKeyDown(rl.KeyS) {
> p.Rect.Y += 1
> }
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>


-- 
Regards,
M. Gopal

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Is it possible to simplify this snippet?

2019-05-01 Thread Andy Balholm
You could have a cleaner switch statement if you had a list of the keys that 
were pressed, instead of needing to check each one individually:

func whichKeys(keysToCheck ...keyID) []keyID {
var result []keyID
for _, k := range keysToCheck {
if rl.IsKeyDown(k) {
result = append(result, k)
}
}

return result
}

for _, k := range whichKeys(rl.KeyA, rl.KeyD, rl.KeyW, rl.KeyS) {
switch k {
case rl.KeyA:
p.Rect.X -= 1
case rl.KeyD:
p.Rect.X += 1
case rl.KeyW:
p.Rect.Y -= 1
case rl.KeyS:
p.Rect.Y += 1
}
}

Andy


> On May 1, 2019, at 2:51 PM, Burak Serdar  wrote:
> 
> On Wed, May 1, 2019 at 2:31 PM mailto:lgod...@gmail.com>> 
> wrote:
>> 
>> Great example of why future Go updates should include the ternary operator.
>> Your code is mess-ey when written using keywords 'if' or 'switch'
>> but  using '?' it becomes much cleaner
>> 
>> p.Rect.X +=  rl.IsKeyDown(rl.KeyA) ? -1:0   +  (rl.IsKeyDown(rl.KeyD) ? 1 : 
>> 0 )
>> p.Rect.Y +=  rl.IsKeyDown(rl.KeyW) ? -1:0   +  (rl.IsKeyDown(rl.KeyS) ? 1 : 
>> 0 )
> 
> I don't think this is readable at all. I think the cascading-ifs
> version is much easier to read.
> 
> You could do something like the following, but I think pointless
> unless there's more to the original snippet:
> 
> checkKey:=func(k, v int) int {
>  if rl.IsKeyDown(k) {
>return v
>  }
> return 0
> }
> p.Rect.X+=checkKey(rl.KeyA,-1)+checkKey(rl.KeyD,1)
> p.Rect.Y+=checkKey(rl.KeyW,-1)+checkKey(,rl.KeyS,1)
> 
>> 
>> On Wednesday, May 1, 2019 at 8:38:10 AM UTC-4, гусь wrote:
>>> 
>>> if rl.IsKeyDown(rl.KeyA) {
>>> p.Rect.X -= 1
>>> }
>>> if rl.IsKeyDown(rl.KeyD) {
>>> p.Rect.X += 1
>>> }
>>> if rl.IsKeyDown(rl.KeyW) {
>>> p.Rect.Y -= 1
>>> }
>>> if rl.IsKeyDown(rl.KeyS) {
>>> p.Rect.Y += 1
>>> }
>> 
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to golang-nuts+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com 
> .
> For more options, visit https://groups.google.com/d/optout 
> .

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Is it possible to simplify this snippet?

2019-05-01 Thread Reto
Granted, only if they are mutually exclusive...
Sorry, I missed the part of your mail mentioning that

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Is it possible to simplify this snippet?

2019-05-01 Thread Reto
On Wed, May 01, 2019 at 09:05:19AM -0700, jake6...@gmail.com wrote:
> There is nothing wrong with the code you presented. It is clear and readable.
> If you find that it clutters the function too much, then make it into it's 
> only utility function.

But it is wrong, even from a readability perspective.
You should not be writing an if chain like this, when we have case / switch 
operator.
*If* you insist of doing that, at least use `else if` so that you don't test 
every single test case if you know that only one can ever be true.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Is it possible to simplify this snippet?

2019-05-01 Thread jake6502


On Wednesday, May 1, 2019 at 8:41:46 AM UTC-4, Jan Mercl wrote:
>
> On Wed, May 1, 2019 at 2:37 PM гусь > 
> wrote: 
> > 
> > if rl.IsKeyDown(rl.KeyA) { 
> > p.Rect.X -= 1 
> > } 
> > if rl.IsKeyDown(rl.KeyD) { 
> > p.Rect.X += 1 
> > } 
> > if rl.IsKeyDown(rl.KeyW) { 
> > p.Rect.Y -= 1 
> > } 
> > if rl.IsKeyDown(rl.KeyS) { 
> > p.Rect.Y += 1 
> > } 
>
> A switch statement would be perhaps more readable and certainly better 
> performing on average. 
>

To clarify. A switch would not work correctly if it was possible for more 
than one of these to be true. 

I would also point out that go is about creating clear and readable code. 
It is less about writing compact code than many other languages. This takes 
some getting used to. For example, in C this could be done in two lines 
using ternary operators. There is nothing wrong with the code you 
presented. It is clear and readable. If you find that it clutters the 
function too much, then make it into it's only utility function. 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Is it possible to simplify this snippet?

2019-05-01 Thread Jan Mercl
On Wed, May 1, 2019 at 2:37 PM гусь  wrote:
>
> if rl.IsKeyDown(rl.KeyA) {
> p.Rect.X -= 1
> }
> if rl.IsKeyDown(rl.KeyD) {
> p.Rect.X += 1
> }
> if rl.IsKeyDown(rl.KeyW) {
> p.Rect.Y -= 1
> }
> if rl.IsKeyDown(rl.KeyS) {
> p.Rect.Y += 1
> }

A switch statement would be perhaps more readable and certainly better
performing on average.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Is it possible to simplify this snippet?

2019-05-01 Thread гусь
 if rl.IsKeyDown(rl.KeyA) {
p.Rect.X -= 1
}
if rl.IsKeyDown(rl.KeyD) {
p.Rect.X += 1
}
if rl.IsKeyDown(rl.KeyW) {
p.Rect.Y -= 1
}
if rl.IsKeyDown(rl.KeyS) {
p.Rect.Y += 1
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.