23 October 2010

Concepts of Functional Programming

Last week I had the pleasure to give a presentation at Javaabend in Vienna. Javaabend (German for Java evening) is a local Java user group event organised by openForce Information Technology at irregular intervals.

LambdaA Little Rant
This presentation has a long history, so I will start with a little rant. Last year when I started playing around with Scala, we (read some enthusiastic employees) formed an informal study group to have a look at functional languages and Scala in particular. In the beginning we made good progress and had quite some fun and met biweekly. Unfortunately the organisation had a strange attitude to training (as well as to public relations) and we were disbanded. Being stubborn as I am, I managed to establish a budget from "another source" after some time and started preparing this presentation for the monthly "Developer Round Table". The presentation was postponed several times and in the end I left the company for good.

Scope of Presentation
Now let's come back to the presentation. Talking about the principles of functional programming is a bit off-topic for the Code Cop and it's just scratching the surface of the core principles: purity, higher order functions, closures, currying, continuations and (well not really) monads. I'm no expert on functional programming, so feel free to comment corrections or clarifications. Especially the concept of monads is a bit mysterious.

Slides
Download the Concepts of Functional Programming slides. As usual the slides are not very useful without my explanations because they entirely consist of single words and/or images. This is my take on the current presentation style. I received some good feedback on the style and especially the images. One attendee even told me that the images were "too" good for him, he was distracted by them. (Thank you Flickr community for all these wonderful CC licensed images.)

MonadsDiscussion
After the presentation there was an interesting discussion on the advantages of functional programming over the imperative style, e.g. Java.
  • Is it easier to get things done with many lines of simple, imperative code, compared to one line of functional code (that most definitely does not look simple when you are new to the area)?
  • Are the functional paradigms more difficult to comprehend? Is this the reason that functional programming isn't used as widespread as the imperative one? Would the average developer produce bad quality code when using functional languages?
These are difficult questions, only time will tell.

ResourcesAcknowledgement
Researching the core principles of functional programming was part of a System One Research Day.

17 October 2010

Android Browser Bookmarks

In my previous post I already mentioned my Android phone. Soon after I got it, I wanted to import my bookmarks from my desktop PC to its standard browser. So being green I copied my favourites to the sdcard and tried to open them. Well it didn't work.

Google AndroidWorking Around
If I lived in "Google-land", I could use Google Bookmarks. But I don't and had to look for alternatives. After some searching I found a possible way to import bookmarks:
  1. Upload your single html file of exported bookmarks to a document on Google Docs.
  2. In your browser navigate to said page.
  3. Manually click on each link and save as bookmark.
Noooo, you must be kidding me. This is hideous. Then I found a nice instruction how to
import the bookmarks directly into the browser's database. That would be cool. But I'm not bold enough to root my new phone and "play with that, probably break it a bit - and then cry about it later." (Ward from androidcommunity.com)

My Solution
On several places I read hints that some Android apps were able to import bookmarks, but I couldn't find any. Instead I found Matthieu Guenebaud's Bookmarks Manager. It's able to backup and restore the browser bookmarks and uses a plain zip file to store them.
 Viewing .ZIP: Bookmarks_2010-09-10_14-11-24.zip

Length Method Size Ratio Date Time Name
------ ------ ----- ----- ---- ---- ----
2273 DeflatN 710 68.8% 09.10.2010 2:11p bookmarks.xml
526 DeflatN 531 0.0% 09.10.2010 2:11p 23.png
764 DeflatN 769 0.0% 09.10.2010 2:11p 24.png
326 DeflatN 331 0.0% 09.10.2010 2:11p 51.png
684 DeflatN 689 0.0% 09.10.2010 2:11p 57.png
239 DeflatN 238 0.5% 09.10.2010 2:11p 69.png
541 DeflatN 546 0.0% 09.10.2010 2:11p 90.png
1266 DeflatN 1271 0.0% 09.10.2010 2:11p 198.png
490 DeflatN 495 0.0% 09.10.2010 2:11p 164.png
304 DeflatN 309 0.0% 09.10.2010 2:11p 124.png
408 DeflatN 413 0.0% 09.10.2010 2:11p 229.png
------ ----- ----- ----
7821 6302 19.5% 11
The file bookmarks.xml has a simple XML structure of <bookmark>s inside a <bookmarks> element. Yes, that's something I can use.
  1. Backup the bookmarks, even if empty, to get an initial file.
  2. Unpack the archive.
  3. Insert bookmarks into the existing XML structure.
  4. Repack the archive.
  5. Restore from the modified zip file.
  6. Enjoy your new wealth of bookmarks.
