Wednesday, March 16, 2011

Mon vecu du seisme de Fukushima, depuis Tokyo

Je traduis en francais le contenu de mon precedent post. (Je n'ai pas d'accent sur mon clavier)

  • Je travaille a Tokyo dans un immeuble de 24 etages. Le 11 Mars, 14h46, j'etais au bureau au moment ou le seisme s'est produit. Nous en avions deja eu un le Mardi ou Mercredi precedent, j'ai donc pense qu'il s'agissait encore d'une autre secousse "habituelle". Puis ca s'est mis a trembler de plus en plus fort, de droite a gauche. C'est de loin le plus fort seisme que j'ai vecu en 10 ans de vie au Japon. Certains collegues ont commence a se refugier sous leur bureau. C'est la premiere fois que je vois ca, c'est dire la force de la secousse. Moi, je lacais mes chaussures, tout en gardant un oeil sur la porte de sortie, pret a bondir.
    La secousse devint plus faible (je ne peux pas dire au bout de combien de temps. Ca m'a paru assez long, entre 15 et 30 seconds peut-etre). L'immeuble tenait toujours debout, personne ne semblait blesse ici. Nous savions que, cette fois, ce n'etait pas une autre secousse "habituelle". Nous nous demandions ou le seisme s'etait produit. Tout le monde verifiait les dernieres infos sur le net. Premiere question qui m'est venue: rester ou sortir ? Nous attendions les instructions du centre de prevention des desastres. L'annonce arriva quelques secondes apres, nous informant de l'origine du seisme, de sa force, et de ce que nous devions faire. La consigne fut de garder son calme car nous etions en securite a l'interieur. Puis nous eumes plusieures repliques plus ou moins fortes. Une premiere, une deuxieme, une troisieme... L'immeuble tanguait a chaque replique. On se sentait comme sur un bateau, ce qui eut tendance a jouer un peu avec nos nerfs.
  • Puis une autre secousse assez forte, bien que moins forte que la premiere. J'ai dit a mes collegues que je sortais et qu'ils feraient mieux de venir. Nous sommes sortis, et attendions que les choses se calment. Il y avait beaucoup de monde dehors. Nous etions tous en train de verifier les infos sur nos telephones portables. Nous vimes ce qu'il s'etait passe au nord, et ce qu'il advint aux villes broyees par le tsunami. Il y eut des infos comme quoi le tsunami allait arriver sur Tokyo, bien que beaucoup plus faible.
  • Au bout d'une heure, nous sommes retournes au bureau. Bien sur, les ascenceurs etaient hors d'usage. Nous avons utilises les escaliers de secours pour remonter, et decouvrimes l'ampleur des degats dans notre immeuble : des fissures sur les murs et sur le sol. Cependant rien de serieux, bien qu'on en vienne a se demander si le batiment va toujours resister au cas ou un autre seisme puissant se declenchait.
  • De retour sur ma chaise. Il est presque 16h. Nous continuions de jeter un oeil aux infos, tout en travaillant. Tous les trains etaient arretes. Les lignes du metro egalement. Ca allait etre une longue journee avant de pouvoir rentrer. Certains collegues quitterent le bureau des que la sonerie de fin journee se fit entendre. Certains ont probablement du essayer de se trouver un chemin dans les gares bondees de monde. Certains ont decide de marcher jusqu'a chez eux, peu importe la distance. Trois heures ou plus de marche dans la nuit et le froid ne les ont pas arretes (une de mes collegues est arrive le lendemain matin a 4h30). En ce qui me concerne, j'habite beaucoup trop loin du bureau pour pouvoir marcher. J'ai decide de passer la nuit au bureau, a moins que les trains se remettent en marche. Ou a moins que les repliques sismiques ne soient trop fortes.
  • La nourriture ! Je n'y ai pas pense immediatement. C'est en voyant une photo d'un magasin ayant ses rayons vides que j'y ai pense. Une autre photo aussi, d'une femme portant quatre "cup ramen (nouilles)" dans ses bras. J'ai file en bas pour faire des achats. Il ne restait plus grand chose. Seulement des petites boites de "cup ramen". Les plus grandes etaient deja vendues. Plus de onigiri (riz), plus de obentou (boites repas), plus de pain. Heureusement, le peu qui restait me suffit largement.
  • Au bureau, il fait chaud. On a de l'eau chaude, les toilettes. Pas de quoi se plaindre compare a ceux qui vont devoir passer la nuit dehors ou dans les gares. Pendant la nuit, la East Japan Railway (compagnie de train de l'est du Japon) a annonce que ses trains bougeraient a partir de 7h du matin. J'ai decide de partir a 6h30 pour voir si pouvais prendre un train. J'ai deux choix pour pouvoir rentrer : l'un etant d'aller a la gare de Tokyo, et d'essayer d'attraper un des trois trains allant vers chez moi. L'autre choix etant de prendre la ligne Ginza du metro, d'aller jusqu'a Shibuya et de changer de train. Choix difficile. Le second est un plus long trajet. J'avais un mauvais presentiment a propos du premier choix, parce que la compagnie East Japan Railway a tendance a dire des bobards, ou plutot de relativiser un peu trop les problemes. Neanmoins, j'ai decide de suivre mon premier choix et d'aller d'abord a la gare de Tokyo.
  • Il m'a fallu trente minutes de marche jusqu'a la gare de Tokyo. Des personnes ayant passe la nuit la-bas etaient assises ou dormaient encore par terre. La plupart des magasins etaient fermes (j'en ai vu un seul d'ouvert ou il y avait deja la queue). Contre toute attente, il n'y avait pas beaucoup de monde a l'entree par laquelle je suis venu. Tres peu de monde. Un des passages principaux menant a plusieurs quais etait etonnement assez vide aussi. Par contre, les quais, eux, etaient bondes. Je ne pouvais pas distinguer ou la file d'attente se terminait. Deux des trois quais ou je pensais aller etaient noirs de monde. Entre temps, j'ai remarque qu'il y avait beaucoup plus de monde venant des entrees opposees a celle par laquelle j'etais venu. Tout le monde s'affairait a essayer d'acheter un billet de train, ou a se frayer un chemin vers les quais. Le dernier quai ou je projetais d'aller n'etait pas si sature, alors je decidais d'y rester.
  • L'attente fut longue. Sur le quai, un petit vent nous frigorifiait. Un employe de la East Japan Railway nous ressassait toujours la meme chanson : "Veuillez patientier. Le train est en cours de verification et devrait bientot arriver. Le terminus sera la gare X, etc...". Encore et toujours la meme annonce. Une des personnes pres de moi lui demanda d'arreter de dire des mensonges. Certains personnes vinrent a bout de patience et redescenderent les escaliers. J'ai remarque que d'autres employes en bas empechaient que plus de personnes ne viennent sur le quai. Le train arriva finalement apres une heure et demie d'attente. Une fois dans le train, la premiere annonce fut : "Nous avons des problemes avec un des wagons. Nous devons proceder a de nouvelles verifications. Il va falloir du temps avant que le train ne reparte". La goutte qui fait deborder le vase. Je suis sorti, ainsi que d'autres passagers. Changement de plan: direction le metro.
  • Une fois sorti de la gare, j'ai marche jusqu'a la station de metro de Nihonbashi. Le metro etait un peu en retard, mais arriva finalement apres 4 ou 5 minutes d'attente. Il etait bonde, mais j'ai vu pire. Une fois arrive a Shibuya, j'ai change de train. Il n'y avait que des trains s'arretant a toutes les gares. Le trajet allait etre long, mais qui s'en plandrait dans cette situation ? Apres un dernier changement de train, j'arrivais enfin a mon appartement.
  • Finallement, je suis arrive a 11h. 4h30 de trajet, pas trop mal compte tenu les conditions. Je regrette d'etre alle a la gare de Tokyo, mais il fallait bien confirmer. Depuis, nous avons des repliques tous les jours. La centrale nucleaire de Fukushima devient petit a petit hors de controle, bien qu'il n'y ait pas de raison de paniquer. A propos de panique, je vais ecrire un autre post a propos de ce que j'ai vecu le Lundi 14 quand j'ai voulu faire les courses.

