Sunday, 29 May 2011

Some thoughts on the iPad 2 actual feel

After using the demo iPad 2s in various shops I decided to take plunge and get one. I'd been reading so much stuff on the Internet especially RSS feeds on the iPhone that I wanted a larger screen and to be able to read longer articles in comfort.

This isn't a review but something I've noticed during my first week of ownership. Along with the iPad I also bought one of the new covers. When playing in the shops I'd only used the bare the metal version which was very pleasant to hold and manipulate.

However, I am now rather averse to using the iPad without the cover. I tend to hold the iPad in book mode and having the cover folded underneath the feel is of a natural fabric rather than metal. This has almost turned the iPad from a machine into something else: almost a book or another kind of natural object. It's an interesting sensory experience.

I occasionally remove the cover, usually to quickly reattach it which shows the other neat aspect which is how easily it self-aligns. The only thing it's missing is when folded underneath is the ability to 'stick' to the underneath so it does feel like a paperback folded back on itself with only a single page visible fighting to flatten itself.

Monday, 23 May 2011

First ever CodeProject article

I've been playing around with the WPF TreeView control trying to get it to draw a connected line org. chart style view of a tree.  I was going to write it up here but it got a bit long and formatting for the blog is a little tricky so I turned it into a Code Project article, my first one.  You can find it here.

Tuesday, 17 May 2011

Fluent Functors

I've been learning about BOOST Spirit; a C++ expression based compiler generator.  One of the examples is for a Roman Numeral parser.  This contained the following interesting code for pre-loading a symbol table.

struct ones_ : qi::symbols<char, unsigned>
{
    ones_()
    {
        add
            ("I"    , 1)
            ("II"   , 2)
            ("III"  , 3)
            ("IV"   , 4)
            ("V"    , 5)
            ("VI"   , 6)
            ("VII"  , 7)
            ("VIII" , 8)
            ("IX"   , 9)
        ;
    }

} ones;


So,
  • struct ones_ is a new class definition.
  • ones_() is the constructor
  • The call to add("I", 1) is a call to the member function add associating the string "I" with the value 1 by adding them to the symbol table.
There's nothing particularly strange there.  However, the continuation, i.e. add()()()()()... looked a little odd and puzzled me for a minute or so.  Then I realized add() must be returning *this and the other ()s were invoking the  parenthesis operator, i.e. in this case ones_& operator()(const char*, const int);

Following this little revelation to confirm the theory I constructed the following program which sums the numbers 1-6 printing 21.
#include <iostream>

class Func
{
private:
    int m_sum;

public:
    Func(const int n) : m_sum(n) { /* Empty */ }

    const int Sum() const { return m_sum; }

    Func& operator()(const int n)
    {
 m_sum += n;

 return *this;
    }

    static Func add(int n) { return Func(n); }
};

int main()
{
    std::cout << Func::add(1)(2)(3)(4)(5)(6).Sum() << std::endl;
}

All that's needed is a seed function, in this case add() which returns *this followed by operator()().  It would work fine without the named seed function using just operator()() but then it would loose a little meaning.

This isn't really that helpful and in most circumstances probably constitutes obfuscated code.  However it's certainly cute and where overloading is done fully and with meaning of which Spirit is a case in point then it becomes an effective and usable syntax.

As Fluent Programming seems to be on rise this another demonstration of it within C++, just like iostreams.  The added Syntactical Sugar provided by C++'s Functor mechanism makes for Fluent Functors.