Yellow Lab

Dev

Functional Programming in Ruby

After many years of software development in the imperative style under my belt, I have in recent years developed a keen interest in functional programming as an alternative paradigm. First via the confusion of Functional Java, then Scala and now some forays into Haskell. Having experienced first hand the improvement in legibility and comprehension the functional style can bring, I naturally wondered how FP could be applied to our Ruby work on Yellow Lab.

Ruby’s strength is in its loose typing and imperative style. I like to think of it as the new and improved Perl. A rapid prototyping and scripting language for the modern developer and technical SME alike. Whilst it lacks the rigour of a purely functional language (as all imperative languages do) it does have functional capabilities.

The essence of the functional style is the application of functions instead of variable mutation. It follows that functional languages must support function expressions for there to be any prospect of applying the consistent behaviour over an enumeration. Ruby supports three types of function expressions:

Blocks

Anyone who has used collect or inject on a map knows what a block is. They are anonymous function expressions which are applied in a method when the yield keyword is encountered.


fruits.each {|fruit| eat fruit } # delicious!

In this example the brackets denote the block, the pipes delimit the input parameter list and the remainder is the block body to be executed. Blocks cannot be assigned to a variable so their reuse is limited. One of the Yellow Lab developers has written more about Ruby blocks on his blog.

Procs

Procs are references to blocks. The are constructed explicitly with Proc.new or implicitly when ampersand prefixed method parameters receive anonymous blocks. They are invoked with a call to call.


def do_something_with_fruit(fruit, &fruit_related_activity)
fruit_related_activity.call(fruit)
end

do_something_with_fruit(banana) {|fruit| eat fruit} # => the banana is eaten!

On first sight they seem fit for purpose. But they have some strong limitations.

1. Flow control statements, such as return and break, will affect the flow in the invoking method, not just in the proc.

2. They can happily be invoked with an incorrect numbers of parameters. Some may think this is a benefit, but I believe it is an open invitation for bugs.

Lambdas

Lambdas are akin to Procs, but correct the two limitations listed above. That is, flow control statements take effect within the scope of the lambda only and the number of parameters must be correct. They are the best fit for reusable references to function expressions.

blend = lambda {|fruit| blender.blend(fruit)}
freeze = lambda {|fruit| freezer << fruit}

basketOfFruit.each &blend # => a smoothie
basketOfFruit.each &freeze # => now icecream
canOfWorms.each &freeze # => um ...


Utility

Our application uses the Rails framework and has many of the artefacts you’d expect to see in a Rails application: controllers, models, views, some rake and capistrano tasks. Much of it is elegant and simple imperative style code. Blocks are used frequently within Labs to simplify the application of behaviour across an enumerable – especially in rake and capistrano tasks that prepare our data for runtime. There seems little necessity in this kind of application for the inclusion of lambdas. (I expect this would be quite a different situation if we were building a framework instead).

Ruby is far from a functional language. No one could possibly argue otherwise. The lack of typing and immutability ensure that it is too tempting to revert to a less rigid style. But, as this post has shown, there are some functional aspects of the language and it is certainly worth keeping these in the toolbox.

Tags: , , , ,

Tuesday, September 15th, 2009 Dev No Comments

What A Modern Rails Application Looks Like In Production

Yellow Lab is a Rails application. When we first started up we chose Ruby on Rails (Sensis generally uses Java) to see how using a dynamic platform would change our delivery patterns. Now that we’ve been running for a while, we wanted to share what our high level architecture looks like and the various components that make it up.  We hope it is interesting to see what a live, enterprise, Rails application looks like when deployed, and if you have some ideas to share with us, we are always happy to hear them.

We find that simple systems are easier to build and understand, which make them easier to change, so with that in mind we are always striving to make things as simple as they can be, without being simplistic.  Our setup is a fairly standard in modern web applications with a web server that receives client’s requests then passes those requests back to either the file-system (if static assets are requested) or to the application server which in turn pulls data from a database, a search engine or the file system.

Yellow Lab System Overview

The Bits And Pieces

  • The Web Server (Nginx) – (pronounced as “engine X”) is a light weight, high performance web server/reverse proxy. We chose Nginx for it’s light footprint and easy configuration.  It has served us very well and hasn’t got in the way of anything we’ve needed to do. If you are looking for a web server and you’d like to try something beside Apache’s httpd then Nginx is a great alternative.
  • The App Server (A Bunch Of Mongrels)Mongrel is our application server.  If you are from a Java world you can think of it as Apache’s Tomcat for Ruby.  We use Mongrel to host our Rails application and it does a fine job.  Mongrel was the default deployment option for Rails until the original author, Zed Shaw, decided he didn’t want to support it any more and a competing technology, Phusion’s Passenger, came along.  Passenger is the “preferred deployment setup for Rails” – it is under active development but doesn’t really offer us enough to switch from Mongrel.  We’ll probably swap over to it, but we’re in no rush.
  • The Meat And Bones (Ruby On Rails) – We are using the Ruby on Rails framework to build the business logic for the site.  So, has it changed our delivery patterns? Yes, it’s made us faster. Some of it is probably due to the fact that we are a lab/test bed group, but RoR has certainly been a major contributing factor to allow us to get features into ‘production’ more quickly (there are some stories and learnings here, but we can explore that some other time).  Ruby is a fantastic language to build web applications with and Rails leverages Ruby’s language features pretty well.
  • The Database (PostgreSQL)PostgreSql is a mature, open source database with clear licensing.  If you haven’t looked at it, then you should.  We use PostgreSql to store business listing data and user generated content (we are sooo web 2.0).  Fairly straight forwards stuff.  We also use a PostgreSql GIS extension, PostGIS, to generate reference data for suburbs that we use during our search.
  • How We Find Stuff (FAST ESP) – All Sensis properties use FAST ESP (bought by Microsoft last year) for their search requirements. Being a test bed for new functionality (of which search is a major component), we use it too.  In the open source world Solr (build on Lucene), Sphinx or Xapian may fill the same requirements. We make heavy use of FAST and many of it’s feature and there is much we can say about it as well as all the things we have learned about search from using it, but we’re trying to keep this short and sweet, so moving right along.

That’s our logical design, but physically we have two servers.

Why Two Machines?

Notice that one of our machine is completely devoted to FAST (our search product). While we could use just a single server for delivering search results we need the grunt power of a second machine when we “ingest” our data (load it) into FAST.

If you have a comment or suggestion about our setup or simply want to brag about showcase your own, please leave a comment, we’d love to hear from you.

Tags: , , ,

Wednesday, August 19th, 2009 Dev 2 Comments

Search

 

Categories

Links

Sign in/out