If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Friday, November 27, 2009

SCALA: using higher order functions

The creators of scala did a hell of a job to design the core library with control abstractions. Each collection class uses the trait Iterable which implements an exists() method.

Let's take a look at how Iterator.scala implements this higher order function exists().

The big advantage is that it now becomes easy to define generic functions to handle certain collection types. Let's see how easy it becomes to write a method that checks if a collection of numbers contains a negative number

As you can see we don't have to write all the boiler plate code of iterating anymore. We just specify the function literal which checks our condition.
Let's test our code with an Array and List, both containing integers.

numbers contains negative number = true
numbers2 contains negative number = false

Thursday, November 26, 2009

Scala: Tail recursion

Looking back at our Fibonnaci examples the Scala compiler offers one big performance boost when using recursion. The only condition is that you use tail recursion. What this basically means is that the last expression in your recursive method is a call to itself.

Below follows a refactored recursive implementation which performs almost as fast as the non-recursive implementation.

Scala Collection goodies

Scala offers several ways of accomplishing the same. Here we demonstrate 2 ways of calling a function for each object in a collection.

Wednesday, November 25, 2009

Fibonnaci in scala

The first implementation below is my own implementation which stores all calculated values
directly in the array and reuses those values to calculate following values.

Another implementation which heavily uses recursion is implemented using pattern matching. See wikipedia. But as you would expect this implementation has bad performance as we will prove. But nevertheless pattern matching opens up a lot of possibilities.

results in

fibonnaci processed 30 numbers in 16 miliseconds.
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229
fibonnaci2 processed 30 numbers in 47 miliseconds.
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229

In fact the first implementation can print 1000 numbers in almost the same amount of time.... The 2nd implementation however will become unresponsive from n > 50...

Lesson learned: Be carefull in how you use recursion.

Scala: the art of writing code consise

If you come from a java background you're bound to get rusty in programming imperative as opposed to functional. I'm actually familiar with both programming styles and it would be great to be able to use both mixed together.

Now let's take a look at a simple function which takes a list of numbers and returns the sum.

In java this might look like the snippet below:

Ok... i know what you're gonna say... Use the enhanced for loop...

Well... get ready for some SCALA versions of the same functionality

You can also use the placeholder syntax:

But SCALA promotes NOT using var's ...so let's try to even reduce this further:

Using placeholder syntax this becomes:

As you can see we managed to reduce the function to a body with 1 line of code...

Expect more SCALA examples to follow.

Scala: getting started

I'm reading up on scala and the time has come to start using it. The first thing i wanted to get working is the plugin for Eclipse... i tried it for several versions of Eclipse but the plugin was crappy. So next i tried installing Netbeans 6.7.1 and the Scala Netbeans plugin... this gave me also some headaches untill i ended up following the steps on Caoyuan's blog.

Now i'm playing around with the latest nightly build of Scala (2.8) and the latest plugin and they seem to be workable.

Monday, November 9, 2009

Advanced Javascript: part 8


After playing around with the forEach method I noticed that sometimes it would be very usefull to be able to stop iterating over the array. So if the function argument returns false the iteration stops.

A good use case was my implementation of the DOM select function:

Advanced Javascript: part 7

UTIL.DOM: Creating your own html extension functions

It's convenient to work with some kind of html element proxy but the clone method does not work properly in IE. No need to worry though. You can still add convenience methods on original objects without cloning them. That's what javascript is all about... change runtime behaviour of objects.

So I wrote an extend function which takes an html element as parameter and dynamically adds behaviour.

Since we want to these extensions for free by default, we write our own get method for retrieving elements

You can now clear the container in 1 statement:

var myContainer = DOM.get("some-id");

Adding an event handler or event listener using our own method allows us to add behaviour later on easily. An example is shown below:

It also becomes very easy to style elements using CSS.

Advanced Javascript: part 6


The main goal of the DOM library is to provide convenience methods to create DOM elements.

To achieve default styling the library generates classNames out-of-the-box when no className is provided through the config constructor object. Also, all properties will be copied over to the html element.

So instead of writing code like this:

var cell = document.createElement("td");
cell.className = "td"; //default className is set to tagName
cell.width = "100px";

you can write a one liner now:

var cell = DOM.td({"width": "100px"});

Even simpler...

when you provide a string as constructor we assume it's innerHTML so

var cell = document.createElement("td");
cell.className = "td"; //default className is set to tagName
cell.innerHTML = "cell value";


var cell = DOM.td("cell value");

Advanced Javascript: part 5


Advanced Javascript: part 4


A simple interface that allows developers to log messages of type
  • info
  • warning
  • error

Advanced Javascript: part 3


When creating fast webpages you're bound to do partial updates of your page sooner or later. So instead of completely generating full html tables server side, you might want to consider fetching your data in JSON format when certain events happen.

Anyway, the snippet below allows you to do just so.

Now to make an asynchronous request becomes very simple.


Advanced Javascript: part 2

A common mistake (?) often seen is people declaring like a zillion GLOBAL variables in their scripts. That might not cause any problems when relying on only 1 script in your page but it will become a problem for sure when you include scripts from colleagues which contain variables with the same name.

RULE 1: always use namespaces in your scripts

Of course i followed this rule as the image below shows.

Advanced Javascript: part 1

I've been spending a lot of time researching and experimenting with advanced and well-performing javascript.

I managed to increase performance on some webpage containing a html table containing 1400 rows of data (build with javascript) by 500%. This was achieved through abstracting functionality, reducing complexity and using JSON as dataformat.

Inspired by this great result i'm in the middle of writing my own JS library in a use-case driven fashion leaving out all the fancy bits of traditional libraries.

At this moment I created 4 domains which form the base for creating widgets:
  • base -> generic functions
  • dom -> enables fast creation of DOM elements using default standards
  • io -> making (xml)-httprequests
  • logging -> easy way of visualize logging onto your webpage without using annoying alerts