Saturday, August 7, 2010

Emacs

I'm one of the few people who converted from vi to Emacs (FAQ/Emacswiki). I've been using vim for years, but after a couple of days with Emacs, I don't think I will use vim  for anything other than simple one line changes to config files.


The reason I like Emacs so much is, because it really is a geek toy. Sure, you can also do some serious text editing with it, but mostly I just love to tweak it and make it do silly things. Which is easy as it neatly integrates into the operating system - as long as it's a *nix system. I went through some pains to get even basic stuff like spell checking working on Windows. Also I think I didn't Tramp setup correctly, which I expect to be a breeze on *nix. Mainly because all those "standard" tools ain't standard on Win. Anyway it's still fun.


A couple of features I like about Emacs
  • Buffers
    Buffers work like tabs in other editors. However, they can contain more than only files. They can also hold scratchpad areas, shells, output from running processes, and anything else Emacs can access. It is extremely convenient to have all output (e.g. from a compiler) saved in a buffer. Or how many times did you have to redirect shell output to a file? You can run a shell in Emacs instead of a terminal. And the best thing is that most shells understand Emacs' navigation keys.
  • Kill Ring
    Works like the clip board. Well actually, it works like a cyclical buffer of 30 clip boards. So you can copy multiple words, lines, regions and still paste them 15 minutes later. How many times did you wish for a second clip board? Now you have 30.
  • Mark Ring
    Every time you take diversions (e.g. by searching, or pressing M-< or M->), Emacs uses the mark to save your previous position, kind of like sticking your finger behind one page of a book while you go to glance at another page.
    More information about the mark ring.
  • Tramp
    Transparent file access through Tramp. Tramp can open local or remote files through various protocols like ftp or ssh and it can also open shells. Although I still need some time to get used to the syntax - seems like I always forget an semicolon or slash etc.
    More information about Tramp.
  • Version Control
    Transparent version control (be it CVS, SVN, Git etc) through the VC package. The commands are all the same regardless of what version control system. 99 out of a 100 times you'll either want to update, commit or add files - Emacs is more than capable to do that. 
    More information about VC.

Sunday, August 1, 2010

C++ Template Metaprogramming

Template Metaprogramming
In a recent post I admired the functional programming features (among many other features) that the BOOST libraries add to the C++ runtime system. Well guess what there is a pure, lazy functional programming language looming in C++'s template system! That means all the features you know and love from Haskell.


I've been reading "C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond". It outlines how can make use of metaprogramming features in your programs. 


A Simple Example
This is an introductory example from the book. Let's say you want to conveniently express binary numbers in your code. You could write a conversion function like this:


/*
 * general case
 */
template<unsigned long N>
struct binary
{
    static unsigned const value =
    binary::value<N/10> * 2 + N%10;
}


/*
 * special case, invoked to terminate recursion
 */
template<>
struct binary<0>
{
    static unsigned const value = 0;
}


In your code you could then write something like the following


/*
 * the binary expression is converted to 42 at compile time
 */
int fortytwo = binary<101010>::value;


If you're familiar with functional programming this should look familiar to you. And even if you don't know FP, the example shouldn't be too complicated to grasp. When a binary expression is compiled, the general binary struct is invoked recursively until the remainder is zero, at which point the so called template specialization is called which terminates the recursion.


Even from this simple example I think you can see potential uses Metaprogramming, especially when it comes to crafting domain specific languages for your application. The book dedicates a whole chapter to the design of domain specific embedded languages.


BOOST Support
BOOST also provides the metaprogramming library (mpl), which is a template metaprogramming framework of compile-time algorithms, sequences and metafunctions. mpl is also extensively used in the book. Another objective of the mpl is reduce the syntactic noise the templates produce. Template syntax is becoming really messy really quickly if expressions grow just little complex.