Au bureau, les videos etant bloquees, je ne voyais que des images des evenements. Ce n'est qu'une fois a la maison que je me suis rendu compte de l'ampleur du chaos et de l'horreur. Une vague poussant les voitures, les camions, les trains, les bateaux, les maisons comme de vulgaires jouets en plastiques. Ce que j'ai vecu a Tokyo semble tellement insignifiant compare a ce que sont en train de vivre les habitants au nord. La plupart des habitants vivants dans les zones sinistrees ont tout perdu.
Beaucoup ont perdu leur vie. Beaucoup ont perdu leurs parents, leurs enfants, leurs amis, leur maison. Il est facile de s'imaginer comment ca doit etre dans ce genre de situation, mais je pense qu'il est difficile de comprendre vraiment a quel point la douleur et le desarroi doivent etre forts.

J'en profite pour ajouter un petit commentaire. J'ai regarde le 20h de France2 hier sur le net. Un envoye special a Tokyo etait en train de resumer la situation ici. Il m'a bien fait rire. Il parlait de la secousse qu'on a eu avant-hier. Le pauvre, il etait tout pale. Son hotel a tremble pendant 15 a 20 minutes. Il etait mal. Il disait que dans ses circonstances les japonais se refugient sous les tables, mais qu'il n'imaginait pas faire ca alors que tout tremblait pendant un siecle. Il est ou son hotel ? Sur un paquebot ? J'etais situe plus pres que lui de l'epicentre de son "interminable" seisme. Ca a bien secoue quelques secondes. Oui, on s'est demande si ce n'etait pas la grosse replique dont on nous parlait. Mais 15 minutes ? 20 minutes ? Il etait tellement panique qu'il en a perdu la notion du temps ? Ce n'est pas etonnant de voir les francais paniquer et rentrer en France quand on entend des anneries pareilles. En parlant de rapatries, je ne sais que penser de tous ceux qui ont prit la poudre d'escampette (ils ne sont pas tous dans ce cas heureusement. Je comprends ceux qui vivent au nord, ceux qui ont des enfants...). L'autre qui arrive a Roissy tout sourire, et qui dit qu'elle va peut-etre retourner au Japon dans 15 jours, si tout va bien. La situation ici ne prete pourtant pas vraiment a sourire. Meme pas la honte. D'apres Fillion, on est passe de 5000 francais sur Tokyo a 2000. Les 3000, ils sont passes ou ? Ils font quoi comme boulot au juste ? Bref, la version "apocalypse" des medias francais me laisse vraiment perplexe. D'entendre des anes raconter n'importe quoi pour effrayer le public (j'imagine qu'il faut du sensationnel). On ne sait vraiment pas en qui croire. Une chose est sure, c'est qui si ca se passait en France, ce serait un beau bordel.

