Two Plus Two Publishing LLC
Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > >

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 02-08-2012, 07:38 PM   #2376
Neko
Pooh-Bah
 
Join Date: Jul 2005
Posts: 4,000
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

I think it's pretty debatable whether it's a bad practice or not.

Quote:
Originally Posted by Xhad View Post
This didn't occur to me, but it probably is given that the length could hypothetically change during the loop (assigning to indices outside the existing bounds extends the array)
My (admittedly foggy) recollection is that .length is updated just once when the size of the array is increased.

Modifying an array you are looping over is definitely a bad practice though
Neko is offline   Reply With Quote
Old 02-08-2012, 07:43 PM   #2377
clowntable
Carpal \'Tunnel
 
clowntable's Avatar
 
Join Date: Jun 2006
Location: 39, 46, 56, 59, 191
Posts: 45,558
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

The biggest offense to my sense of style is that the numbers can't sum themselves.
clowntable is offline   Reply With Quote
Old 02-08-2012, 07:53 PM   #2378
myNameIsInga
adept
 
Join Date: Apr 2010
Location: Deep end of the player pool
Posts: 746
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Xhad View Post
This didn't occur to me, but it probably is given that the length could hypothetically change during the loop (assigning to indices outside the existing bounds extends the array)
Changing the size of something (especially increasing it) like a collection when using its Length or Count in a for-loop is a recipe for disaster imo. That loop could end up running until you are out of memory. Decreasing the size I think you run the risk of causing an index out of range exception.

I know its "legal" in C# to for example add elements to a List inside a for-loop but I think its probably a very bad idea
myNameIsInga is offline   Reply With Quote
Old 02-08-2012, 08:09 PM   #2379
myNameIsInga
adept
 
Join Date: Apr 2010
Location: Deep end of the player pool
Posts: 746
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by kyleb View Post
Never calculate something n times when 1 times is enough. I can't believe I see that example in for loops in tutorials.
I know in C# the arr.Length calc is done only once. Its actually the case that using: (int i = 0; i < arr.Length; i++) is faster than the alternative in the case of C#
myNameIsInga is offline   Reply With Quote
Old 02-08-2012, 08:39 PM   #2380
Zurvan
Retired
 
Zurvan's Avatar
 
Join Date: Apr 2005
Location: On the front porch, yelling at kids
Posts: 32,281
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

I feel like I must be wrong, otherwise someone would have noticed before now, but I think this is wrong:

Code:
for ( i = 0; i < raceTimes.length; i++ ) {
    var totalTime = (totalTime || 0) + raceTimes[i];
}
totalTime there is scoped within the for loop, and thus doesn't exist outside, isn't it?

Not that I can ever remember stupid scoping rules in javascript
Zurvan is offline   Reply With Quote
Old 02-08-2012, 08:44 PM   #2381
Xhad
Carpal \'Tunnel
 
Xhad's Avatar
 
Join Date: Jul 2005
Location: hatredcopter
Posts: 10,962
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Zurvan: I don't know the answer to that myself but here is something else funny about this whole thing. The codeyear site has this little webform where you can type in your code and run it. Interestingly enough it actually gives a warning about that version of the loop for the exact reason you said, but if you run it it works anyway.
Xhad is offline   Reply With Quote
Old 02-08-2012, 09:08 PM   #2382
Neko
Pooh-Bah
 
Join Date: Jul 2005
Posts: 4,000
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Zurvan View Post
I feel like I must be wrong, otherwise someone would have noticed before now, but I think this is wrong:

Code:
for ( i = 0; i < raceTimes.length; i++ ) {
    var totalTime = (totalTime || 0) + raceTimes[i];
}
totalTime there is scoped within the for loop, and thus doesn't exist outside, isn't it?

Not that I can ever remember stupid scoping rules in javascript
I actually noticed that too, but I think js has some weird ass scoping rules;

I just ran this:

Code:
function run_tests(){
    
    for (var i=0; i<35; i++){
	var test = i;
    }
    console.log(test);
}
and the result is 34
Neko is offline   Reply With Quote
Old 02-08-2012, 09:14 PM   #2383
_dave_
_Pooh_Bah_
 
Join Date: Feb 2005
Location: UK (or what remains of it)
Posts: 12,634
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

I thought about that scope being wrong too lol. I guess javascript doesn't local scope in loops, only in functions / objects?

but I think it's bad to repeatedly use var insidde the loop in the second version, I'm sure I remember a tutorial telling me (re)instancing a variable is expensive compared to using one already defined. maybe that's changed these days though.
_dave_ is offline   Reply With Quote
Old 02-08-2012, 09:17 PM   #2384
Neko
Pooh-Bah
 
