RK4 in Scala

August 27, 2009

I’ve been looking at Scala recently and I’m impressed. It’s got a nice mix of imperative and functional styles that means that it can be really useful and easy to code in.

As an example I thought I’d knock up an implementation of RK4 in it.

The work-horse of RK4 is implemented as follows:

def rk4Iter(f: (Double, Double) => Double, 
            t : Double, 
            y : Double, 
            h : Double) = {
  val k1 = f(t, y)
  val k2 = f(t + h / 2, y + h * k1 / 2)
  val k3 = f(t + h / 2, y + h * k2 / 2)
  val k4 = f(t + h, y + h * k3)
  y + h * (k1 + k2 * 2 + k3 * 2 + k4) / 6

One thing I think we can all agree on is that having functions as first-class objects makes this incredibly succinct and easy to follow. I’m just calling the function f a whole bunch of times. Coming from a Java background this is pleasantly less noisy (compare with f.calculate(t, y) etc).

Another subtlety is the lack of specifying a return type. Scala is smart enough to infer the return type 🙂

This method is intended to be called from a loop that performs n steps of size h:

def rk4(f: (Double, Double) => Double, 
            t0 : Double, 
            y0 : Double, 
            n : Int, 
            h : Double) 
    : Double = {
  n match {
    case 0 => y0
    case _ => 
      val y1 = rk4Iter(f, t0, y0, h)
      rk4(f, t0 + h, y1, n - 1, h)

For those coming from a Java background you’ll notice that match looks a lot like switch. You can think of it as switch on steroids (I’ll leave off explaining more as it’s a blog post in its own right!). It is sufficient to say that case _ is similar to default.

In functional programming the preferred way to do things is using recursion rather than iteration (when written properly the compiler can identify tail-recursive functions and make them just as fast as loops). We do this because it makes them easier to mathematically reason about them. Suppose the function works for:

  • the 0th case
  • the nth case if it works for the n -1th case

then the function works for all positive integers.

A recursive style also reduces the scope for off-by-one errors.

And now for an example that ties it together:

  (t, y) => 4 * Math.exp(0.8 * t) - 0.5 * y, 
  0, 2, 10, 0.1)

I think anyone coming from a Java background will agree that passing around functions like this is lovely: goodbye interfaces and lots of curly braces, hello Functional Programming.


Widgets in PHP – Eliminating Copy and Pasting

March 8, 2009

Recently I have been doing a lot of work on tidying up the front-end of our PHP app. One thing that struck me is the amount of re-use of code for various widgets – and not the good sort.

Our example – A customer quote

Throughout our app it is common to see copying and pasting being used to acheive certain looks and effects. As an example, if we want to display a quote from one of our customers we could use something like:

<div class="Quote">
  <div class='StartQuote'></div>
    Awesome. Simply awesome.
  <div class='EndQuote'></div>

With the appropriate styles this creates something looking like:

sample quotes image

I know that this trivial example can be done entirely in CSS – but we all encounter things that can’t be done entirely in CSS and cross-browser compatible.

What’s wrong with this?

You may now be asking yourself this question. We want our quotes to all look the same so we get it right once then being copying and pasting – what is wrong with that? Well:

  • If we want the look to change we’ve got to change it everywhere
  • We’re prone to making mistakes when copying and pasting
  • It’s quite a lot of effort and mess for something that should be simple

An Alternative – Widgets

What we really want to do is: say what we want at a higher level, e.g.

    Awesome. Simply awesome.

Then have this snippet be replaced with the correct HTML. I call this a ‘widget’ based approach – we now have quote widgets everywhere instead of chunks of HTML.

Nice goal – how do we get there?

One word (ish): post-processing. PHP has a little known set of functions called ob_start and ob_end_flush. These can be used to send all your output to a callback function:

// Lots of HTML / Widget code

The post_process function takes a single string argument, the contents of the page, and returns a string, the post processed content – the content with the widgets replaced by the correct HTML.

Thoughts for the Future

I like this approach. I like being able to write my pages at the level that I’m thinking: quote here, panel there, calendar here… however, there isn’t yet a system that makes writing these new widgets easy – yet. Watch this space 🙂

AJDT and Ant

February 27, 2009

I’ve just had one of those mornings where you spend hours trying to crack a problem and just getting nowhere.

The problem was getting AspectJ to work under Ant but only by using the AJDT 1.6.3 Eclipse plugin.

Unfortunately, this is very poorly documented. Fortunately, this helps you make the right decision: don’t use AspectJ in Ant by relying on AJDT.

There is a very good reason for this (beyond it being hard to get working): you cannot rely on AJDT being available when compiling in anything other than Eclipse (e.g. from a CI server).

What’s the alternative? Download AspectJ and use these libraries directly in Ant – they’re well documented and just work.

Bug in Annotation Processing (Java 6 Update 12 / Eclipse)

February 26, 2009

Just uncovered a fun bug in one of the Java Compiler / Eclipse (haven’t checked to see which one is behaving exactly correctly).

If you are working Eclipse then the Eclipse compiler believes the following snippet of code is OK:

  appliesTo = "PendingVotes",
  indexes = {
    @Index(name = "VoteId", columnNames = { "VoteId" }),

However, the Java compiler (Version 6, update 12) will report the following error:

annotation.java:6: illegal start of expression

Now, I’ll display that snippet again and highlight the problem:

  appliesTo = "PendingVotes",
  indexes = {
    @Index(name = "VoteId", columnNames = { "VoteId" }),

See that sneaky little comma at the end? I believe this is valid syntax (it allows someone to add another item later and only alter a single line – useful for generating diffs).

Remove that comma and everything becomes pain free again.