Open Side Menu Go to the Top

03-27-2013 , 03:06 PM
Right, that was why I asked about the infinite counter. I was wondering if you solved the risk of diving into an infinite loop with some assumed action there. And of course, I was also wondering what you would write in there.

I mean, it *looks* like lazy evaluation, and superficially, it behaves about the same, but you know, it's not and that is the point of it. On one side, I feel like it is splitting hairs, but at the same time, there's that risk risk of getting gunked up, blowing the stack, or chasing rainbows from too much uh... FPS. Or should it be FCS? (Fancy Play Syndrome : Fancy Code Syndrome)
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
03-27-2013 , 04:10 PM
To be clear, the problem was never the infinite counter. It's that the primes generator always recursively calls itself no matter what.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-27-2013 , 07:30 PM
Here's a much deeper explanation of lazy that discusses the underlying mechanisms and primitives you'd consider with lazy evaluation here: http://mitpress.mit.edu/sicp/full-te...html#%_sec_4.2

If you need me to translate any of that into Python-ish, I can do that, but I think their initial illustration is so important that I should write it out.

Code:
def lazyFunction(a, b):
    if a==0:
        return 1
    else:
        return 0

ans = lazyFunction(0, (1/0))

>>> Traceback (most recent call last):
  File "c:/Users/dt1/AppData/Local/Temp/py4684f43", line 232, in <module>
    ans = lazyFunction(0, (1/0))
ZeroDivisionError: integer division or modulo by zero
>>>
A truly lazily evaluated language would not throw an error in this illustration. I've never ran into a situation where I thought I needed this though.

Last edited by daveT; 03-27-2013 at 07:35 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-27-2013 , 10:09 PM
btw, here is something python-specific I learned while coming up with these samples... did anyone notice my slice in the streamOfBaseTwos iterator? There was a reason for it...

Code:
def ranges():
    i = 0
    ans = []
    while True:
        ans.append(i)
        i += 1
        yield ans

r = ranges()
a = next(r)
a.append(-1)
print(next(r))
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-28-2013 , 11:04 PM
Another general survey: is this as bad as I think it is?

Code:
switch (true)
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 12:53 AM
i'm not sure what it will do and i'm *really* not sure what it's intended to do, so yes.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 01:02 AM
Quote:
Originally Posted by Xhad
btw, here is something python-specific I learned while coming up with these samples... did anyone notice my slice in the streamOfBaseTwos iterator? There was a reason for it...

Code:
def ranges():
    i = 0
    ans = []
    while True:
        ans.append(i)
        i += 1
        yield ans

r = ranges()
a = next(r)
a.append(-1)
print(next(r))
Are you talking about the append(-1) line?

What is it you discovered?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 01:04 AM
Quote:
Originally Posted by tyler_cracker
i'm not sure what it will do and i'm *really* not sure what it's intended to do, so yes.
I let myself look at codecademy at work again. The first "case" includes a regex. This is in the same tutorial that explains what a switch statement is. I'm actually tempted to post a "question" and make the entire body of my post a simple, "Seriously?"
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 01:08 AM
Quote:
Originally Posted by daveT
Are you talking about the append(-1) line?

What is it you discovered?
The fact that the -1 appears in the print statement. Meaning, appending it to to a modified ans inside the generator thanks to Python's referencing. That seems like the sort of thing that would almost certainly be not-on-purpose if I were writing a generator intended to yield lists.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 02:07 AM
Quote:
Originally Posted by Xhad
I let myself look at codecademy at work again. The first "case" includes a regex. This is in the same tutorial that explains what a switch statement is. I'm actually tempted to post a "question" and make the entire body of my post a simple, "Seriously?"
Code Academy is coding lessons by the people for the people. Write a better tutorial and submit it, I guess?

I am perplexed that people feel like doing it the easy way is more optimal than the "hard" way when they ultimately gain poor education and results, but I really don't know what to think about this anymore.

I just know that no matter how good I get, I'll never feel like I "learned" to program and that feeling will persist for the next 30 years. I guess that bragging rights goes a long way, but what's the point if you can't actually create anything significant with this knowledge?

