What will make you a better Elixir developer?

 

Practising, practising and practising. That’s it 🙂

So that’s what I did. During job interviews, you get these coding challenges, some are good, some are bad. I remember one: the bowling kata which is quiet fun.

Here is the description of the coding test:

Create a program, which, given a valid sequence of rolls for one line of American Ten-Pin Bowling, produces the total score for the game. Here are some things that the program will not do:

  • We will not check for valid rolls.
  • We will not check for correct number of rolls and frames.
  • We will not provide scores for intermediate frames.

Depending on the application, this might or might not be a valid way to define a complete story, but we do it here for purposes of keeping the kata light. I think you’ll see that improvements like those above would go in readily if they were needed for real.

We can briefly summarize the scoring for this form of bowling:

  • Each game, or “line” of bowling, includes ten turns, or “frames” for the bowler.
  • In each frame, the bowler gets up to two tries to knock down all the pins.
  • If in two tries, he fails to knock them all down, his score for that frame is the total number of pins knocked down in his two tries.
  • If in two tries he knocks them all down, this is called a “spare” and his score for the frame is ten plus the number of pins knocked down on his next throw (in his next turn).
  • If on his first try in the frame he knocks down all the pins, this is called a “strike”. His turn is over, and his score for the frame is ten plus the simple total of the pins knocked down in his next two rolls.
  • If he gets a spare or strike in the last (tenth) frame, the bowler gets to throw one or two more bonus balls, respectively. These bonus throws are taken as part of the same turn. If the bonus throws knock down all the pins, the process does not repeat: the bonus throws are only used to calculate the score of the final frame.
  • The game score is the total of all frame scores.

Examples:

  • [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10] should return 300
  • [9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9 ] should return 190
  • [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5] should return 150
  • [9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0] should return 90
I’ve put my answer to this problem in a Github Gist.

First steps with Elixir

Functional programming languages (FPL) are becoming more and more popular. Not sure if they are more and more used but at least they are more and more discussed on the web.

I’ve finally decided to take the time to learn one of these FPL. And there was the problem, which one to choose! There is so many of them.

After some reading and some stupid google query searchHaskell seems nice but a little bit too complicated to start with. So I looked at Clojure, Scala, and list goes and goes. But there is something I didn’t tell you about me, I’m a Ruby on Rails developer. And there is one FPL with a syntax close to Ruby based on a good FPL from the 80s: Elixir.

Elixir is a FPL based on Erlang with a nice and simple syntax close to Ruby. One another important thing made me pick Elixir, its getting started documentation, it’s just awesome. It took me some days to go through it. It’s a good piece of documentation with relevant examples.

I’m just at the first stage of discovering this interesting language. Some updates soon.

 

Gitweb configuration, the easy way

Ok if you have juste one git repository, git instaweb is for you. This command automatically generate the config to connect with a webserver installed on your server. I recommand you to try, it’s impressive as it’s simple. For me the best config is :

git instaweb -d webrick

Now if you have several git projects as I am, you want to use gitweb. In fact instaweb uses the gitweb script but configured for one specific repository. The script is in .git/gitweb/gitweb.cgi .

On Ubuntu, install the script with the command:

apt-get install gitweb

I’m going to assume you have apache2 and mod_perl installed. So in the config file

/etc/apache2/sites-enabled/000-default

add this config at the end of the VirtualHost block:

ScriptAlias /git “/usr/lib/cgi-bin/gitweb.cgi”
<Directory “/home/git/”>
Options Indexes FollowSymLinks ExecCGI
Order allow,deny
Allow from all
</Directory>

Now you just have to configure gitweb by setting the directory containing your git projects in the file:

/etc/gitweb.conf

Finally, restart apache and you should be able to browse your projects on this url:

http://localhost/git

XML to NSDictionary Converter

I found recently a nice lib easily converting XML to NSDictionary. The code is now on GitHub.

Here are the key ideas:

  1. XML elements map to key names in the dictionary
  2. Each element corresponds to a child dictionary
  3. Attribute key-value pairs are added to the element’s child dictionary
  4. Strings from text nodes are assigned to the child dictionary’s “text” key
  5. If an element name is encountered multiple times, the value of the element is set to an array of children dictionaries

For a more detailed presentation.

A detailed example:

The XML to parse:

