Fun with the RPN calculator in Ioke

August 6th, 2010 3 comments

In this article, Cédric Beust explains how to port a RPN calculator from Haskell to Fantom. It could have made me want to code more in Haskell or go discover Fantom. Not at all. Reading the article, I felt the urge to code the same algorithm with Ioke. You know, this language I discovered through a MasterMind Kata a while ago.

Here is the Fantom code:

foldingFunction := | Int[] n, Str p -> Int[] | {
  echo("n:" + n)
  switch(p) {
    case "*" : return n.push(n.pop * n.pop)
    case "+" : return n.push(n.pop + n.pop)
    case "-" : return n.push(n.pop - n.pop)
    case "/" : return n.push(n.pop / n.pop)
    default: return n.push(p.toInt)
  }
};
"8 1 2 + 5 * +".split.reduce([,], foldingFunction)

Here is my first try with Ioke:

rpn=method(sum, x,
  case(x,
    "+", [sum pop! + sum pop!],
    "*", [sum pop! * sum pop!],
    "-", [sum pop! - sum pop!],
    "/", [sum pop! / sum pop!],
    [x toRational]
  ) + sum
)
"8 1 2 + 5 * +" split fold([], sum, x, rpn(sum, x)) println

Quite neat. The code looks a lot like the Fantom code or even the Haskell code/
Now let’s use the power of Ioke and remove duplication by defining rpn method on List itself :

List rpn = method(x,
  append!(case(x,
    "+", pop! + pop!,
    "*", pop! * pop!,
    "-", pop! - pop!,
    "/", pop! / pop!,
    x toRational
    )
  )
)
"8 1 2 + 5 * +" split prepend!([]) fold(rpn) println

I like that one better. We could remove some more duplication at the expense of readability, because, you know, there must be another way to convert “+” to , “*” to * and “-” to -:

List rpn = method(x,
  append!(case(x,
    or("+","*","-","/"), Message fromText(x) appendArgument(pop!) sendTo(pop!),
    x toRational
  ))
)

Here we used reflection, but we could use eval method and regexp:

