News for hackers. Stuff that matters.
Re:What is the coolest thing you can do in ten lines of code? by rwmj on Saturday December 31, 2011 @04:55AM (#3410589)
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/

There are no comments on this comment yet.