
What is the coolest thing you can do in ten lines of code? - sadiq
http://stackoverflow.com/q/811074/1288
======
rokhayakebe
I don't about you guys but when I did

0) Open notepad 1) Write <html>My name is Ely <img src=''></html> 2) CTRL + S
as index.htm 3) FTP to server 4) Then went to site.com/

That's when I knew I was going to be an amateur programmer for life. Yeah,
yeah I know, it's HTML. But that gave me the courage to later jump into
server-side languages.

~~~
sp332
If we could teach this simple process in school, we would inspire tons of
people to program little things for themselves. That would be awesome.

------
charlesap
Is 30 close enough to 10?

I got a job with 30 lines of code appended to my resume, with a screenshot of
the resultant output.

Perhaps the first two pages had something to do with it but I was told that
the third page was what stood out.

This was a sysadmin position, the code includes bash, sed, awk, grep, tcpdump,
netcat and gnuplot.

Hint to anyone who tries this sort of resume stunt: You WILL be asked what the
code does.

Just the code: <http://bbookkss.com/CharlesPerkinsCV3of3Pages.pdf>

~~~
moe
That's indeed an interesting example because, by any standard, this is
_terrible_ code.

Yet for a sysadmin position it still provides valuable insight into your a)
pain threshold, b) familiarity with the tools, c) willingness and ability to
bend reality for the boss.

I'd just add a big disclaimer that this kind of code only happens in
emergencies.

(disclaimer: I'm guilty of these gnuplot hacks myself and they always stick
around way longer than they should...)

~~~
cydonian_monk
> They always stick around way longer than they should...

But isn't that true for all 'terrible' code? Most of the elegant and well-
formed code I write ends up getting butchered for the sake of 'features,'
while the quick hacks that 'just work' stick around forever.

------
huhtenberg
Back in real-mode x86 times the coolest thing could've been done in 2 lines

    
    
      CLI     ; Disable interrupts
      HLT     ; Halt CPU until next interrupt
    

This hung the machine dead.

~~~
gsivil
This is not cool in my book.

~~~
huhtenberg
... nor is downvoting nerdy posts on HN of all places :)

------
danso
<http://stackoverflow.com/a/811081>

This person claims that simple variable usage and stdout should be all that is
needed to impress. While I studied enough low-level software development to
still be impressed by an adder circuit, I just don't think this will impress
enoug of the people we NEED to convert. Just about everyone takes it for
granted that when they log onto FB, they will see their name...and that doesnt
seem like "programming" to them

------
biot
Less than 10 bytes of code on an Apple II:

    
    
      ] CALL -151
      * 300: AD 30 CO 20 ED FD 4C 00 03
      * 300G
    

Enjoy! For bonus points, get this down to 8 bytes.

~~~
mkramlich
I used to do that kind of hacking in the 80's. I recognized the CALL-151. I
have an old Apple II+ in the other room I'm tempted to try firing up just to
solve this mystery.

~~~
cschneid
For people who didn't hack apple IIs, can you expand on what's going on there?

~~~
mmphosis
; enter the machine language monitor

    
    
      CALL -151
    

; toggle the speaker and load the accumulator with the "floating bus" value--
the byte last read by the video refresh controlled by the video scanner.

    
    
      0300-   AD 30 C0   LDA $C030
    

; print the accumulator as a character

    
    
      0303-   20 ED FD   JSR $FDED
    

; repeat (loop by going to 0x300)

    
    
      0306-   4C 00 03   JMP $0300
    

; run it! (0x300 Go; just like G in your debugger)

    
    
      300G
    

Running this will display random characters and make noise.

------
ColinWright
A colleague of mine wrote a spreadsheet in one line (253 bytes) of BASIC. I
wonder if I still have it somewhere ...

~~~
RodgerTheGreat
That's pretty interesting. What features did it offer? Just a grid of cells,
or some crude facility for summing/counting rows and columns as well?

~~~
ColinWright
It was a grid of cells. Each cell could contain a constant or a formula,
strings or numbers. You selected the cell to change with a mouse, and it
recomputed once per click on a button. In essence it was fully functional,
just lacking file load and save.

It didn't have charts or graphics, but the cell functionality was pretty
complete. Using "EVAL" meant you had access to any function in the underlying
language.

I'll see if I can dig it out.