Earthquake experience in Tokyo

I was at the office when the earthquake happened on March 11th. Here is my sequence of events, from Friday 11th to Saturday 12th.

  • I'm working in a 24-story building. The earthquake started at the beginning of the afternoon (officially at 14h46). We already had one on Tuesday or Wednesday, so I thought it was "just another one". Then it started shaking more and more. It's the biggest quake I've experienced in ten years here. Some Japanese colleagues started covering themselves under their desk. I was putting my shoes on (I was wearing slippers) and looking right at the door, ready to rush out. The shake started to slow down. The building was still in one piece, and so were we. We all knew it was a big one, and wondered where it came from. Everybody was looking at the latest news on Internet. Leaving out or staying in ? We were all staying in there, waiting for instructions from the disaster prevention center. Then came the announce, telling us how big the earthquake was, where it happened, and what we had to do. The instructions were that it was safe to stay in, and there was no reason to panic. Then came aftershocks. One, two, three... The building was dancing. It felt like being on a boat, and this was getting on everybody's nerve.
  • Another rather big shake (not as big as the first one though) came. I can't remember the time. I told my colleagues I was going out, and told them they should come too. So we went out, waiting for things to calm down. There were many people in the streets. We were all checking the news on our cell phones. We saw what was happening in the north, and what happened to the cities eaten by a tsunami. There were reports that the tsunami would come to Tokyo, but that it would be rather small. Nothing compared to what annihilated the north-east coast.
  • We came back into the building after about an hour. Of course, elevators were stopped. We used the emergency stairs and witnessed how much damages the earthquake had made to the building : cracks on the walls and on the floor. Nothing serious, but it still leaves you wondering what will happen if another big one comes.
  • Back at my desk. It's around four o'clock. We're still keeping an eye on the latest news while working. All trains around are stopped. So are the Tokyo Metro lines. It was going to be a long day before being able to go back home. As the "end of work" bell rung, some colleagues were already leaving. Some would try to find their way through the crowded train stations, despite the transport paralysis. Some decided to walk, no matter how far they were living. Even a three hour walk in the dark and in the cold would not stop them (One of my colleague decided to walk too and told me she arrived safely home the following morning, at 4:30). As far as I'm concerned, I'm leaving too far away from Tokyo. So I decided to stay all night at the office, unless trains would run again, and unless some big shakes were being felt again.
  • Food. I didn't think about food at once. But then I saw a picture of a convenient store, with empty shelves. And another picture of a woman holding four cup noodles. I rushed for the convenient store downstairs. It was almost too late. Only a few small cup noodles left. Bigger ones were already sold out. No more onigiri, no more obentou, no more bread. Fortunately, I had just enough for the night and the morning.
  • It was warm inside, I had hot water, toilets. Nothing to complain about. During the night, East Japan Railway announced that their trains would start moving around 7am. I decided to leave the office at 6:30am and see how it turns out. I had two major routes : one was to go to Tokyo Station and try to get on one of the three lines going to my place. Another one was to ride on the Ginza Line (Tokyo Metro) to Shibuya, change there and get on a train to my place. Difficult choice. The second one is a longer route. I had a bad feeling about the first one. East Japan Railway often says things different from what the reality is. Nevertheless, I decided to check at Tokyo Station first.
  • A 30 minute walk to the station. Some people who spent the night there are sitting or sleeping on the floor. Most shops are closed. The entrance from which I came from was not crowded at all. Very few people. I could not believe it. One of the main passage to the several platforms was not *that* crowded either. But the platforms were ! I couldn't tell where the waiting line was ending. Two of the three platforms I was planning to go were overcrowded. While I walked from one to another, I encountered more and more people aiming at the platforms, or lining up to buy a ticket. They were pouring in from entrances located at the opposite side of where I came from. The third platform was miraculously not very crowded, so I decided to wait there.
  • A long wait. It was cold on the platform, light wind blowing. An employee of East Japan Railway keeps on telling us the same message, on and on. "The train is being inspected, it will take some time until it arrives, the terminus will be station X..." Again and again. One man told him to stop telling lies. Some people around me gave up and got back downstairs. I noticed that the railway employees were preventing more people to come, to avoid that the platform gets overcrowded. Despite the bad feeling, I'm still waiting. The train finally came after an hour and a half. I manage to get on the train, and the first thing we're being told is : "One of wagon is having some troubles and has to be inspected again. It will take some time until it leaves the station". That the last drop poured in my patience pot. I went out. Some others went out too. Change of plan : I go for my second option.
  • Out of Tokyo Station, walking towards Nihonbashi Metro Station. The train was a bit late, but finally came. It was a bit crowded, but I've seen worse. I safely arrived at the Shibuya Station and changed train. The next train I was riding on only had trains which stop at every station. No express. Did I bother ? Of course not. It was going to be a long ride, but as long as I arrive at destination... One more train change, and I was home.
  • Finally, I arrived home at 11am. Not bad, but I regret going to Tokyo Station first. Now we're having aftershocks every day. The nuclear plant in Fukushima is slowly getting out of control, although there's still no reason to panic. Talking about panic, I'll write another blog entry about how it was to go shopping on Monday 14th.

