Lurker News
Preferably an elegant snippet rather than an entire (well engineered) codebase.

Comments (380) 116 threads

Earlier comments
wumms#
Waiting for a keypress in ZX81 BASIC (maybe it's the opposite of elegant, but I remember it as the first time seeing a kind of easter egg in code):

    10 PAUSE 4E4
"PAUSE n

stops computing & displays the picture for n frames of the television (at 50 frames per second, or 60 in America). n can be up to 32767, which gives you just under 11 minutes; if n is any bigger then it means 'PAUSE for ever'.
A pause can always be cut short by pressing a key"[0]
(4E4 = 4*10^4 = 40000)
[0] http://www.worldofspectrum.org/ZX81BasicProgramming/chap19.h...

starmole#
That's a way to loaded and bait like question. Why not ask for "the most elegant snippet" instead?

There are beautiful code bases like DOOM source or SAT solvers that are like super models beautiful. Complete and hard to improve. Marvel at from a distance.
There is beautiful code in the libraries everybody uses all the time. All the lib* code. Somebody to marry. Discover the good beauty over time.
And then there is the beautiful snippet of clever code at the bar, quick to love, but one you get to know him it's much more trouble than he's worth.

manifestsilence#
This one is kind of contrived, but a work of mad genius. Mandelbrot code that resembles Mandelbrot, in Python 2:

    _                                      =   (
                                            255,
                                          lambda
                                   V       ,B,c
                                 :c   and Y(V*V+B,B,  c
                                   -1)if(abs(V)<6)else
                   (              2+c-4*abs(V)**-0.4)/i
                     )  ;v,      x=1500,1000;C=range(v*x
                      );import  struct;P=struct.pack;M,\
                j  ='<QIIHHHH',open('M.bmp','wb').write
    for X in j('BM'+P(M,v*x*3+26,26,12,v,x,1,24))or C:
                i  ,Y=_;j(P('BBB',*(lambda T:(T*80+T**9
                      *i-950*T  **99,T*70-880*T**18+701*
                     T  **9     ,T*i**(1-T**45*2)))(sum(
                   [              Y(0,(A%3/3.+X%v+(X/v+
                                   A/3/3.-x/2)/1j)*2.5
                                 /x   -2.7,i)**2 for  \
                                   A       in C
                                          [:9]])
                                            /9)
                                           )   )
</code></pre>
<a href="https://codegolf.stackexchange.com/questions/23423/mandelbrot-image-in-every-language" rel="nofollow">https://codegolf.stackexchange.com/questions/23423/mandelbro...</a>
neillyons2 replies#
This is very common in Elm but it blew my mind after years of programming in Python. In Python it is very easy to raise an IndexError by getting an element from a list by index that doesn't exist. eg.

    # python
    names = []

    names[0]  # <- raises IndexError
In Elm you are forced to always consider this possibility.

    # Elm
    names = []

    case List.head names of
        Just name ->
            name
        Nothing ->
            "empty"
MawKKe1 reply#
Idiomatic python:

  v = names[0] if names else "empty"
lgas#
Yes, but the point is you can forget the check in python but not in Elm.
dorfsmay#
This always felt weird to me given that Python does a good job with dict:

    d = {}
    assert(d.get("blah"), None)
I was impressed by Rust the first time I run into:

    let mut last;
      for i in &[1, 2, 3] {
          last = i;
      }
    println!("{}", last);

  >> borrow of possibly uninitialized variable: `last`

Load more comments