Monthly Archives: January 2009

Lambda's and Anonymous functions in C# 3.5

Here’s a lambda function in use with the new extensions for a List object.

personnel.foreach(x => x.DoSomeCoolStuff());

(more…)

360° Angle Class for C#

Here is a nifty class to handle an angle. it will wrap around increments and decrements that exceed 360 or 0, e.g. adding 20 to 350 will give 10 not 370 and subtracting 10 from 5 will give 355 not -5.

  • Implicit conversion to and from double
  • ToString and GetHashCode are mapped to the inner angle
  • Operators +,-,>,>=,<,<= have been implemented.
  • Mixing this class and doubles in statements works seemlessly.

Code sample:

Angle angle = 0;
double angle2 = angle + 5;
Console.WriteLine("a1:{0) a2:{1}",angle,angle2); 
//Output: a1:0 a2:5

Files

Making better use of mod_deflate

Output compression using Gzip and Deflate is a common feature of modern webservers. Webpages can be compressed by the server and then decompressed by the client seamlessly.

By default (at least on debian/ubuntu) Apache has a module installed and enabled called mod_deflate. While great, here is the default configuration:


      AddOutputFilterByType DEFLATE text/html text/plain text/xml

Now at a glance this is fine, But modern webpages consist of more than just html, we have CSS, Javascript, RSS and even JSON, all of which can benefit from compression but aren’t enabled by default.

Here’s a modified config file that will compress these files:


      AddOutputFilterByType DEFLATE text/html text/plain text/xml application/javascript text/css application/rss+xml application/json

This config is usually located at /etc/apache2/mods-enabled/deflate.conf.

With this done jQuery (minified) goes down from 54KB to just 16KB of data send to the client :D

Multiple Onload Functions

Here’s a quick way not so good way (see update) of getting multiple javascript functions to be called when the page has finished loading.

Add this to the pages Head section:

 

And then where you need an onload event callback do this:

onload_functions.push(function() {alert('The page loaded...'););

Update:

As pointed out by phihag, this solution isn’t great, instead try using phihag’s code or use jquery and just $(document).ready(function{} {alert('The page loaded...');});.

CSS Columns and the Box Model

One of the greatest frustrations for me when designing a website has been using css to layout a multi-column format (like this one).

I’ve found that it is important to understand the box model. more importantly knowing that when you set the width of a block element (ie div) it sets the width of the content. The padding, border and margin is wrapped arond it.

Box model image