------
SeanLuke
This is sorta evil. But was fun to write tonight.

    
    
        ;; run this on SBCL
        (sb-ext:unlock-package :common-lisp)
        (let (syms) (do-symbols (s :common-lisp) (if (fboundp s) (push s syms)))
          (labels ((worm (sym)
        	     (let ((next (elt syms (random (length syms))))
        		   (old (symbol-function sym)))
        	       (format t "~%Worm attacks ~a, next target is ~a!~%" sym next)
        	       (setf (symbol-function sym)
        		     (lambda (&rest args) 
        		       (when next (worm next) (setf next nil)) (apply old args))))))
          (worm '+)))
        ;; now try adding some numbers (and so on)
    

Long ago I also wrote a quine-based payload version of the above, moving
towards doing the same for NewtonScript. But I've long since lost it.

------
rwmj
Maybe not cool, but practical, I'd like to nominate some OCaml bitstring
matching code:

    
    
        let bits = Bitstring.bitstring_of_file "data" in
        bitmatch bits with
        | { width : 5;
            data : width } ->
            printf "width %d data %Ld\n" width data
    

It reads the first five bits of the file, interprets these bits as a number,
then reads the next 0-31 bits from the file (note: not aligned, the first 3
bits come from the first byte in the file).

The nice thing is that if everything can be shown at compile time to be
aligned to byte/word boundaries, then it all turns into relatively efficient
calls to C.

There are some more realistic examples on the web page:

<https://code.google.com/p/bitstring/>

------
RodgerTheGreat
Just about any Forth Haiku would probably qualify. Here's a nice Mandelbrot
set (not my code):

    
    
      : z^2 2dup z* ;
      : layer push push z^2 pop pop 2dup push push z+ pop pop ;
      : many layer layer layer layer layer layer layer layer layer layer ;
      : len dup * swap dup * + ;
      : mant y 0.7 - 3 * x 0.5 - 3 * many drop drop len dup 1 < * ;
      0 0 mant
      0.1 0.1 mant
      0.2 0.2 mant
    

([http://www.forthsalon.appspot.com/haiku-
view/agpmb3J0aHNhbG9...](http://www.forthsalon.appspot.com/haiku-
view/agpmb3J0aHNhbG9ucg0LEgVIYWlrdRjpgQIM))

------
ColinWright
Coming late to the party, but Simon Tatham's .sig is pretty cool:

    
    
        for k in [pow(x,37,0x13AC59F3ECAC3127065A9) for x in [0x195A0BCE1C2F0310B43C,
        0x73A0CE584254AB23D5A0, 0x12878657EA814421CC92, 0x7373445BB3DA69996F4A,
        0x77A7ED5BC3AA700E80B2, 0xE9C71C94ED87ADCF7367, 0xFE920395F414C1A5DB50]]:
        print "".join([chr(32+3*((k>>x)&1))for x in range(79)]) # <anakin at pobox.com>
    

It won't be completely obvious to most people why this is cool, but it's
effectively RSA-signed.

------
inoop
Viznut's classic signature:

    
    
        #include <math.h> /* outputs 8 kHz 8-bit unsigned pcm to stdout */
        main(v,i,z,n,u,t){for(v=-1;;)for(n=pow(/* gcc -lm sig.c;./a.out>/dev/dsp */
        1.06,"`cW`g[`cgcg[eYcb^bV^eW^be^bVecb^"[++v&31]+(v&64)/21),i=9 99;i;putchar(
        128+((8191&u)>i?0:i/8)-((8191&(z+=n))*i-->>16)))u+=v&1?t/2: (t=v&6?t:n/4);}

------
psykotic
You can generate the Sierpinski pattern in a lot less code than posted:

    
    
        iterate (\xs -> zipWith xor ([0] ++ xs) (xs ++ [0])) [1]

------
eps
Mandelbrot fractal set viewer in _brainfuck_ would fit the bill nicely. It's
not 10 lines exactly, but reflowing the BF code with no loss of clarity is not
a big deal :)

[http://esoteric.sange.fi/brainfuck/utils/mandelbrot/mandelbr...](http://esoteric.sange.fi/brainfuck/utils/mandelbrot/mandelbrot.b)

------
spc476
In Lua:

    
    
      function Y(f)
        local function g(...) return f(g,...) end
        return g
      end
    
      print(Y(function(rec,x)
                if x < 2 then
                  return 1
                else
                  return x * rec(x-1)
                end
               end)(5))

------
fauldsh
I'd have said that for impressing people with small pieces of code something
like SecondLife scripting would be ideal. You can manipulate objects in a 3d
environment using simple code. Who doesn't enjoy programming a gun?

~~~
statictype
Also COM scripting on Windows (and presumably Applescript on the Mac).

Sure there's a lot of special keyword magic involved, but nothing will get
people more excited about programming than seeing how they can easily automate
their routine tasks to make their life easier - that's what programming is
supposed to be about anyway.

~~~
rmgraham
You could probably stuff Node into this category too, if your target is the
type to be impressed with writing their own basic web server in 10 lines of
code.

------
bitcracker
Conways game of life animated in APL:

[http://uk.youtube.com/watch?v=a9xAKttWgP4&fmt=18](http://uk.youtube.com/watch?v=a9xAKttWgP4&fmt=18)

I didn't count the number of lines but I guess it's even less than ten.