Join Date: Jul 2005
Posts: 4,000
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Now that I think of it, Python also does something similar:

Code:
for i in range(10):
    q = 10
    
print q
will print 10
Neko is offline   Reply With Quote
Old 02-08-2012, 09:26 PM   #2385
Zurvan
Retired
 
Zurvan's Avatar
 
Join Date: Apr 2005
Location: On the front porch, yelling at kids
Posts: 32,281
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

What I don't understand is why reinstancing the variable isn't resetting it.

I javascript, but **** like this drives me nuts. It's why I'm such a Nazi at work about writing clean JS
Zurvan is offline   Reply With Quote
Old 02-08-2012, 09:38 PM   #2386
Xhad
Carpal \'Tunnel
 
Xhad's Avatar
 
Join Date: Jul 2005
Location: hatredcopter
Posts: 10,962
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Neko View Post
Now that I think of it, Python also does something similar:

Code:
for i in range(10):
    q = 10
    
print q
will print 10
Thinking about this it makes more sense to me than the alternative given that you can assign to variables without declaring them. Otherwise, how would something like this work?

Code:
q = someinitialvalue
for x in somelist:
   q = somefunctionof(q, x)

print q
Interestingly enough the following throws an error in Python 3. I remember reading this isn't the case in Python 2 though!

Code:
[i for i in range(10)]
print(i)
Xhad is offline   Reply With Quote
Old 02-08-2012, 09:48 PM   #2387
TheIrishThug
Pooh-Bah
 
TheIrishThug's Avatar
 
Join Date: Jan 2005
Location: Belligerent and numerous
Posts: 5,223
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Some languages to block scoping (Lexical scoping) while other just have function scoping.
TheIrishThug is offline   Reply With Quote
Old 02-08-2012, 10:08 PM   #2388
tyler_cracker
Carpal \'Tunnel
 
tyler_cracker's Avatar
 
Join Date: Apr 2005
Location: Shallow End OTKP
Posts: 15,824
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Xhad View Post
Interestingly enough the following throws an error in Python 3. I remember reading this isn't the case in Python 2 though!

