Well, in honor of Dropbox’s new Linux Client (note: you must have an account to be able to see the Linux client page), I’ve decided to give out (at least) 3 invitations to Dropbox’s private beta for (what I deem to be) the cleverest sub-ten-line code snippets posted. If there are many outstanding entries, I’ll be happy to give out more than 3 though.
To give you some idea of what I’m talking about take a look at these examples:
Schwartzian Transform in Perl:
1 2 3 4 | @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, foo($_)] } @unsorted; |
A Common Lisp Quine (extra whitespace for readability):
1 2 | ((lambda (x) (list x (list 'quote x))) '(lambda (x) (list x (list 'quote x)))) |
I have a few better examples in mind too (clever regexes, pointer magic, etc), but I don’t want to steal anyone’s real entry
The code you submit doesn’t have to be of your own invention, but bonus points if it is. Any language and any topic (obfuscated code, polyglots, numerical methods, etc.) are fair game - if I don’t understand it, I’ll ask someone smarter than me for help.
I’ll only consider comments submitted by midnight Wednesday, and I’ll announce winners within a day of that.
If you feel like entering, please be sure to leave some way for me to contact you so I can get your invite to you if you win.

Comments 8
Is this Dropbox service very useful to programmers or something? I can’t find an explanation on the website.
Posted 07 Sep 2008 at 7:56 pm ¶It’s basically an easy to use interface on a revision control system (think subversion) with 5GB of free storage.
Posted 07 Sep 2008 at 8:16 pm ¶;Enjoy the probably screwed up indentation
;;;Polyvariadic Y combinator returning multiple values (in Scheme)
(define (Y . fs)
((lambda (x) (x x))
(lambda (x)
((lambda (g) (apply values (map g fs)))
(lambda (f) (lambda a ((lambda (h) (apply h a))
(call-with-values (lambda () (x x)) f))))))))
;;Examples
((Y (lambda (f) (lambda (x) (if (zero? x) 1 (* x (f (- x 1))))))) 5)
; => 120
(call-with-values
Posted 07 Sep 2008 at 8:22 pm ¶(lambda () (Y (lambda (_ o) (lambda (x) (if (zero? x) #t (o (- x 1)))))
(lambda (e _) (lambda (x) (if (zero? x) #f (e (- x 1)))))))
(lambda (e o)
(list (e 10) (e 11) (o 101) (o 102))))
; => (#t #f #t #f)
Java enums. Pretty damn useful things.
public enum Direction {
North(0,1), South(0,-1), East(1,0), West(-1,0),
Northeast(North, East), Northwest(North, West),
Southeast(South, East), Southwest(South, West);
public int x,y;
Direction(int dx, int dy){
x=dx;
y=dy;
}
Direction(Direction… directions){
this(0,0);
for(Direction d: directions){
x+=d.x;
y+=d.y;
}
}
}
This is what I pull out whenever someone wants to know what’s better about Java 1.5 vs 1.4. Northwest(North, West) is the closest I’ve come to poetry in Java.
Posted 07 Sep 2008 at 10:57 pm ¶I guess I got a little on this last year, but here’s something I had to do for school: a graph coloring algorithm in Prolog:
sudograph(_Nodes, Constraints, Colors) :- con(Constraints, Colors).
color(X, Colors) :- member(X, Colors).
gencolor([],_).
gencolor([E|R], Colors) :- color(E, Colors), gencolor(R, Colors).
diffcol([E, E2|L]) :- \+ member(E, [E2|L]), diffcol([E2|L]).
diffcol([_]).
con([], _Colors).
con([A|L], Colors) :- gencolor(A, Colors), diffcol(A), con(L, Colors).
I guess it can’t really be called “elegant,” given that it’s Prolog, but it’s terse.
Posted 09 Sep 2008 at 1:04 am ¶I’m always fascinated with simple yet clever things (not necessarily having to be useful), so here comes my favorite line of code in that sense:
// zero-out any number using XOR
Posted 10 Sep 2008 at 3:46 am ¶x ^= x;
Update and announce the winners (or lack thereof). This isn’t arantaonceinawhile; its arantaday!
Posted 21 Sep 2008 at 6:22 pm ¶My dead-simple graph colouring method (I’m using the Ruby GRATR library for the base classes):
def colour! first_colour = :a
each do |vertex|
adjacent_colours = adjacent(vertex).map { |v| vertex_label v }
colour = first_colour
colour = colour.next while adjacent_colours.include? colour
vertex_label_set vertex, colour
end
end
The ‘colour = colour.next while adjacent_colours.include? colour’ was always the favourite part of my PhD code.
Posted 30 Sep 2008 at 9:09 am ¶Post a Comment