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.

Wednesday, July 21, 2010

Programming Collective Intelligence

Programming Collective Intelligence is a new book from O'Reilly written by Toby Segaran.

Even though the subtitle read "building smart web 2.0 applications", it's about more than that. I'm less interested in building websites and all the more data mining of existing sources. And I was not let down.
 

Toby really manages to explain the underlying concepts in an accessible way. Which important because much of it is based on methods of statistical analysis. He covers discovering groups, searching, ranking, optimization, document filtering, decision trees, price models or genetic algorithms. The book explains how to implement Simulated Annealing, k-Nearest Neighbors, Bayesian Classifier and many more.

Throughout the book, Toby explores various other AI techniques, and explains clearly, how to implement them in Python. One nice touch at the end of the book, was to include a reference to modules used in the book, and to include small usage examples. I was quite pleased by Collective Intelligence, and I would recommend the book to any intermediate to advanced programmer who wants to learn more about AI, and also web specific applications of AI theory. Take a look at the table of contents (it does not list all the algorithms, tho).

All code in the book is written exclusively in Python. Don't worry if you're not familiar with Python. In the introduction there is a short description of the language and the code is usually well explained as well as commented. I use very little Python, but I was still able to follow the code listings.




Monday, June 7, 2010

Learning C++

C++
Recently I joined a new project at work. It's some application running on an embedded Linux platform. So it was decided that we would write it in C++. C++ is great for these kind of jobs, where you have limited space and processing power.

The only problem was, I had never worked with C++ before. Why was I even assigned to the team you ask. Well, I had previously been working on a related project for the same client. So I had a lot of domain specific knowledge that the other team members didn't have. Also I'm the resident UNIX guy so I would help them when their machines started acting up.

So there I was learning C++ as I went. Reading a lot of books by Scott Meyers and a ton of BOOST documentations. And after a couple of months I'm still a C++ rookie. Frankly, I didn't think it was a Language worth learning. I though it was outmoded and inferior to modern languages.

BOOST
The BOOST library is an amazing piece of software. I can't imagine where we would be without it. From Wikipedia:


"The BOOST libraries are aimed at a wide range of C++ users and application domains. They range from general-purpose libraries like the smart_ptrlibrary, to OS abstractions like Boost FileSystem, to libraries primarily aimed at other library developers and advanced C++ users, like the template metaprogramming (MPL) and DSL creation (Proto).
In order to ensure efficiency and flexibility, Boost makes extensive use of templates. Boost has been a source of extensive work and research into generic programming and metaprogramming in C++."
Most of all I was pleasantly surprised to see how many functional programming features is provides. I mean I use boost::bind, boost::function or boost::signals every single day. boost::bind allows you to create a callable object and bind to an arbitrary member function. It also allows partial function application and the like. There is also the BOOST lambda which allows you to use lambda expression just the way you're used from FP languages.


Valgrind
I've also installed Valgrind on our development machines. We generally use it to detect memory leaks and other memory management issues. Apart from suchmemory checks, it also features several profilers. But the coolest feature IMO is Helgrind, which can detect race conditions in multi-threaded applications and/or improper locking of resources. 

Thursday, May 27, 2010

To Infinity and Beyond

Sigfpe made the effort to collect many of his great blog posts he wrote over the years into a single, structured post. Basically writing an index. He covers Monads, Comonads, Fold, Unfold, the general Category Theory. I've been reading and rereading for weeks and there's still more to discover. Check it out here.