As seen on slashdot, a fully functional P2P client in a 625 character perl script.
Who wants to take the first crack at golfing this down?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: P2P Golf: MoleSter
by dragonchild (Archbishop) on Dec 16, 2004 at 14:04 UTC | |
Update: This is what I just submitted back to the author. I think it ends up being 516 characters over 8 lines.
Being right, does not endow the right to be rude; politeness costs nothing. | [reply] [d/l] [select] |
by jonadab (Parson) on Dec 17, 2004 at 04:16 UTC | |
You did better than I did (_especially_ with optimizing sub a, and with getting rid of the Socket constants, which I knew was possible but didn't do), although you did miss a couple of opportunities... We also both missed the argumentless-close trick that someone else pointed out, although I would have to test that one to be sure it doesn't close the socket also (or that doing so doesn't change the operation of the script). Can someone confirm which close statements need their arguments and, for that matter, whether it's possible to eliminate any of the close statements altogether without breaking something? So I threw out my version and worked from yours update: again, and this is what I now have, weighing in at 508 bytes not counting line breaks (522 bytes with a full ASCII CRLF ending every line but the last, or 515 with Unix-style one-character newlines):
Note that this code is completely, utterly untested. "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings." — Pratico & Van Pelt, BBHG, p68 | [reply] [d/l] [select] |
by dragonchild (Archbishop) on Dec 17, 2004 at 14:24 UTC | |
The author is working on revamping the while() loop and moving to use read() instead of <> for the socket. But, I don't have that version, so I'll work with what I have.
This code is sooo completely untested. :-) The readable version ... Read more... (993 Bytes)
Update: After jonadab's suggestions and some reorganization, we save 11 characters (down to 459 over 6 lines):
Yes, there's a subroutine definition in the while-loop. "perl -MO=Deparse" tells me it's ok. Being right, does not endow the right to be rude; politeness costs nothing. | [reply] [d/l] [select] |
by jonadab (Parson) on Dec 17, 2004 at 15:16 UTC | |
by dragonchild (Archbishop) on Dec 17, 2004 at 15:57 UTC | |
| |
by bgreenlee (Friar) on Dec 16, 2004 at 18:32 UTC | |
You should be able to get rid of at least 4 more characters by getting rid of the " F" after the two "close F"'s. Without an argument, close will close the currently selected filehandle. The above is actually 7 lines if you get every line as close to 80 chars as possible. -b | [reply] |
by dragonchild (Archbishop) on Dec 16, 2004 at 18:34 UTC | |
True, but you gain characters by doing that. The reason for where the newlines are is to cut spaces from the final tally. You'll notice every newline is after a select, close, sub, or for. That's not coincedence. :-) Thanks for the tip on close(). Being right, does not endow the right to be rude; politeness costs nothing. | [reply] |
by eric256 (Parson) on Dec 16, 2004 at 14:27 UTC | |
The easiest way to test it is to run it twice, and have each instance run on a different port. Then they can communicate with each other. ___________ Eric Hodges | [reply] |
|
Re: P2P Golf: MoleSter
by bgreenlee (Friar) on Dec 16, 2004 at 16:30 UTC | |
I missed this post yesterday, as I was deep into writing my own perl P2P app, after someone posted the challenge to the Fun With Perl mailing list. Mine is slightly bigger (717 characters, including newlines), but still 9 lines. I worked under the assumption that any modules in the standard perl distro were fair game (hence IO::Socket) I tried to emulate the operation of the original python version as closely as possible. I did add one feature that i thought was essential, though: the ability to view files on the network without actually downloading them. I'm sure better golfers than I could knock this down a bit. Anyway, mine is used like so: Server: Client: Note that when entering a pattern, you need to escape the backslash. So to search for .mp3 files, the pattern would be \\.mp3$
-b | [reply] [d/l] [select] |
|
Re: P2P Golf: MoleSter
by blazar (Canon) on Dec 16, 2004 at 15:10 UTC | |
Of course I'm not the one "Who wants to take the first crack at golfing this down" but giving a peek into the first few lines I notice that there's plenty of room for golfing. In fact I'm only a mediocre golfer, but golfers for example generally pop() rather than shift(). Or, for example, C<undef$/;> takes one keystroke less than C<$/=undef;>. But then (e.g.) C<$/=$_> will do and is even shorter. Again, selecting a randomly picked line, I'm a big fan of the three-arguments form of open(), but C<open(F,'<',$_[1])> can be replaced by C<open F,pop>. As a side note, since we're talking golf here, well one doesn't generally care much about good programming practices in this context. But the author of the original code's systematic (&-form of) calling of subs clearly tells what his programming skills can be... | [reply] [d/l] [select] |
by dragonchild (Archbishop) on Dec 16, 2004 at 15:16 UTC | |
Being right, does not endow the right to be rude; politeness costs nothing. | [reply] [d/l] [select] |
by Fletch (Bishop) on Dec 16, 2004 at 19:03 UTC | |
You actually cannot use pop instead of shift in the first line unless you rearrange the argument list order. If there's no ordering dependencies between things (in this case &a and &i don't seem to depend on one being called before the others, and neither references $p) you can if you just reverse the order you pop rather than the argument order . . .
Update: D'oh, missed the @ARGV later on. Never mind me. | [reply] [d/l] [select] |
by dragonchild (Archbishop) on Dec 16, 2004 at 19:05 UTC | |
by blazar (Canon) on Dec 20, 2004 at 14:51 UTC | |
You actually cannot use pop instead of shift in the first line unless you rearrange the argument list order. The same goes for most of the subroutines.WOW! Thank you so much for having pointed out so: I would have never imagined myself... What I wanted to stress is that even for a mediocre golfer like me at a first glance the code, despite some evident conciseness still was largely suboptimal from this point of view for just applying a standard golfing technique like using pop() instead of shift() (and reversing the order of the arguments - pointing out the obvious, just to keep you satisfied) wherever applicable would have saved quite a lot charachters. But then it's well known that experienced golfers use any sort of more refined tricks. For example I can remember one case in which there were four arguments to take from the cmd line and it was practically mandatory to assign these to variables so they used something along the lines of this (since the golf rules did not require strictures or warnings): $$_=pop for a..d; You cannot use open F,pop; because there are 3 args coming in, not 2.Huh?!? The &x() syntax is actually the golfed version in several places, particularly &$3($2,$4,$');. Now, in others, it can be removed. But, I sincerely doubt it has anything to do with the author's skills as a programmer. Personally, I'm impressed with a P2P client in under 1000 characters.Well, indeed if we're talking symrefs then you're right. It may also save some chars in other situations. Generally with modern enough perls the &-syntax does not do what one expects, and as a matter of a fact unless there's a really good reason to adopt it (and indeed in golfing and obfuscation games there may be some well beyond "regular" ones), code that abuses it reveals that he/she who wrote it is still programming Perl4. I'm not sure if this is the case with this particular program, but this is the impression I got at a quick glance. | [reply] [d/l] |
by dragonchild (Archbishop) on Dec 20, 2004 at 15:06 UTC | |
by blazar (Canon) on Dec 21, 2004 at 14:39 UTC | |