I saw pictures of the tsunami on Internet, but I only discovered the scale of chaos and horror after reaching home. Cars, trucks, trains, boats, houses being pushed by the wave like plastic toys. My earthquake experience seems so insignificant compared to what people are facing over there. Most people living in the destroyed towns have lost everything. Lots have lost their life. Lots have lost their parents, their children, their friends, their home. People are starting to share what they saw. There was a man who was looking for his wife and daughters, who were heading to one of his daughter's graduation ceremony. He found their car among the rubble. Looking into the car, he could see some feet and arms inside, but he was not allowed to confirm who was inside... It's easy to imagine how it must to be in such a situation, but I think it's difficult to understand how painful it really is.

Wednesday, March 9, 2011

Oracle database certifications in Japan or how to make more money from certs

I don't know if this is well-known, but the path to become an OCA/OCP/OCM is a bit different in Japan. First, the naming is different. There are four exams : Bronze, Silver, Gold, Platinium. Yes, one more exam. Before 10g or 11g (I don't remember), certifications in Japan had no relation with OCA/OCP/OCM. Now, Silver=OCA, Gold=OCP and Platinium=OCM. What about Bronze then ? That's where Oracle found a way to get more money from Japanese. Japanese people love exams. There are all sorts of exams in Japan. An exam about a famous manga written by Yokoyama Mitsuteru called sangokushi (very good by the way if you're interested in China's romance of the three kingdoms), an exam about fishes, etc... Moreover, IT companies in Japan are taking certifications very seriously. Certifications are almost must-have career objectives. So many IT employees are aiming at Oracle certifications. Ok, back to the Oracle database certification. Let's compare the path to OCA in Japan and in the US.

