I think I just had an epiphany regarding the upcoming coroutine support in Python. I don't mean I came up with anything new (I think Ruby programmers have been doing it for a long time), just that I finally grok it—or at least, I think I do.
You see, I'm writing a little AJAX-based flash card website and I started off writing a standalone dynamic HTML mock-up of (obviously) the client side but without any communication to the server yet.
I then wrote a console-based flash card program to experiment with the algorithm I want to use for what card to show when, when to learn new cards, etc. The console-based program just has a function called test that takes a card object, tests the card on the user and returns a boolean as to whether the user got the card right or not.
So my code has a bunch of places where I say:
for card in to_test: correct = test(card) if correct: ... else: ...
In theory, it's only this test function that's throw away. It would be nice if I just had to replace those calls to test when I come to write the server version.
The only catch is that it will be the client that initiates requests for cards. Easy, I thought: I can use yield statements in the server code wherever I want to present the user with the next card. That way, the client (or more accurately some proxy for the client running on the server) can do a next() to get the next card.
The problem is that the client needs to return the result. The yield can't be a one-way street. At the point the server yields a card, it needs to also find out the result of that yield.
Enter coroutines. If I understand correctly, this is exactly the kind of problem coroutines solve. The yield statement in my server-side code becomes a yield expression. The client sends the generator the result of testing the card and that becomes what the yield expression evaluates to.
Have I understood coroutines correctly? If so, I can't wait for Python 2.5!
by : Created on Nov. 13, 2005 : Last modified Nov. 13, 2005 : (permalink)
Okay, so this is just about the coolest optical illusion I've ever seen. Especially the second part about the pink dots disappearing all together.
Apparently the visual cortex only cares about differences and the pink dots are fuzzy enough (especially when you are focused on something else) that they seem constant even though the eye is doing its usual fast little movements (saccades). The green appears because a reduction in pink and a gain in green are the same thing.
Jenni pointed out to me that
if you fixate somewhere outside the circle, it still works, but if you move your focus to another point outside the circle, you can see the pink again. You don't have to look at the pink dots again to make them reappear. You just have to move them to a different point on the retina.
by : Created on Nov. 13, 2005 : Last modified Nov. 13, 2005 : (permalink)