Closures

Closures (aka Blocks) are essentially blocks of code that can be passed as arguments to a function or method. Unlike method pointers, anonymous classes, and other similar things, closures have the unique ability to be able to see the value of variables in the same scope they were created in. For example, if you declare a variable on one line and a closure on the next, the closure will have visibility of the variable when the closure executes, assuming it is passed into the closure.

Martin Fowler has a great example of this. To pull only certain items out of a collection in Java you’d have to do something like this:

public static List<Item> getItemsWeCareAbout(List<Item> orig) {
  List<Item> toReturn = new ArrayList();
  int val = 230;
  for (Item item : orig) {
    if (item.meetsConditionWeCareAbout(val)) {
      toReturn.add(item);
    }
  }
  return toReturn;
}

However, the same code in Ruby can be done like this:

def items_we_care_about(orig)
  val = 230
  return orig.select { |i| i.meetsConditionWeCareAbout val }
end

The Ruby version utilizes closures in that we can pass a block of code into the select method. The actual closure is the code within the curly braces ({..}). This code simply says that each item ( |i| ) that returns true from the meetsConditionsWeCareAbout method should be included in the return collection. Also note that the variable declared in the method is passed into the closure, meaning that the closure has visibility into the variable each time it is executed, not just when it was defined.


The Bear in My House

The other night my daughter woke up screaming about 4am. I went in and she was sitting straight up in bed, eyes blank. I got her calmed down and she went back to sleep, only to wake up 20 minutes later. Repeat, but this time she stayed asleep.

In the morning I asked her what her bad dream had been about. With a look of doubt on her face she very seriously asked, “Daddy, was there a bear in the house last night?”.

Yes. Yes, there was. Not really. I told her no and took it as a lesson.

I had been channel surfing and stopped on a Discovery channel show about wolves. In it, there was a grizzly bear that stole a meal from a pack of 4-5 male wolves. During this time she had been playing with her dolls on the floor, so I thought nothing of what I was watching. I am slowly but surely learning that kids, even at age 2, see and hear everything. The show that I didn’t even think she was paying attention to ended up causing a bad dream later that night.

Young children really are sponges.


May CPI Data Out

The consumer price index (CPI) data for May was released yesterday, showing a two point spike in both the national and southeast averages. If we calculate the inflation rate for this year. All is not well.

In 2007, the entire inflation was 4.44% for the southeast and 4.08% for the nation as a whole:

Southeast = [ ( 203.457 – 194.8 ) / 194.8 ] * 100 = 4.44%
National = [ ( 210.036 – 201.8 ) / 201.8 ] * 100 = 4.08%

In just the first five months of 2008 we are rapidly approaching the same figures. The southeast inflation for January 2008 through May 2008 is 3.22%. The national inflation for the same period is 3.14%.

Southeast = [ ( 210.006 – 203.457 ) / 203.457 ] * 100 = 3.22%
National = [ ( 216.632 – 210.036 ) / 210.036 ] * 100 = 3.14%

If we look at these numbers we can estimate the total inflation for the year, ceteris paribus. By calculating the change from month to month for each region we are interested in we can arrive at an average change per month. If we then apply this to the rest of the months in the year we can figure inflation for the total period, giving us our estimate.

Here are the numbers:

National CPI Southeast CPI National ? Southeast ?
12/2007 210.036 203.457
01/2008 211.080 204.510 1.044 1.053
02/2008 211.693 205.060 0.613 0.550
03/2008 213.528 206.676 1.835 1.616
04/2008 214.823 208.085 1.295 1.409
05/2008 216.632 210.006 1.809 1.921
Average 1.319 1.310

So with a average change of 1.310 for the southeast and 1.319 for the nation, we can calculate the estimated inflation for the year by adding the current CPI for each region to the region’s average, times the number of months remaining in the year (7):

Est. Southeast CPI = 210.006 + ( 7 * 1.310 ) = 219.176
Est. National CPI = 216.632 + ( 7 * 1.319 ) = 225.865

We can now recalculate the inflation, using these latest numbers, to arrive at our estimated inflation for the year:

Southeast = [ ( 219.176 – 203.457 ) / 203.457 ] * 100 = 7.73%
National = [ ( 225.865 – 210.036 ) / 210.036 ] * 100 = 7.54%

So using our estimation, based on the current progress of the year, we are looking at total inflation for the year capping out around 7.73% in the southeast and 7.54% nationally.

Pretty scary, huh?