Archive for the 'review' Category

Functional Programming Principles in Scala

For everyone who is interested in having a closer look at Scala and functional programming:

Martin Odersky, the author of this programming language, is giving an introduction into the Scala on coursera. It will be a seven week long, free online class with weekly home-work assignments and a certificate at the end.

You can signup here.

Free MongoDB developer online class

During the past few years we saw a steady rise of NoSQL databases. While most of them are for very specialized scenarios there are a few multi-talents which can be a good replacement for a more traditional storage solution.

One of the more well-known is MongoDB, a document-oriented, schemaless database, which I started to like very much. MongoDB is easy to set up and allows me to store objects into the database right away, without thinking about table layout and hacking SQL installation scripts. Of course it also has its downsides, but later more on it.

Currently I take an online class on MongoDB for developers, which is provided by the makers of the database at 10gen.com for free. The online class consists of a couple of video lectures and regular home work assignments. After seven weeks the class will end with a final programming project and of course a 10gen MongoDB developer certificate.

The homework is easy and the course is progressing with a slow pace which is ideal if you cannot or don’t want to spend to much time on it.

I don’t think you can join the current class, but at the end of February (25th) a new course starts:

“M101J MongoDB for Java Developers”

So if you are interested in taking a closer look, then join the course. So far I like it. It gives a good introduction, talks not just about the programming API, but also about best practices in document design and performance tips. You will not be a professional mongo developer after the course, but I think it provides everything you need to start using MongoDB and is also a good starting point to dive deeper into the issue.

 

Writing A Synthesizer With Brainfuck

Brainfuck is a small esoteric programming language which comes with just eight commands and Turing completeness (given enough time or memory). And that is just enough to write a small synthesizer in it. Some people have definitely too much time ;-)

Reinventing Human-Computer Interaction

Many of you probably remember “Minority Report”, a mediocre science fiction film inspired by a short story of good old Philip K. Dick. The only thing I really remember well about this movie is the huge multi-touch computer used by the police force (or something). Although such sci-fi devices look great they are a total nightmare for every day use. Just imagine you have to lift your arms every time you try to switch to another tab page of your browser. That is not really a solution. But how can we use awesome looking multi-touch devices in combination with our other hardware? Here is a small video which came up with some cool and fresh ideas:

reinventing desktop human-computer interaction: 10/GUI

What is the most popular programming language?

Well, the answer is Java … according to the TIOBE Programming Community Index. Other statistics also announce Java as the most popular one. C/C++ is following closely but is still behind Java. Surprisingly PHP is in many charts on the next place while Perl and Python are struggling for being the most popular scripting language. However Ruby seems to continue it’s rise to conquer the world, although slowing down in the last months.

Here is a chart showing the long term trend of programming languages:

from http://www.tiobe.com

OK, so Java and Perl are slowly dying out. But who is replacing these languages? On the one hand Python and Ruby finally seem to get the attention they deserve. C# is also becoming more popular and is growing in a very stable rate. I think the development of C is interesting. The rapidly growing mobile market may be one reason why this language is still that popular. In the time of web application, plugins and powerful CPUs I would expect C to lose “market share” among the programmer community, but that doesn’t seem to happen.

A Deeper Look at the Scala Syntax

Scala is an object-oriented, statically-typed and functional programming language for the JVM. The strength of Scala lies in it’s powerful syntax which allows to write readable, manageable code which is short and concise – and it comes with great support for common design pattern (i.e. a special keyword for implementing the singleton design pattern).

There are plenty of good tutorials and introductions for learning Scala in the web, but only a few mention the great possibilities programmers have to extend Scala or even build an own domain specific language using Scala without being forced to use macros as seen in LISP-like languages.

One interesting feature of Scala are “blocks” which work quite different then code blocks in Java and C++ although they use the same syntax. A block in Scala is rather a sequence of expressions which is evaluated successively and it returns the value of the last expression. Just think of them as the “do” function in LISP. Blocks are surrounded by curly braces and the expressions they hold are separated by semicolons.

scala> {val x = 2; x + 5}
res0: Int = 7

Thanks to some syntactic sugar the semicolon can be replaced by a line break which becomes handy in larger blocks.

scala> {
  val x = 5
  x + 5
}
res1: Int = 10

Because blocks are just expressions they can be used, of course, like every other expression. They can be stored in variables, returned by functions and also be used as parameters for functions:

scala> println(2+5)
7

