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

Comments (380) 116 threads

Earlier comments
practical_lem#
Pike's code should be studied in schools.
verall1 reply#
Someone once posted a C program here on HN, where each line, even the comments, seemed to "line up" in 6- or 8- character blocks with a space in between. It made the whole program look sort of like a table.

I felt it was sort of like poetry. I unfortunately no longer have a link to it, and once looked very hard but couldn't find it.
I would be extremely appreciative of someone else saw it and had a link. If I recall correctly it was a type of interpreter, I remember it having code for parsing.

verall#
I appreciate your help, but I feel like its unlikely to be in there since it wasn't really obfuscated - it was meant to be clear in some sense of the word and had a bunch of comments explaining it.

The comments sounded natural, but lined up so each Nth column was a space (or punctuation) all of the way down the code. None of the things I am seeing in that list seem to have quite the same layout.
There was some comment about it like "some people feel code should be beautiful to enjoy debugging" or something along those lines but I don't really remember.
I have been looking for a long time though and really wish I had saved it properly when I first saw it.

vbezhenar2 replies#
Bit tricks. Like

    public static int bitCount(int i) {
        i = i - ((i >>> 1) & 0x55555555);
        i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
        i = (i + (i >>> 4)) & 0x0f0f0f0f;
        i = i + (i >>> 8);
        i = i + (i >>> 16);
        return i & 0x3f;
    }
It was like magic for me when I encountered it first time.
soylentgraham#
Came here to say this, I first saw this in the book Hackers Delight which is full of 200X-me's mind blowing stuff. It really opened a door to writing (arb)GPU shaders where at the time if's weren't allowed and trying to cut branches from PS2/GameCube/Xbox code
112358132134551 reply#
# Permutations of a list:

- haskell:

  perms [] = [[]]
  perms xs = [ x:ps | x <- xs , ps <- perms ( xs\\[x] ) ]
- js: (using https://github.com/tc39/proposal-slice-notation for conciseness)

  const perms = xs => xs.length === 0
    ? [[]]
    : xs.flatMap((xi, i) => perms([...xs[0:i], ...xs[i+1:]).map(xsi => [xi, ...xsi])

# Cartesian product of 2 or n lists

- haskell:

  cart2 xs ys = [(x,y) | x <- xs, y <- ys]

  cartn :: [[a]] -> [[a]];
  cartn [] = [[]]
  cartn(xs:xss) = [x:ys | x <- xs, ys <- yss]
                        where yss = cartn xss
- js:

  const cart2 = (xs, ys) => xs.flatMap(x => ys.map(y => [x,y]));

  const cartn = (...args) => args.reduce((yss, xs) => yss.flatMap(ys => xs.map(x => [...ys, x])), [[]]);
  // or recursive
  const cartn = (xs, ...xss) => xss.length === 0
    ? xs
    : xs.flatMap(x => cartn(...xss).map(y => [x,y]))
numlock86#
Until now I didn't know about flatMap() and flat(). Thanks for this!

Load more comments