//    <items>
//        <item id=”0001″ type=”donut”>
//            <name>Cake</name>
//            <ppu>0.55</ppu>
//            <batters>
//                <batter id=”1001″>Regular</batter>
//                <batter id=”1002″>Chocolate</batter>
//                <batter id=”1003″>Blueberry</batter>
//            </batters>
//            <topping id=”5001″>None</topping>
//            <topping id=”5002″>Glazed</topping>
//            <topping id=”5005″>Sugar</topping>
//        </item>
//    </items>
//

The Code:

NSString *testXMLString = @”<items><item id=\”0001\” type=\”donut\”><name>Cake</name><ppu>0.55</ppu><batters><batter id=\”1001\”>Regular</batter><batter id=\”1002\”>Chocolate</batter><batter id=\”1003\”>Blueberry</batter></batters><topping id=\”5001\”>None</topping><topping id=\”5002\”>Glazed</topping><topping id=\”5005\”>Sugar</topping></item></items>”;

// Parse the XML into a dictionary
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];

// Print the dictionary
NSLog(@”%@”, xmlDictionary);

The result

// xmlDictionary = {
//    items = {
//        item = {
//            id = 0001;
//            type = donut;
//            name = {
//                text = Cake;
//            };
//            ppu = {
//                text = 0.55;
//            };
//            batters = {
//                batter = (
//                    {
//                        id = 1001;
//                        text = Regular;
//                    },
//                    {
//                        id = 1002;
//                        text = Chocolate;
//                    },
//                    {
//                        id = 1003;
//                        text = Blueberry;
//                    }
//                );
//            };
//            topping = (
//                {
//                    id = 5001;
//                    text = None;
//                },
//                {
//                    id = 5002;
//                    text = Glazed;
//                },
//                {
//                    id = 5005;
//                    text = Sugar;
//                }
//            );
//        };
//     };
// }
//

Install Rmagick on Mac OS X

I like to use the standard ruby tools when developing with Rails. So if I have a gem to install, I won’t use ‘apt-get install’ or ‘port install’, I will use ‘gem install’. To install Rmagick the command is:

sudo  gem install rmagick

And the result is :

Building native extensions.  This could take a while…
ERROR:  Error installing rmagick:
ERROR: Failed to build gem native extension.

/usr/local/bin/ruby extconf.rb
checking for Ruby version >= 1.8.5… yes
checking for gcc… yes
checking for Magick-config… no
Can’t install RMagick 2.13.1. Can’t find Magick-config in /usr/local/bin/:/opt/local/bin:/opt/local/sbin:/usr/local/mysql/bin/:/usr/bin:/bin:/usr/sbin:/sbin

*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
–with-opt-dir
–without-opt-dir
–with-opt-include
–without-opt-include=${opt-dir}/include
–with-opt-lib
–without-opt-lib=${opt-dir}/lib
–with-make-prog
–without-make-prog
–srcdir=.
–curdir
–ruby=/usr/local/bin/ruby

Gem files will remain installed in /usr/local/lib/ruby/gems/1.8/gems/rmagick-2.13.1 for inspection.
Results logged to /usr/local/lib/ruby/gems/1.8/gems/rmagick-2.13.1/ext/RMagick/gem_make.out

It seems there are dependencies not installed. To solve this problem, I will use macport and install the necesary libs with the command:

sudo port install tiff -macosx imagemagick +q8 +gs +wmf

After a bunch of compilation, I relaunch my gem command :

sudo gem install rmagick

And “Voila!” it works. Now have fun with RMagick functionalities.

Killer tool to develop on iPhone: ack-grep

When developing for iPhone, the IDE coming in mind is XCode and it’s a good thing cause XCode is a good piece of software. I use XCode everyday but I never found the search functionality good enough. That’s why I switched to ack-grep. It’s a little perl script way faster than grep or any other search functionality. And the result presentation is clear and efficient.

Recently, I had to work on an complex iPhone application developed by another company. ack-grep helped me a lot to dig in the hairy code. Since I’m using this tool I can’t work without it.

The installation is easy, it’s worth a try.

iPhone essential performance tools list

Once your iPhone application is done, it is good to check the possible memory leaks who could cause your application to crash. So here is a the list:

  • Leaks application that can be found in Xcode: run -> Start with Performance Tool -> Leaks
  • Apple’s Instruments utility that can be found in /Developer/Applications/Performance Tools
  • Clang, an static code analyzer to find bugs in objective-c code: http://clang.llvm.org/StaticAnalysis.html