Code:
[i for i in range(10)]
print(i)
Code:
[tyler@toaster:~]$ python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> [i for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(i)
9
i'm not sure how i feel about this. probably bad.
tyler_cracker is offline   Reply With Quote
Old 02-08-2012, 10:15 PM   #2389
MrWooster
veteran
 
MrWooster's Avatar
 
Join Date: Mar 2007
Location: Shoving AK
Posts: 2,984
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Zurvan View Post
I feel like I must be wrong, otherwise someone would have noticed before now, but I think this is wrong:

Code:
for ( i = 0; i < raceTimes.length; i++ ) {
    var totalTime = (totalTime || 0) + raceTimes[i];
}
totalTime there is scoped within the for loop, and thus doesn't exist outside, isn't it?

Not that I can ever remember stupid scoping rules in javascript
JavaScript doesn't scope within loops, only objects.
MrWooster is offline   Reply With Quote
Old 02-08-2012, 10:15 PM   #2390
Xhad
Carpal \'Tunnel
 
Xhad's Avatar
 
Join Date: Jul 2005
Location: hatredcopter
Posts: 10,962
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

From what I understand there is some over-my-head reason why they couldn't fit comprehensions into Python 2 without either introducing that quirk or breaking backward compatibility. So Python 2 chose the first option and Python 3 chose the second.
Xhad is offline   Reply With Quote
Old 02-08-2012, 10:18 PM   #2391
tyler_cracker
Carpal \'Tunnel
 
tyler_cracker's Avatar
 
Join Date: Apr 2005
Location: Shallow End OTKP
Posts: 15,824
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

relatedly, i don't think i posted this here but i should have because i ****ing love it: https://www.destroyallsoftware.com/talks/wat

(i pronounce "wat" more like "waat" than "waet" but watever.)

my coworker who sent that to me wondered what a wat for python might look like. today he posted this:

Quote:
When you compare objects of different types in Python, and they don't implement the special methods that indicate how they should be compared, Python will still compare them. But it compares them based on the alphabetical orders of their types. So every int is less than every string, because "int" < "str".

I think they did this to get a consistent sort order when a list contains more than one kind of object. Still it's one thing to say "all numbers are less than all strings" and another to say "all numbers are less than all strings BECAUSE the string 'int' is less than the string 'str' and this also means that all floats are less than all ints, all lists are less than all tuples, all dicts are less than all sets."

In Python 3, they took this out and comparing objects of different types raises an exception by default. You can still define the special methods on a class to allow them to be compared, but you have to do it explicitly. (When sorting, you can also provide a key function that generates keys of a single type from whatever objects are in the list.)
tyler_cracker is offline   Reply With Quote
Old 02-08-2012, 10:47 PM   #2392
kyleb
Carpal \'Tunnel
 
kyleb's Avatar
 
Join Date: Sep 2004
Location: Seattle
Posts: 39,019
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

lol javascript
kyleb is offline   Reply With Quote
Old 02-08-2012, 11:29 PM   #2393
Xhad
Carpal \'Tunnel
 
Xhad's Avatar
 
Join Date: Jul 2005
Location: hatredcopter
Posts: 10,962
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Don't think this is quite on the level of weird as that JavaScript stuff, but ime this throws nearly everyone at first. Compare this:

Code:
def ok(n, x=0):
    x += n
    print(x)

ok(1)
ok(2)
To this:

Code:
def wat(n, x = []):
    x.append(n)
    print(x) #or print x for you Python 2 people

wat(1)
wat(2)
Spoiler:
Xhad is offline   Reply With Quote
Old 02-09-2012, 12:34 AM   #2394
tyler_cracker
Carpal \'Tunnel
 
tyler_cracker's Avatar
 
Join Date: Apr 2005
Location: Shallow End OTKP
Posts: 15,824
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Xhad View Post
Spoiler:
Spoiler:
tyler_cracker is offline   Reply With Quote
Old 02-09-2012, 01:30 AM   #2395
sorrow
old hand
 
sorrow's Avatar
 
Join Date: Apr 2008
Location: Perth, Western Australia
Posts: 1,531
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

For amusement value: https://www.destroyallsoftware.com/talks/wat

Talking about various scripting language oddities, javascript high among them.

(Hangs head in shame for grunching the last couple of posts...)
sorrow is offline   Reply With Quote
Old 02-09-2012, 01:32 AM   #2396
Xhad
Carpal \'Tunnel
 
Xhad's Avatar
 
Join Date: Jul 2005
Location: hatredcopter
Posts: 10,962
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

OK, idly looked at SO and this one threw me for a loop (no pun intended):

Code:
>>> a, b, c = (lambda: x for x in range(3))
>>> a()
2
>>> b()
2
>>> c()
2
Xhad is offline   Reply With Quote
Old 02-09-2012, 01:56 AM   #2397
kyleb
Carpal \'Tunnel
 
kyleb's Avatar
 
Join Date: Sep 2004
Location: Seattle
Posts: 39,019
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

God JS is so great but there is so much stupid **** about it as evidenced in that video.
kyleb is offline   Reply With Quote
Old 02-09-2012, 04:00 AM   #2398
goofyballer
 
goofyballer's Avatar
 
Join Date: Jun 2005
Posts: 62,498
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by sorrow View Post
For amusement value: https://www.destroyallsoftware.com/talks/wat

Talking about various scripting language oddities, javascript high among them.

(Hangs head in shame for grunching the last couple of posts...)
I feel like it would be hilarious to make someone who doesn't program sit through that and watch their bewilderment at why these nerds are laughing at this **** (obv I found it hilarious too)
goofyballer is offline   Reply With Quote
Old 02-09-2012, 10:07 AM   #2399
TheIrishThug
Pooh-Bah
 
TheIrishThug's Avatar
 
Join Date: Jan 2005
Location: Belligerent and numerous
Posts: 5,223
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Xhad View Post
...
Code:
def wat(n, x = []):
    x.append(n)
    print(x) #or print x for you Python 2 people

wat(1)
wat(2)
Spoiler:
This was one of the first things I learned in Python when I started really using it. When the parser creates the method, it initializes the default value and holds on to it. When the default happens to be a reference type, it holds on to the same reference. Still, very Wat!
TheIrishThug is offline   Reply With Quote
Old 02-09-2012, 01:08 PM   #2400
MrWooster
veteran
 
MrWooster's Avatar
 
Join Date: Mar 2007
Location: Shoving AK
Posts: 2,984
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by sorrow View Post
For amusement value: https://www.destroyallsoftware.com/talks/wat

Talking about various scripting language oddities, javascript high among them.

(Hangs head in shame for grunching the last couple of posts...)
Brilliant!!
MrWooster is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Forum Jump


All times are GMT -4. The time now is 05:58 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright ę 2008-2017, Two Plus Two Interactive
 
 
Poker Players - Streaming Live Online