Saturday, May 29, 2010

ActionScript: JavaScript Event Handlers for JW Player

JW Player has a rich JavaScript API. One of the things you can do is to create JavaScript event handlers for ActionScript events. However, there are a few things that can bite you in the butt if you don't keep them in mind.

First of all, as the documentation points out, you can't set an event handler until JW Player is ready for it. If you set it too early, it won't work, and you won't even get an error message. If you have a function called playerReady, it'll automatically be called by JW Player. That's a great place to setup your handlers.

Next, when JW Player calls playerReady, it's supposed to pass an obj containing the ID for the HTML DOM object. In my experience, it doesn't. Hence, you have to lookup the object manually. See my previous post about the fact that you can't use jQuery's $() to lookup the object, but should instead stick with document.getElementById.

Last of all, remember that when you give JW Player your JavaScript callback, you have to pass the function's name as a string. It feels natural to pass a function pointer or even to have an inline function, but that doesn't work (nor will you get an error message).

In summary, here's the code:
function playerReady(obj) {
"TIME", "handleTimeEvent");

function handleTimeEvent(e) {

ActionScript: jQuery and ActionScript Callbacks

Let's suppose you have a SWF, and you're calling callbacks on that SWF from JavaScript. The following works:
The following doesn't work, and there is no error message:
I often think of $() as a way to use document.getElementById, but since it returns a jQuery shim, some things don't work as expected. This one took me quite a while to figure out.

Thursday, May 20, 2010

Neuroscience: Burn-out Visible in the Brains of Patients

I just found this on Hacker News: Burn-out visible in the brains of patients. Since I've suffered from burnout for about a decade, this comes as no surprise to me.

Try to do pushups until you can't do any more. Now, wait a minute, and then do 50 more pushups. That's the best way I can explain what burnout feels like--my brain just feels like jello a lot of times.

I'm sure a lot of other programmers have to deal with this just like I do.

Tuesday, May 18, 2010

Apple: iPad and Emacs

Someone asked my boss's buddy Art Medlar if he was going to buy an iPad. He said, "I figure as soon as it runs Emacs, that will be the sign to buy." I think he was just trying to be funny, but his statement is actually fairly profound.

It's well known that submitting iPhone and iPad applications for sale on Apple's store is a huge pain--even if they're free and open source. Apple is acting as a gatekeeper for what is and isn't allowed on your device. I heard that Apple would never allow a scripting language to be installed on your iPad because it would allow end users to run code that they hadn't verified. (I don't have a reference for this, but if you do, please post it below.) Emacs is mostly written in Emacs Lisp. Per Apple's policy, I don't think it'll ever be possible to run Emacs on the iPad.

Emacs was written by Richard Stallman, and it practically defines the Free Software movement (in a manner of speaking at least). Stallman's vision for the future of computing is very open, and Apple's vision for the future of computing is very closed. Hence, it's ironic that Emacs, which is such a profound part of Free Software history, can't ever run on the iPad.

Hence, I think there's a profound truth when Art Medlar said, "I figure as soon as it runs Emacs, that will be the sign to buy."

Tuesday, May 11, 2010

Books: The Little Schemer

I just finished The Little Schemer. I liked it. It's a very short book. Most of my time was spent doing the exercises, which were very worthwhile. There were a few sections I had to read twice, but the book was far more accessible than say SICP. If you've never coded in Lisp or Scheme or if you're looking for some good exercises for your coding fu, I highly recommend this book. Happy hacking!

Saturday, May 08, 2010

Python: The Halting Problem

I've been reading The Little Schemer, and I particularly enjoyed learning about the halting problem. I enjoyed learning about it so much that I figured I'd take the time to explain it in Python so that other Pythonistas could enjoy it with me ;)

Let's suppose I have two functions:
def does_stop():
return True

def does_not_stop():
while True:
does_stop does stop. does_not_stop does not stop. How do I implement the following function:
def will_stop(f):
"""Return True if the function f eventually stops, and False otherwise."""
Let's suppose for a moment that I can implement such a function. Now, let's look at this function:
def just_might_stop():
return will_stop(just_might_stop) and does_not_stop()
Does just_might_stop stop or does it continue forever? That is, what is the value of "will_stop(just_might_stop)"?

Well, I don't know yet, but let's suppose that "will_stop(just_might_stop)" returns True, i.e. that just_might_stop does stop. Well, looking at the definition of just_might_stop, it should "return will_stop(just_might_stop) and does_not_stop()". Since we stipulated that "will_stop(just_might_stop)" is True, then the function will execute "does_not_stop()", and hence, just_might_stop will never stop.

This time, let's suppose that "will_stop(just_might_stop)" returns False, i.e. that just_might_stop does not stop. Looking at the definition of just_might_stop, it should "return will_stop(just_might_stop) and does_not_stop()", i.e. it should return False. However, that means that just_might_stop did stop, even though we stipulated that "will_stop(just_might_stop)" returns False.

It turns out that whether will_stop(just_might_stop) returns True or False, it still leads to a contradiction. Hence, it turns out that it's not possible to implement will_stop.