I guess it is hard for me to balance this because programming is really difficult for me and those tutorials, when I've taken them, seemed to result in broken thinking more than unified thinking. I can't shake the feeling that, since I'd be lost like hell after using them, everyone else would be lost like hell. At the same time, this suggests that I believe everyone is dumber than I am and that isn't the case. I think I'm about the lowest in the IQ totem pole for programming capability, so others may very well be smarter than I am and the success rate could be astounding for all I know, but I would need quite a bit of convincing.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 02:15 AM
Quote:
Originally Posted by Xhad
The fact that the -1 appears in the print statement. Meaning, appending it to to a modified ans inside the generator thanks to Python's referencing. That seems like the sort of thing that would almost certainly be not-on-purpose if I were writing a generator intended to yield lists.
Why is that surprising? There is only one object allocation in that code snippet. Inside ranges() you are just mutating ans and yielding it.

It's no different from

Code:
a = [1,2,3]
b = a
a.append(4)

print b
# [1,2,3,4]
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 02:28 AM
dave,

i think xhad is keying in on a common problem in technical pedagogy: providing too much information, and at the wrong level. someone who needs a calm explanation of how switch statements work is probably not ready to digest regular expressions. and even if he is, regex is not even what we're talking about; it only complicates the lesson's real purpose, which is to explain switches.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 02:45 AM
They have to get the students up to professional speed in one year at 3 hours a week, which obviously involves mastering regex and switch statements over JSON or XML files.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 06:42 AM
For people that use forward declarations: do you ever get tempted to declare every single function in the program? Is this a good practice or just something that is borne out of irritation?

(before declaring C a crappy language over this: not C; Clojure)
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 07:42 AM
Bah, in the good old days you had to think about what you were going to do so you could tell the compiler what to expect, then you had to tell the compiler what to do, then because we liked typing, we told the compiler what it had done. None of this making **** up as you went along declaring things all over the place. That's just sloppy thinking!
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 07:53 AM
You'll take my circular functions out of my cold dead hands!

I decided to declare 'em all* and I don't care if it's whack; I'm shippin', yo.

*I didn't declare the -main function. I have my limits.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 09:29 AM
Quote:
Originally Posted by ballin4life
Why is that surprising? There is only one object allocation in that code snippet. Inside ranges() you are just mutating ans and yielding it.

It's no different from

Code:
a = [1,2,3]
b = a
a.append(4)

print b
# [1,2,3,4]
I know the exact technical reason why it works the way it does. In fact, I correctly guessed it before I even though to try it. The reason it's a trap is because, the way the generator is written, it is giving the impression that it yields a sequence of lists. That the user can't mutate a return value without affecting future return values is counterintuitive for the same reason that mutable defaults' behavior is counterintuitive.

Quote:
Originally Posted by daveT
Code Academy is coding lessons by the people for the people. Write a better tutorial and submit it, I guess?
It's in a language I don't know (PHP). Then again, my honest advice of deleting that example would improve it.

Quote:
Originally Posted by tyler_cracker
dave,

i think xhad is keying in on a common problem in technical pedagogy: providing too much information, and at the wrong level. someone who needs a calm explanation of how switch statements work is probably not ready to digest regular expressions. and even if he is, regex is not even what we're talking about; it only complicates the lesson's real purpose, which is to explain switches.
Yes. Ironically, a lot of codecademy tutorials make the material look harder than it is and this fancy-for-the-sake-of-fancy (is there a good single word for that) is one of the causes. This particular section is called "Advanced
Switch Statements". I can think of a few words for turning a switch statement into a no-op, but "advanced" is not one of them.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 10:23 AM
Quote:
Originally Posted by Xhad
I know the exact technical reason why it works the way it does. In fact, I correctly guessed it before I even though to try it. The reason it's a trap is because, the way the generator is written, it is giving the impression that it yields a sequence of lists. That the user can't mutate a return value without affecting future return values is counterintuitive for the same reason that mutable defaults' behavior is counterintuitive.
KISS is the Python way, IMO.

