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.

Friday, May 7, 2010

Update: Lucid Lynx on the HP Mini 210

Bye Bye Koala, Hello Lynx
Some time ago, I installed Koala on my HP Mini 210. Shortly after the next Ubuntu version "Lucid Lynx" was released. So I upgraded in hopes that some the hardware would be better supported. So I figured I should write a quick update about how things are.

Improvements
Bluetooth
Bluetooth works consistently now even after many suspend/resume cycles. This is definitely an improvement for people who need Bluetooth enabled peripherals. 

Remaining Problems
No Multitouch
Unfortunately there still doesn't seem to be touch pad drivers which support multi-touch properly. Normally I wouldn't care much. But the way the Mini's touch pad is designed makes this a bit of an annoyance. The left and right mouse buttons are embedded in the touch pad. This means that the cursor jumps around the screen every time I touch the pad with a second finger. Unfortunately this happens all the time when I point and click. 

Short Battery Life
Sadly, the one improvement I was hoping for hasn't happened. Even relying on powerttop to reduce the power consumption, the battery life is still only about two thirds of what it should be. I also figured out that shutting down the wireless controller before suspending, reduces power consumption while the netbook is suspended.

Conclusion
So that's that. Lucid's hardware support has improved compared to Karmic. That and the new look and additional features speak for an upgrade.