Fun, huh? It's yet another version of "this statement is false".

Scheme: My Y Combinator

I've been reading The Little Schemer, and it posed an interesting question: how can you create a recursive function without having the ability to "define" a name for it? For instance, in Scheme, how can you create a simple, recursive function to calculate the length of a list without having the ability to use "define"?

Here's my approach:
((lambda (my-length l)
(my-length my-length l))
(lambda (my-length l)
((null? l) 0)
(else (add1 (my-length my-length (cdr l))))))
'(1 2 3 4 5))
The outer function is "(lambda (my-length l) ...)". It takes a reference to a function that it calls my-length. It calls that function "(my-length my-length l)". Hence, that function, which I call "my-length", receives the list as well as a reference to itself, "(lambda (my-length l) ...)". Since it receives a reference to itself, it's able to call itself recursively.

It turns out the real answer is called the Y Combinator. It's written as:
Y = λf·(λx·f (x x)) (λx·f (x x))
It was created by Haskell Curry. To use the Y Combinator to solve the above problem, you'd write:
((lambda (le)
((lambda (mk-length)
(mk-length mk-length))
(lambda (mk-length)
(le (lambda (x)
((mk-length mk-length) x))))))
(lambda (length)
(lambda (l)
((null? l) 0)
(else (add1 (length (cdr l)))))))
Notice that my version is shorter, but the Y Combinator version is more elegant because the actual length function isn't burdened with having to recursively pass a reference to itself.

If you didn't understand any of the above, don't sweat it. I read it twice, and I still barely get it ;) However, I think I now understand why Paul Graham used that name for his startup incubator, Y Combinator. Paul Graham is a Lisp guy, and his Y Combinator is a startup meant to recursively launch other startups ;)

Wednesday, May 05, 2010

Humor: An Environmentally-friendly Desktop

In an effort to be more environmentally conscious, I've decided to make my desktop more green. And although my sympathies lie with California's Central Valley farmers, I've also decided to show my support of the delta smelt by switching to a shell called "fish".

Do I think my actions will solve global warming? No--but it's important for each of us to do our part!

Tuesday, May 04, 2010

Linux: fish: The Friendly, Interactive Shell

I'm trying out a new shell called fish. Here are some screen shots and here is a great article to help you get started.

Usually, I'm a zsh user. fish aims to be as powerful as, say, zsh, but a heck of a lot easier to use and a lot easier to learn. So far, that's proven true. The shell provides intelligent tab completion, syntax highlighting, helpful error reporting, integrated documentation, etc.

Fish is inspired by Bourne shell syntax, but is not compatible with it. Specifically, a lot of ugly things have been cleaned up and made more regular. I do think that the syntax is nicer, although it takes a while to get the hang of if you're already a shell expert. About the only inconvenient part of switching to fish is that I can no longer copy and paste complicated bash one-liners from various places online.

The documentation is excellent. However, you might find this cheat sheet helpful for getting started:
: Get help with using fish.
Searching through history:
: Type in the search string, and press the up arrow.
: Open a file using the proper application.
if true; echo hello; end
for i in a b c; echo $i; end
switch $you; case '*'; echo hi; end
function hi; echo hello; end
rm $file:
: You don't have to quote $file, even if it has spaces.
: Double quotes do variable substitution. Single quotes don't.
: Nesting doubles in singles or singles in doubles doesn't hurt
: anything. Both forms allow a minimal escaping, such as \', \\,
: or \$.
set smurf blue
: This is variable assignment. They didn't use the "=" syntax
: because in fish, *everything* is a command. Use -e to erase a
: variable, -l to set a variable locally, -g to set a variable
: globally, and -u to set a variable universally. Setting a
: variable universally applies to all fish sessions and will
: even survive a reboot. Use -x when setting a variable to export
: the value of that variable. It is convention that exported
: variables are in uppercase and unexported variables are in
: lowercase.
echo (ls)
: Use parenthesis for subshells.
f1; and f2
: This is like f1 && f2 in bash.
ls **.as
: Find all the .as files recursively.
: Redirect standard error to a file. ^^ is used for appending to
: a file.
function ll; ls -l $argv; end
: This is the replacement for aliasing and "$@".
: This is $! in bash.
: This is the directory for all your functions. They must use a
: .fish extension.
echo input.{c,h,txt}
: Echoes: input.c input.h input.txt
echo {$USER}meister
for i in (seq 10); echo $i; end
: Counts to 10. Actually, so does "seq 10" ;)
set my_array a b c; echo $my_array[1]
: Prints "a". Note, arrays are 1-based, just like seq.
count $my_array
: Returns the number of items in the array.
for i in (seq (count $my_array)); echo $my_array[$i]; end
: Here's how to loop over an array.
for i in $PATH; echo $i; end
: This is the easier way to loop over an array.
The startup file is ~/.config/fish/
: Quickly lists the current directory, or the directory your
: cursor is over.
: Adds "| less". Think "pager".
Fish supports even more Emacs-like commands--even a killring.