Quote:
It's in a language I don't know (PHP). Then again, my honest advice of deleting that example would improve it.
Perfect. We'll just have more script-kiddies building WP modules and that will slowly inch the world to a better, Drupal-ly place.

Quote:
Yes. Ironically, a lot of codecademy tutorials make the material look harder than it is and this fancy-for-the-sake-of-fancy (is there a good single word for that) is one of the causes. This particular section is called "Advanced
Switch Statements". I can think of a few words for turning a switch statement into a no-op, but "advanced" is not one of them.
chichi -- deliberately chic?

^^ Also some fake-Mexican restaurant that is popular in the Midwest and "boobs" in Spanish.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 11:42 AM
It seems that debates about generators can be as infinite as generators :P
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 12:03 PM
Quote:
Originally Posted by Xhad
I know the exact technical reason why it works the way it does. In fact, I correctly guessed it before I even though to try it. The reason it's a trap is because, the way the generator is written, it is giving the impression that it yields a sequence of lists. That the user can't mutate a return value without affecting future return values is counterintuitive for the same reason that mutable defaults' behavior is counterintuitive.
I'm just saying that it is only counterintuitive to the extent that my simple a=[1,2,3], b=a, a.append(4) example is counterintuitive. The mutable defaults behavior is way more counterintuitive ... I mean we know Ruby does mutable defaults differently, but it has the same behavior as Python here, as will AFAIK any language with pointers to mutable data structures.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 12:29 PM
Quote:
I'm just saying that it is only counterintuitive to the extent that my simple a=[1,2,3], b=a, a.append(4) example is counterintuitive
And I'm just saying that I don't agree with this statement at all.

Code:
def count():
    i = 1
    while True:
        yield i
        i += 1

c = count()
a = next(c)
a += 1
I would argue that a nontrivial percentage of people would expect "ranges" to behave more like "count" above, even among those people who understand why a would be mutated in your example. I think this may be one of those problems where you're thinking in the mindset of someone who's already been told there's a trap incoming, and not someone who's just trying to write what they think is straightforward code and accidentally creates a bug.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 05:47 PM
Quote:
Originally Posted by daveT
For people that use forward declarations: do you ever get tempted to declare every single function in the program? Is this a good practice or just something that is borne out of irritation?

(before declaring C a crappy language over this: not C; Clojure)
In C I think about scope / visibility. The public interface of a file goes in a header. Everything else I mark static and usually declare at the top of the .c.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-29-2013 , 09:03 PM
Quote:
Originally Posted by Xhad
And I'm just saying that I don't agree with this statement at all.

Code:
def count():
    i = 1
    while True:
        yield i
        i += 1

c = count()
a = next(c)
a += 1
I would argue that a nontrivial percentage of people would expect "ranges" to behave more like "count" above, even among those people who understand why a would be mutated in your example. I think this may be one of those problems where you're thinking in the mindset of someone who's already been told there's a trap incoming, and not someone who's just trying to write what they think is straightforward code and accidentally creates a bug.
The only difference I see is how much stuff is in between. I'm sure code like
Code:
a = [1,2,3]
b = a
# two pages of code here
# ...
a.append(4)
# two more pages of code here
# ...
print b
has caused bugs for many people too.

Now, if your beef was with the list += operator then we'd be talking

Specifically:
Code:
def ranges():
    i = 0
    ans = []
    while True:
        ans += [i]
        i += 1
        yield ans
also has the same problem, since list += other_list is not the same as list = list + other_list
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-30-2013 , 09:43 AM
Quick one, how do the people here that employ Agile approach software design? Not looking for the "right" answer, just interested in different experiences. Thanks. Enjoyable forum btw.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
03-30-2013 , 12:23 PM
What do you guys think of the Ouya gaming system?

I think it's a pretty cool idea, but I wish they were a bit better at marketing. Creating games for a system that will have 10,000 users doesn't sound like a good use of your time, except for learning and stuff. Wouldn't it just be better to create an indie install program and sell the controllers so people can play on their PC or tablets?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

      
m