In case you don't feel like editing the bookmarks.xml by hand, here is some Scala code.:
val bookmarkXml = scala.xml.XML.loadFile(targetFolder + "/bookmarks.xml")
val lastOrder = Integer.parseInt((bookmarkXml \\ "order").last.text)
val oldNodes = bookmarkXml \\ "bookmark"
val newNodes = to_xml_list(bookmarks, lastOrder + 1000)
val root = <bookmarks>{ oldNodes }{ newNodes }</bookmarks>
scala.xml.XML.save(targetFolder + "/bookmarks.xml", root, "UTF8", true, null)
Thanks to Scala's excellent XML support, reading and modifying the bookmarks file is easy. The method to_xml_list() iterates all favourites and creates XML fragments for each one using the following method.
def to_xml(bm: Favorite, order: Int) = {
<bookmark>
<title>{ bm.name }</title>
<url>{ bm.url }</url>
<order>{ order }</order>
<created>{ bm.fileDate.getTime }</created>
</bookmark>
}
Favorite is a class representing an Internet Explorer favourite that I wrote long ago. (Yeah baby, code reuse!) order is a number Bookmark Explorer uses for sorting bookmarks. See the complete source of GuenmatBookmarks.scala in my Scala Utilities repository.

14 October 2010

Digressions

Two months ago I got a brand new Android phone from my employer. I asked for it to play around and get used to Android development and bingo - it was here :-) This is a major event compared to my prior experiences with employers. But I'm digressing.

A Resolution for 2009So I've been using it for two months. I'm still not sure if it's good for people that just want to talk or text, but it's definitely great to access the internet on the go. So I started catching up with my backlog of articles in Google Reader. Currently I'm back in January 2010, so there is hope that the folder with articles to read will be empty some day. (Alas, it's just one of four, so it looks like I will stay busy for some more months.)

Today I came across Ted Neward's Predictions. I like to read Ted's posts because he doesn't take himself too serious. I especially enjoyed his busy Java developer's guide to Scala two years ago, but I'm digressing again. In his predictions he talked about different technologies and some well known companies. I will not repost his post, but he made some quite sarcastic and funny remarks that I need to share.
  • "Cloud" will become the next "ESB" or "SOA", in that it will be something that everybody will talk about, but few will understand and even fewer will do anything with. --- Well another one in the list of useless buzzwords that lacks a clear definition. Last year I was working on some enterprise integration project and the Czech bank was using AquaLogic ESB. I asked our rock star enterprise architects what exactly defines an ESB. They were not able to give a proper answer. According to the German Wikipedia the term ESB was defined by Gartner in 2002. So it's no wonder that ESB is not proper defined.

  • Being "REST"ful will equate to "I did it myself!"

  • Agile has become another adjective meaning "best practices", and as such, has essentially lost its meaning. --- What does best practice mean anyway? Most likely it is a cookbook and a collection of things that work. According to Andy Hunt best practices are for people that need guidelines. But these guidelines never capture the whole context of the problem. And Ted really hates best practices.

  • Try this: walk into a functional language forum, and ask what a monad is. Nobody yet has been able to produce an answer that doesn't involve math theory, or that does involve a practical domain-object-based example. In fact, nobody has really said why (or if) monads are even still useful. --- When diving into Scala two years ago, I tried to figure out monads. James Iry did a decent job explaining what they do, but I still have no idea what they are or why I should use them. (But probably that's different for pure languages like Haskell.)
How true. Ted, you are great.