Becoming an OCA in Japan


Remember ? OCA is equivalent to Silver, and Bronze is a pre-requisite.

Step1 : Passing Bronze

Pre-requisites for Bronze. You need at least one of the following:

  • 11g SQL Basics I[11g SQLI] : 22260 yens
  • Bronze SQL Basics I[Bronze SQLI] : 22260 yens
Then, you can take the Bronze exam, which costs 22260 yens.

Step 2 : Passing Silver

With Bronze in your pocket, you're ready to go for Silver. This will cost you another 22,260 yens.

Congratulations, you've spent at least 66780 yens, which in the current rate is about 805 US dollars.

Becoming an OCA in the US


You need at least one of the following:
  • Introduction to Oracle9i SQL : 125 US dollars
  • 1Z0-047 Oracle Database SQL Expert : 195 US dollars
  • Oracle Database 11g: SQL Fundamentals I : 125 US dollars
After one of the pre-requisites is cleared, you can take the Oracle Database 11g Administration I, which costs 195 US dollars.

So the cheapest path is 320 US dollars.

How do you call that ?

Different countries, different rates. Yes. Still, 805 dollars vs 320 dollars. How do you call that ? Fraud ? Racket ? Whatever you call it, I only see it as a way to grab more money. Fortunately, most companies in Japan will pay the exam fees if you pass. Let's call it extra motivation then. Thank you Oracle.

Monday, March 7, 2011

Oracle Java Architect certification updated

From August 1st, some pre-requisites will be added to the Java Developer and Java Architect certifications (Details here). Basically, you'll need to have attend some training courses. Expensive ones. Like 3000 dollars. Ouch. But that's not all. That will be 3000 bucks for the basic Java programming course. Wait, wait. I already have a few Java certifications in my pocket, do I need to do a basic course ? It looks like so.