List rpn = method(x,
  append!(if(x !~(#/[-\+\*\/]/),x toRational,Origin eval("#{s pop!}#{x}#{s pop!}))
)
"8 1 2 + 5 * +" split prepend!([]) fold(rpn) println

Ok, now let’s try to recall those days I was an expert in Perl, and compress this to the max:

r=fn(e,e split fold([],s,x,s append!(if(x !~(#/[-\+\*\/]/), x toRational,
Origin eval("#{s pop!}#{x}#{s pop!}")))))
r("8 1 2 + 5 * +") println

This one, I’m not really proud of. Should I?

Tags: , ,

Technical retrospective after 2 years at Algodeal

July 16th, 2010 Comments off

For two years I’ve been working at Algodeal. Two great years full of fun and challenges, going back to full-time developer after 4 years of technical consulting and agile coaching. Being the CTO at Algodeal, I thought that hiring a small team of talented people and being an equal part of this development team would be fun. It is! It worked for me at Adesoft. It worked again.

If I had to do a technical retrospective, here’s what I’d say:

There are great tools for java developers out there. Want to know my top 4?

Git: I’ve said it millions of times this year: you have to learn git. No excuse. Using a different VCS would really hurt now. Using git everyday for 2 years was also a good reason to make a lot of presentations to various companies, users groups and conferences. (Even more to come later the year) If you need to be any more convinced, go check out how git bisect could save your day.

Mockito: The other tool dear to my heart. Other mock framemorks made it clear that I should stay away from mocks and stubs, except for custom-made ones. Now, this all changed. I’m pretty sure that this kind of tool can make your tests quite redeable and super easy to refactor. A mindset change is all it takes. I got rid of most custom stubs, fakes and builders I used to write. Slides in french.

Google Collections, aka Google Guava: Ever wanted to write pseudo functional code in Java? This is easy with Functions and Predicates and a little bit of glue in between:

List<String> months = Arrays.asList(
    "January", "February", "March", "April", "May", "June", "July", "August",
    "September", "October", "November", "December");

String juneToJuly = with(months).only(startsWith("J")).exclude("January")
    .to(UPPERCASE).join(" to ");

assertThat(juneToJuly).isEqualTo("JUNE to JULY");

Nice isn’t it? Guava is full of other surprises, Suppliers, Futures, Preconditions, to make code faster and more reliable.

Infinitest: At first, there was JUnitMax, written by Kent Beck himself. An eclipse plugin that would run the full test suite for every change you make in the code base. It really changed the way I TDD. I loved this tool, I talked about it to everybody, until Kent Beck decided to discontinue the tool (guess what, one year after, he changed his mind). Enters Infinitest: better, faster, stronger. If you do TDD and don’t use a continous testing tool, you really miss something fundamental. Tools are mature enough for you to get onboard.

Team of 5 or 6 is perfect size:

It’s incredible how fast a small team can go. I’ve been in bigger teams and it’s so hard to keep everybody informed, and up to date. It’s so hard that you don’t do it. A team of 5 is made of enough opposite points of view and yet is quite easy to focus as a one. Sure a few expertise remain not shared by everyone, but with a team this size, everybody knows what he doesn’t know. And when it becomes depressing, he learns. In a large team, you don’t even know what you don’t know.

Build and test the whole thing fast. I mean fast:

This one I’m very proud of. From the beginning, we took good care of our test suite and tried to keep it as fast as possible. By fast, I mean under 4 minutes for the full build, unit tests, integration tests and functional tests. Guess what is the biggest benefit you get for free, trying to keep a quick build? You gain to challenge any business and technical decision: Will it be easy and quick to test? Will the tests be stable (anybody testing ajax)? If the answer is NO, then is the feature really useful for the users? Can it be done a simpler way. Turns out that if it can be easily/quickly tested, it can be easily/quickly developped, easily/quickly deployed and easily/quickly used.
Not always true of course, but often enough for this to become one of our hard rules.

It even lets us do unbreakable serverless continous integration. Wanna try?

Challenging the architecture often is a good habit:

It forces you to choose an architecture easy to deploy and easy to get rid of. Eg. We got rid of Voldemort in one day. That was super easy since it’s API is made of thow methods get(key), put(key, value). Compare that to JDBC API. If you want the full story, the data that we now store on HDFS, was first stored on a local filesystem, then on a remote filesystem, then on Voldemort.
An architecture that you cannot refactor or get rid of easily might not be worth setting up in the first place.

Hope this retrospective will help you and your team. Any feedback is welcomed.

Mon programme pour USI2010

May 6th, 2010 Comments off

Hier soir, Octo recevait presque tous les speakers qui animeront une présentation lors de la conference USI 2010 les 1 et 2 juillet prochains. Tout d’abord, merci à Octo pour cette soirée très sympathique et plutôt drôle.

Université du SI

Université du SI

L’occasion était donnée a chaque speaker de présenter son sujet en une minute. Toutes ces minutes, mises bout à bout, feront un petit film de présentation des sujets en une heure. Ce film vous permettra de composer votre programme autrement qu’en lisant une page web ou un programme papier. Bonne idée non ?

Voici mes coups de coeur :

  • Keynote d’ouverture de Chris Anderson, éditeur en chef du magazine Wired. Son point de vue sur les évolutions de l’économie numérique en général et de la presse en particulier sera certainement enrichissant.
  • Ma présentation GIT une heure plus tard. Celle là, vous n’avez pas le droit de la manquer, je le prendrais personnellement…
  • L’inratable “Why, not how” par Neal Ford et Martin Fowler. Superbes speakers, sujet captivant.
  • The future of money” par Renier Lemmens, PDG de Paypal. Il y a là une réelle transformation déjà amorcée, il sera intéressant d’avoir le point de vue d’un acteur de poids dand ce domaine.
  • Avaler la pilule rouge, recracher la pilule bleue” par Guillaume Duquesnay, coach Octo.
  • Puis “L’histoire de l’informatique” par Philippe Nieuwbourg, Directeur du musée de l’informatique.
  • Et pour finir, une belle keynote de clôture par Juan Enriquez : l’Homme va-t’il prendre le contrôle de son futur ?

Enfin, ne soyons pas langue de bois (pas mon genre), la keynote de Bernard Stiegler du mardi matin devra être bien meilleure que sa présentation en 1 minute, soporifique à souhait.

Une bien belle conférence pour geeks et boss se prépare. J’en salive d’avance.

Tags: ,

Crush .png images at commit time with git hooks

March 28th, 2010 1 comment

If you are a little involved into writing web applications, you have to know Yahoo!’s YSlow and Google’s PageSpeed. These are two Firefox plugins to help you accelerate a web site.

One thing they will help you do is to reduce the size of all those png images to decrease bandwidth. Sure png images are lighter than gifs but still you can remove some fat out of them keeping quality constant. Take a look at Smush.it if you want to learn more.

There are tools that can do similar things on the desktop. Pngcrush is one of them. This is as simple as this:

pngcrush imageIn.png imageOut.png

or even better:

pngcrush -rem allb -brute -reduce imageIn.png imageOut.png

Now how can we automate that? If pngcrush was java-based, you could do it dynamically with a servlet filter or at build time using ant/maven. A more effective way to do it, is at commit time using git.

The idea is to crush each png image added to the project the first time it is committed or each time it is updated. Git can do that with a simple hook script that will be executed pre-commit.

#!/bin/sh
if ! which -s pngcrush
then
  echo "Please install pngcrush to reduce png images size before commit"
  exit 1;
fi

for file in `git diff --cached --name-only | grep ".png\$"`
do
  echo "Crushing $file"
  pngcrush -rem allb -brute -reduce $file ${file%.png}.new | grep "filesize"
  mv -f ${file%.png}.new $file
done

Get this code on Gist.

Have fun!

Tags: , , , ,

Git bisect might save your day

March 23rd, 2010 Comments off

The day maven ruined my day (again…)

Yesterday, I lost 2 hours because our Maven project at Algodeal wouldn’t build anymore. I couldn’t execute:

mvn eclipse:eclipse

nor run the full build.

With the help of a colleague, we found out that only a two steps build would do the trick:

mvn clean install -DskipTests;mvn eclipse:eclipse

Obviously maven is already not my best friend but yesterday I was just fed up.

How git is called to the rescue
Read more…

Tags: ,