updated sfgate comment stripper

Mar 11, 2009 17:14

http://www.tursom.org/remove_sfgate_comments.user.js

It's still a greasemonkey script. And I feel all cool because I used map() instead of iterating over an array.

Leave a comment

Heh. zeekar March 15 2009, 00:49:58 UTC
Just the other day someone was accusing me of having written some bad code. I hadn't, but because it used "map", they assumed I wrote it. map is cool.

...though it'd be cooler if you used anonymous functions instead of named ones. If you're only ever going to call it once, and from map, then just do

my_array.map( function(item) { do stuff with item } )

:)

Reply

Re: Heh. tmaher March 15 2009, 06:16:09 UTC
Firefox 3 even has reduce(), apparently. My nipples are perky! I don't like that their implementation of Arrays seems to make shift()/unshift() seem to run in O(n).

My personal rule-of-thumb is that if it takes more than two lines, don't make it a lambda function. I claim that as a legibility and style decision more than anything else.

Reply

Re: Heh. zeekar March 15 2009, 12:03:25 UTC
Javascript arrays are just hashes with numeric keys, so yeah, shifting up or down requires changing all the keys.

Go go gadget functional programming. One of many reasons I dislike Python (which is Javascriptish in many ways) is Guido's active discouragement of functional idioms...

Reply

Re: Heh. tmaher March 15 2009, 17:20:08 UTC
See, I think having to explicitly change all the keys indicates a lack of either priority on the part of the implementers. "array" is just an interface to a data structure that supports a pile of operations (access child nodes by numeric index, push, pop, shift, unshift, splice, join, map, reduce, etc).

I can easily imagine implementing a JS array as a lookup table with fixed size records, and pointers off to the actual data payloads. The actual keys don't exist anywhere - they're just translated into offsets into the lookup table. This makes push, pop, shift, and unshift all constant-time operations - you just move the last or first entry in the lookup table, respectively.

Now, that does introduce some issues with making sure you've got enough empty space before and after the lookup table to accommodate this. I think it's doable.

Reply

Re: Heh. zeekar March 15 2009, 23:44:44 UTC
Ah, but you misunderstand. The "arrays are just hashes with numeric keys" is not an implementation detail behind the scenes, it's the interface presented to the programmer. Aside from some tacked-on support (a magical "length" attribute), there's no difference - the indices are keys, which show up when you iterate over the keys, etc. The same is true in PHP. There are no non-associative arrays, only associative arrays where you happen to pick numbers for the keys.

Reply


Leave a comment

Up