The approved trainings for "Oracle Certified Master, Java EE 5 Enterprise Architect" are:

  • Java Programming Language, Java SE 6
  • Object-Oriented Analysis and Design Using UML
  • Developing Applications for the Java EE 6 Platform
  • Developing Architectures for Enterprise Java Applications

I clearly don't want to take "Java Programming Language, Java SE 6". I'd prefer to take "Developing Applications for the Java EE 6 Platform" instead. But that course says :
Required Prerequisites:

  • Experience with the Java programming language
  • Familiarity with object serialization
  • Familiarity with relational database theory and the basics of structured query language (SQL)
  • Familiarity with the use of an IDE
  • Java Programming Language, Java SE 6 (SL-275-SE6)

What ? I need to waste money on that "Java Programming Language, Java SE 6" course ?

"Developing Architectures for Enterprise Java Applications" is even worse. You need both "Object-Oriented Analysis and Design Using UML" and "Developing Applications for the Java EE 6 Platform" (which implies that you also need "Java Programming Language, Java SE 6"). I leave you calculate how much it costs to take that course.

(the content of this blog entry originally comes from my post at Coderanch. Check that thread for some more up to date news on this topic.)

Thursday, March 3, 2011

Using Clojure in Vim - Part 2 : accessing REPL via Nailgun

I explained how to install the Clojure plugin for Vim, Vimclojure, in a previous blog entry. Now that I'm starting to get familiar with Clojure, I thought it would be nice to be able to access REPL from Vim. To achieve this, I downloaded the Nailgun client and server. Download them from the following locations:


Starting the Nailgun server

As explained in the Vimclojure help documentation in Vim (:help vimclojure), the server has to be started with clojure and clojure-contrib jars in the classpath. For example : "java -cp clojure.jar;clojure-contrib.jar;server-2.2.0.jar vimclojure.nailgun.NGServer 127.0.0.1". Actually, in the Vimclojure plugin archive, there is a "bin" directory with two batch files: ng-server (for unix) and ng-server.bat (for Windows). These batch files allow to launch the server easily. Create a ".clojure" file in the directory where you launch the batch from, and put all the necessary jars and classpath directories in it. One per line.


After launching Nailgun, a message should appear, telling you that the server is running :
NGServer started on 127.0.0.1, port 2113

Installing the Nailgun client

Unpack the client archive in a directory of your choice. Edit your .vimrc (_vimrc) file, and add or modify the following lines :
let vimclojure#WantNailgun = 1 
let vimclojure#NailgunClient = "D:\\nailgun-0.7.1\\ng.exe"
Replace the directory with the directory where you unpacked the client.

Using REPL in Vim

Launch Vim and create a new Clojure file. For example, simple.clj. I assume that it is saved in one of the directories of the classpath used for the Nailgun server.
(ns simple)
(defn hello [] (println "Hello"))
To launch a new Vim REPL, type <LocalLeader>sr. <LocalLeader> is defined by the Vim maplocalleader variable, which is '\' if not defined. Another useful command is <LocalLeader>sR, which will start REPL in the namespace of the current buffer (it uses Clojure's "require"). In the previous example, this would start REPL in the "simple" namespace. REPL will be opened in a new window, where Clojure can be used as usual. Very convenient. Check the following image to see the previous tiny snippet in action.




There are other useful commands, which are all explained in ":help vimclojure". One of them is <LocalLeader>lw, which executes Clojure's "doc" to the word under the cursor. It's very helpful to check the documentation of a function. Also helpful is <LocalLeader>sw, which shows the source of the word under the cursor. The preview windows opened by these commands can be close via <LocalLeader>p. Check the Vimclojure's help for more.

Wednesday, March 2, 2011

Clojure In Practice : Yahoo! Weather

I have finished reading Clojure In Action. Now it's time for some hands-on exercises. There are some practical examples in the book, but they did not interest me. A while ago, when I was reading Maven By Example, I came across the Yahoo! Weather RSS Feed, and I thought that reading RSS feeds was an entertaining way to make samples. I'll try to make a simple example of how to read the Yahoo! Weather RSS Feed in Clojure. I'm still new to Clojure, so there may be better ways to do it. But at least, let's make something work.