scala> println {
  val x = 2
  x + 5
}
7

The attentive reader might already have a glimpse of the potential power of such a syntax. This tiny, simple feature allows us to define functions which look and feel more like keywords of the language’s syntax. Just think of a function which measures the time a given piece of code needs to be executed. How would you write something like this in Java or even in a dynamic language like Python? In Scala this can easily be done by writing a function which expects a code block and handles it like a function which returns a value of any type to simulate lazy parameter evaluation.

def time(code: => Any):Any = {
  val time = System.currentTimeMillis
  val result = code
  println("time elapsed: " + (System.currentTimeMillis-time))
  result
}

def fib(n:Int):Int = if (n < 2) 1 else fib(n-1)+fib(n-2)

scala> time {
  fib(40)
}

time elapsed: 1020
res2: Any = 165580141

Creating HTML Documents from Files with Wiki Syntax

I want to create some simple HTML documents with some basic layouting like heading, images and links. Writing pure HTML can be a slow and painful procedure. Usually I’m working with a lot of wikis, so I want to have a file were I can write my document using a common wiki syntax and generate HTML code from this wiki file afterwards.

There some good Perl wiki-to-html converters out there, but since I don’t want to install Perl on my Windows machine at my laboratory, I rather want to have something written in Python.

I found a nice parser and html converter for the Creole Wiki Syntax, it works well with Linux and Windows.

Unfortunately Creole is not the most powerful wiki syntax, I didn’t found out how to create a table of content yet and there are some other drawbacks. If someone knows a good wiki2html tool which doesn’t have tons of dependencies and runs fine under Windows, feel free to leave me a message.

Security in Windows 7

Windows Vista’s UAC is designed to get in the way and annoy you, this is Microsoft’s strategy to force developers writing programs which are able to run with normal user privileges. But it seems that Microsoft developers don’t want to redesign their own programs like MS Paint or Notepad, instead they defined a white list for some programs in Windows 7 which leads to a really hilarious security hole.

Java Mocking Frameworks Comparison

I found a very nice comparison of mocking frameworks for Java/JVM. If you already have a basic understanding about mocking and are looking for a good framework with easy to learn syntax this site will definitely be helpful.

However my favourite is Mockito, which comes with a very intuitive syntax and allows mocking by stub methods. Let’s say you have a class Foobar with a public method int callMe(String s) to stub the method use the following code:

Foobar mockedFoobar = mock(Foobar.class);
when(mockedFoobar.callMe("bla")).return(42);

That’s all, just two lines of code and you successfully mocked a method without complicated inheritance strategies :)

Why Scala is worth a try

In the last few years several interesting languages appeared for the Java Virtual Machine. I think the first notable one was Nice which has a very similar but less verbose syntax as Java and provides concepts like Multimethods or Design by Contract. One of the most famous alternative JVM languages is probably Groovy, a dynamic scripting language which can be compared with Python or Ruby. The idea behind Groovy is to extend the original Java syntax to make coding much faster but still stay compatible with Java.
A totally new approach to develop a language for the JVM is Clojure, a Lisp clone optimized for concurrency and compatibility with traditional Java libraries. Clojure is a dynamic, functional language, can be interpreted or directly compiled into bytecode and has the most terrifying syntax someone can imagine who never came in contact with (L(i(s(p)))) before.

All these languages have their own particular niche, except maybe Clojure which could be a good Java replacement for parenthesis lovers. But what happens with the rest of us? I personally like the idea of having a programming language which allows me to use the wide range of Java libraries without writing tons of code to do things, a modern script language is able to do within three lines of code. Although Python comes with very good libraries and allows me to write dense, readable code with the speed of light, the execution time is utterly devastating and it can be quite challenging to use Python for big projects. I want a JVM language for fast prototyping which scales up!

And maybe I found a candidate for this job: Scala is a very weird but interesting language which uses type inference, has a REPL and an interpreter to give you the feeling of working with a true scripting language, but can be used as a fully object-oriented, statically typed Java replacement with good support for design pattern. Scala compiles into bytecode is compatible with Java and unbelievable fast. It even supports functional programming with all those nice concepts like pattern matching, higher order functions, lazy evaluation etc …

A small motivation of why using Scala can be found in a presentation by Martin Odersky. For those who want to directly jump into the code I can recommend the tutorial “First Steps to Scala”, which doesn’t introduces all features, but the most interesting for Java developers who want to try out Scala.

I want to see some code!