Log in

No account? Create an account
Today's violation of the Principle of Least Surprise 
13th-Jul-2005 11:15 pm
  var answer = [];

  for (word in ['this','language','can','bite','me']) {
Just to make it easy, I'll tell you that the language in question is Javascript, [...] is an array constructor, and .push appends its arguments to the end of the list/array. The answer, of course, is...

See, Javascript doesn't actually have arrays, Javascript has objects with numbered properties. And since foo.['whatever'] is defined to be foo.whatever with numbers and strings automagically converting to each other as needed, you'll never, ever notice the difference.

Unless, of course, you try to use for, which iterates through property names, not property values.

I suppose, to be completely fair, I should note that in Perl, some idiot could conceivably do
  for my $word (['this','language','can','bite','me']) {
     push @answer, $word;
and get a list of length 1 for their trouble. Oh, well...
14th-Jul-2005 01:34 pm (UTC)
So you have to say

var answer = [];

var foo = ['this', 'language', 'can', 'bite', 'me'];

for (index in foo) {

Yes, I'd say that's gratuitously confusing. Of course in Lisp you can do

(let ((answer nil))
(dolist (word '("this" "language" "can" "bite" "me"))
(push word answer))

and get ... yes ...

("me" "bite" "can" "language" "this")

But to be fair, you can use VECTOR-PUSH-EXTEND instead of PUSH to build an array instead of a list, and then it'll be the right way around. Or call NREVERSE when you're done. Or use Hairy Loop:

(loop for word in ("this" "language" "can" "bite" "me")
collect word)

which yields what you'd expect.
14th-Jul-2005 01:35 pm (UTC)
Bleah, the CODE tag doesn't preserve indentation. Sorry.
14th-Jul-2005 08:49 pm (UTC)
... and I neglected to quote the word list in my Hairy Loop example. Feh.
15th-Jul-2005 02:57 am (UTC)
<PRE> is your friend.
14th-Jul-2005 01:50 pm (UTC)
I was surprised that "push" in Javascript appends rather than prepends. Push is for stacks, not queues.
15th-Jul-2005 02:56 am (UTC)
they're clearly following what Perl does, since the Array prototype also defines .shift() and .pop()

and it's still a stack; it just grows the other way (except since these aren't really arrays in any kind of physicalmemory sense, there isn't really any "way" here, but anyway...)
15th-Jul-2005 06:18 am (UTC)
Well, it's a stack in that .pop() pops off the end, but if you iterate over the stack it would go from bottom to top, which is weird. (Unlike Lisp push/pop.)
This page was loaded Aug 21st 2017, 6:00 pm GMT.