I was thinking of what was needed in order to make use of the feed :
  1. A way to send a request to the Yahoo! Weather RSS feed.
  2. A way to interpret the feed (XML).
  3. A convenient way to hold the weather data.

Sending a request to the Yahoo! Weather RSS feed

The url to the Yahoo! Weather RSS feed is http://weather.yahooapis.com/forecastrss. There are two request parameters which can be used to customize the type of weather report : the WOEID, which is the weather location, and the units for temperature (Celsius or Fahrenheit). (Check all details here).



The clojure.xml namespace has a convenient function to parse xml data from a stream or a URI: the "parse" function. Let's try it by getting a weather report where temperatures are in Celcius :

(ns kuriqoo.weather
  (:require [clojure.xml :as xml]))

(def yahoourl "http://weather.yahooapis.com/forecastrss?u=c&w=")

(defn get-feed [woeid]
  (xml/parse (str yahoourl woeid)))
Five lines of code in order to grab the feed !! Programming is dead :)



The "parse" function returns a struct-map which has bunch of :tag, :attrs, and :content keys to represent the content of the xml. Cool, but not too convenient to use. The next step will be to make it easier to get values from tags and attributes.

Interpreting the feed

The clojure.zip namespace has some interesting functions to navigate through trees. As we know, xml represent data in a tree of tags, and clojure.zip will help go through it. When I first saw that namespace, clojure.zip, I thought it had something to do with zip files. No, it contains helper functions related to zipper data structures. In brief (from here), a zipper is a data structure representing a location in a hierarchical data structure, and the path it took to get there. It provides down/up/left/right navigation, and localized functional 'editing', insertion and removal of nodes. Let's change the "get-feed" function to transform our data into a zipper :
(ns kuriqoo.weather
  (:require [clojure.xml :as xml]
            [clojure.zip :as zip]))

(def yahoourl "http://weather.yahooapis.com/forecastrss?u=c&w=")

(defn get-feed [woeid]
  (zip/xml-zip xml/parse (str yahoourl woeid)))
I've only added a required namespace and a call to "xml-zip". Programming is dead, really.



The clojure.zip namespace has some basic functions to help us read the feed. We can go up, down, left, right inside the tree of tags. There's an more convenient way to navigate through it : the zip-filter namespace of the Clojure-Contrib library. The doc says that zip-filter is a system for filtering trees and nodes generated by zip.clj in general, and xml trees in particular. There are some usage examples in the source file (xml.clj), so it's worth looking at. Let's use it. From Yahoo!'s documentation, we can see that the "rss" tag is the root tag of the xml document. It contains a "channel" tag which contains a few sub-elements.

By using the zip-filter namespace, we can easily access tags and attributes. For example, to get the city name of the weather report, we would use :
(ns kuriqoo.weather
  (:require [clojure.xml :as xml] 
            [clojure.zip :as zip]
            [clojure.contrib.zip-filter.xml :as zf]))

(-> (zf/xml1-> zipper :channel :yweather:location) (zf/attr :city))
where "zipper" is the data returned by our "get-feed" function. As you can see, it first grabs the "channel" tag, then the "yweather:location" tag, then the "city" attribute. The "xml->" function may returns a collection. Instead, the "xml1->" function returns the first element of that collection. It's more convenient to get tags and attributes values.

Gathering the weather data

There are several ways to hold data in Clojure. The way weather data is expressed in xml seems to fit with Clojure struct-maps. I'll make four of them : one for the units (temperate, distance...), one for the wind information, one for the astronomy informations (sunset, sunrise), and one for the actual weather conditions.

(defstruct units :temp :distance :pressure :speed)
(defstruct wind :chill :speed)
(defstruct astronomy :sunrise :sunset)
(defstruct condition :text :temp :date)
Then comes the function which grabs the data we're interested in :
(defn get-weather [woeid]
  (let [zipper (get-feed woeid)
        z-location (zf/xml1-> zipper :channel :yweather:location)
        z-units (zf/xml1-> zipper :channel :yweather:units)
        z-wind (zf/xml1-> zipper :channel :yweather:wind)
        z-astronomy (zf/xml1-> zipper :channel :yweather:astronomy)
        z-condition (zf/xml1-> zipper :channel :item :yweather:condition)]
    { 
    :city (zf/attr z-location :city)
    :country (zf/attr z-location :country)
    :units (struct-map units
                       :temp (zf/attr z-units :temperature)
                       :distance (zf/attr z-units :distance)
                       :pressure (zf/attr z-units :pressure)
                       :speed (zf/attr z-units :speed))
    :wind (struct-map wind
                       :chill (zf/attr z-wind :chill)
                       :speed (zf/attr z-wind :speed))
    :astronomy (struct-map astronomy
                       :sunrise (zf/attr z-astronomy :sunrise)
                       :sunset (zf/attr z-astronomy :sunset))
    :condition (struct-map condition
                       :temp (zf/attr z-condition :temp)
                       :text (zf/attr z-condition :text)
                       :date (zf/attr z-condition :date))
     }))
That's a big function. There must be a way to refactor it, but I'll leave that as my next exercise. Finally, let's make a function to print out a basic weather report:
(defn print-weather [info]
  (let [condition (:condition info)
        units (:units info)]
  (println "Weather at" (:date condition) "," 
           (:city info) "," (:country info))
  (println (condition :text) "," 
           (condition :temp)
           (units :temp))))

Now, let's see all this in action. I'm using the WOEID for Tokyo/Japan, which is 1118370.
user=> (use 'kuriqoo.weather :reload)
user=> (def info (get-weather 1118370))
user=> (println info)
{:city Tokyo, :country Japan, :units {:temp C, :distance km, :pressure mb, :speed km/h},
:wind {:chill 8, :speed 16.09}, :astronomy {:sunrise 6:09 am, :sunset 5:36 pm},
:condition {:text Light Rain Shower, :temp 10, :date Wed, 02 Mar 2011 12:29 pm JST}} user=> (:country info) "Japan" user=> (-> info (:condition) (:text)) "Light Rain Shower" user=> (print-weather info) Weather at Wed, 02 Mar 2011 12:29 pm JST , Tokyo , Japan Light Rain Shower , 10 C nil

That's it. A simple weather report in Clojure. There are two things I'd like to do next. First, the feed contains a weather forecast for the following day, which I'd like to get. Then, I need to refactor the get-weather function and find a better way to get the data.

Book review: Clojure In Action

There are already a few books on Clojure. I decided to go for one of the latest, Clojure In Action (the book was still in early access, finished, but not published yet).

The first seven chapters teach the basics. They are easy to follow. I felt that the chapter on concurrency was not clear enough. A lot of text about common concurrency issues, but no practical examples of the different Clojure features. Maybe in the sections that I skipped ? (more on that next). I just wanted to see one example using multiple threads and Refs, Agents and others.

Leaving the basics, chapter 8 shows how to do mocking and stubbing in unit testing. Then come four chapters using third party libraries and frameworks. I'm ashamed to say that I skipped most of their content (50 pages). HBase, Redis, RabbitMQ, Hadoop... That would have been fine with small libraries which do not require configuration or knowledge. This does not reduce the value of the book though. It's an "In Action" book after all. It is clearly stated on the cover that it will use these frameworks. If you are interested to learn more than just the language, this book contains a vast panorama of "in action" examples. I may come back to these chapters later.

Then, back to the core. Protocols, macros, etc... The modus operandi thing in the Protocols chapter confused me more than anything, although it's a good exercise at making macros.

I haven’t read other books on Clojure so I can’t compare this one to others. I'm pleased with this book, although I was interested only in the core functionality. I think it will give you enough grounding to get going with Clojure.