Sunday, November 30, 2008

Gator Talk

Yesterday I went to the Florida vs. FSU football game. I haven't had much recent, personal college football history...

The last college football game I had been to was ... a Bakersfield College game earlier this year. That's not exactly Division 1. The last D1 game I had gone to was Tennessee vs. UCLA in 1997.

The last Florida football game that I attended was 1995, vs. Tennessee.

The last Florida vs. FSU game that I attended was ... 1986!

Needless to say, Saturday's game was a sweet return. I went with my brother, and we managed to stay relatively dry despite the dreary weather. Some folks thought that the Gators played a little sloppy, but I thought that the game, especially the first half, was about as good as could be ... if it wasn't for the kickoff team. FSU scored 9 points without any offense, courtesy of filed position. Two FGs were setup by long kickoff returns, and another by a fumble (only one fumble given the conditions, is not too bad.)

Anyways, all attention shifts to the SEC Championship Game against Alabama. The game is a de facto playoff game: whoever wins will be playing for the BCS Championship. Injuries are playing a big part of this game. Percy Harvin hurt his ankle last night against FSU, and that is a huge blow. The Gators have a lot of dangerous players, but Harvin is the most dangerous. You have got to assume that either he will not go, or (worse) will play, but play ineffectively. Advantage Alabama.

The Gators still have their three blazing fast running backs, Rainey, Demps, and Moody. They will have to get more out of these guys. However, this game will on artificial turf in the Georgia Dome. The fast track definitely favors Florida.

Florida will also get Lawrence Marsh back. That should help, as they looked a little vulnerable up the middle at times against FSU. If the Gators are stout up the middle (as they have been all year,) it will be very tough for Alabama to score much. Oh what I wouldn't give to go to that game!

Monday, November 24, 2008

Delta Airlines Entertainment

A few days ago I flew from San Jose back to my hometown, Panama City, Florida. To be accurate, I first flew from San Jose to Atlanta, and then from Atlanta to Panama City. On the flight from San Jose, the 757 was equipped with an onboard entertainment system. There was a free part and for-pay part. The main component of the free part was satellite TV courtesy of Dish Network. The other part was a decent selection of free music to listen to. The for-pay was comprised of movies, HBO programming, and a few games.

The Dish Network TV was a major disappointment. It did not work at first. Then it started to work, but would consistently freeze up. This would cause a reboot of the system. The only amusing part of this was that it was Linux system and I got to repeatedly watch the Red Hat reboot sequence. I was never so annoyed to see that freakin' penguin... I've flown JetBlue on several occasions and always enjoyed DirecTV on it with no problems. Actually the first time I flew JetBlue was the night that the Iraq War began (Shock and Awe! Go America!!) Talk about a surreal experience: watching live coverage of the beginning of the war while on a jet (flying to Vegas for the beginning of March Madness...) I digress.

The free music was not so bad actually. I was lending my MacBook to Michael, Jr. so he could watch some DVDs, so I was going to listen to my Nano. I wanted to listen to Coldplay's latest, so I was distressed when I realized that I had loaded up all of Coldplay's music except Viva la Vida. However, I noticed that Delta had that album available, and so I listened to it. The sound quality was even worse than the average sound quality of the Nano, but I still appreciated it.

I didn't try out the for-pay entertainment. Michael, Jr. finished watching the Leap Frog DVDs that he wanted to see, and wanted to draw. So I got the MacBook back and reviewed some chapters from a couple of books-in-progress. I did notice that Delta will begin offering Wi-Fi on flights for $10-13 per flight, depending on the length of the flight.

Wednesday, November 19, 2008

Gumbo and Data

This week I was at Adobe MAX. While there I picked up a DVD with a preview of Flex Builder 4 and Flash Catalyst (Thermo.) One thing of particular interest to me was the new data-centric abilities of Flex Builder. Let's take a look at them.

Recently I finished an article for IBM on using Grails and Flex together. In the article I created a Digg-clone and used things like Flex DataGrid and Forms to browse and submit stories. I decided to see how well the new Flex Builder tools could create the same code for me. So I took the existing service and imported into Flex Builder

The screenshots give you an idea of the process. As you can see from the final screenshot, there is a lot of code generated! A service proxy and return types are both generated. Actually for each there is a base class with all of the guts and empty class built for you to customize. There is a modeling class that contains metadata about the return type. The base service class uses a new Flex class HTTPMultiService. It is a lot of code, a lot more than I wrote. Most of it seems reasonable though.

You can also generate a data grid for displaying the data. This worked perfectly for me. Gumbo also promises that in the future you will be able to generate other components, in particular a Form.

Friday, November 14, 2008

Facebook Puzzler in Scala

An associate of mine pointed out some fun programming puzzles on Facebook. This one was interesting to me. It was interesting because I think it is poorly. Well at the very least it confused on first read. When specifying the cost/weight, if they would have just used the word 'each' in there it would have been far less confusing... Anyways, I decided to solve the problem in Scala, since I've been doing a lot more Scala programming lately. Here is my Solver:

object Solver {
def cache = new scala.collection.mutable.HashMap[(List[Int], Int), List[List[Int]]]()

/**
* Generates a list of solutions to the Diophantine inequality
* w1*x1 + w2*x2 +... wN*xN >= max
* where weights = (w1,w2,...wN)
* Each solution is a minimal solution.
* This means that if (x1,x2,...xN) is a solution
* then (x1,x2, ... ,-1 + xM , ... xN) is NOT a solution
*/
def solve(weights: List[Int], max: Int): List[List[Int]] = {
if (cache.contains((weights,max))){
return cache((weights,max))
}
if (weights.length == 1) {
return List(List(max / weights(0) + 1))
}
var all: List[List[Int]] = Nil
var a = 0
while (a * weights(0) < max) {
all = all ++ solve(weights.drop(1).toList, max - a * weights(0)).map(a :: _)
a += 1
}
val solution = (a :: weights.drop(1).map(_ * 0)) :: all
cache.put((weights,max), solution)
solution
}

/**
* For a given set of weights (w1, w2, ... wN) and costs (c1, c2, ... cN)
* This finds the solution (x1,x2,...xN) to the inequality
* w1*x1 + w2*x2 + ... wN*xN <= max that minimizes the total cost
* c1*x1 + c2*x2 + ... cN*xN
* It returns the solutions as a Tuple where the first element is
* the solution (x1,x2,...xN) and the second is the minimal total cost
*/
def optimizer(costs: List[Int], weights: List[Int], max: Int): (List[Int], Int) = {
val solutions = solve(weights, max)
var answer: List[Int] = Nil
var best = (answer, Integer.MAX_VALUE)
solutions.foreach((solution) => {
val cost = solution.zip(costs).foldLeft(0){(a,b) => a + b._1*b._2}
if (cost < best._2) {
best = (solution, cost)
}
})
best
}
}

I put in the cache for memoization purposes, but it doesn't always help. For example, with their sample input/output, the cache is useless. Anyways, I showed the problem to other smarter folks who immediately pointed out that this was a variant of the unbounded knapsack problem and that my solution uses dynamic programming.

Here's a dirty little secret about yours truly. I studied mathematics in college, not computer science. So it's always amusing for me to come across a problem like this and have people start talking about CS terms. Personally I looked it as a Diophantine equation (inequality to be more accurate.) Of course maybe if I was a CS person, then I would have written a nicer solution.

Tuesday, November 11, 2008

Scala Constructors

Tonight at BASE, I had a rant about Scala constructors. So I'll just continue the rant here. Constructors seem great in Scala. At first. They give you some great syntactic sugar where it creates accessors/mutators all in one shot:

class Stock(val name:String, val symbol:String, var price:Double, var change:Double){
}

This lets you do nice things like :

val stock = new Stock("Apple Computers", "AAPL", 94.77, -1.11)
println(stock.symbol) // works great
stock.price = 95 // works good, price is var
stock.symbol = "APPL" // won't compile, symbol is a val

Yay, no getter/setter garbage. But what about overloaded constructors? You can kind of do that...

class Stock(val name:String, val symbol:String, var price:Double, var change:Double){
def this(name:String, symbol:String) = this(name,symbol, 0.0, 0.0)
}

So in Scala you can do implement the telescoping constructor anti-pattern. Nice. But what if you got your stock data as a CSV from Yahoo's web service? You need to do some parsing. You might think this will work:

class Stock(name:String, symbol:String, var price:Double, var change:Double){
def this(name:String, symbol:String) = this(name,symbol, 0.0, 0.0)
def this(csv:String) = {
val params = csv.split(",")
name = params(0)
symbol = params(1)
price = java.lang.Double.parseDouble(params(2))
change = java.lang.Double.parseDouble(params(3))
}
}

Nope, this won't work. You can only do a single statement in the 'this' constructor, and it must be to either the main constructor or another 'this' constructor. No extra code. Bill Veneers pointed out that this often leads to code like the following:

case class Stock(val name:String, val symbol:String, var price:Double, var change:Double){
def this(name:String, symbol:String) = this(name,symbol, 0.0, 0.0)
def this(ser:String) = this(parseName(ser), parseSymbol(ser), parsePrice(ser), parseChange(ser))

def parseName(ser:String) = ser.split(",")(0)
def parseSymbol(ser:String) = ser.split(",")(1)
def parsePrice = java.lang.Double.parseDouble(ser.split(",")(2))
def parseChange = java.lang.Double.parseDouble(ser.split(",")(3))
}

Oy. I think even the most enthusiastic Scala programmer would agree that is some very smelly code (and inefficient to boot.) A more common pattern is to use a factory object:

object Stock{
def apply(ser:String):Stock = {
val params = ser.split(",")
new Stock(params(0), params(1), java.lang.Double.parseDouble(params(2)), java.lang.Double.parseDouble(params(3)))
}
}
class Stock(val name:String, val symbol:String, var price:Double, var change:Double){
def this(name:String, symbol:String) = this(name,symbol, 0.0, 0.0)
}

Having a singleton object and a class by the same name is a construct introduced in Scala 2.7. Now usage looks like this:

val apple = new Stock("Apple Computers", "AAPL", 94.77, -1.11)
val microsoft = Stock("Microsoft,MSFT,21.20,-0.10")

Kind of inconsistent, no? In one place you use the new, but to get the benefit of the factory, you can't use new. So usually people change the class to a case class:

object Stock{
def apply(ser:String):Stock = {
val params = ser.split(",")
new Stock(params(0), params(1), java.lang.Double.parseDouble(params(2)), java.lang.Double.parseDouble(params(3)))
}
}
case class Stock(name:String, symbol:String, var price:Double, var change:Double){
def this(name:String, symbol:String) = this(name,symbol, 0.0, 0.0)
}

Now usage is more uniform:

val apple = Stock("Apple Computers", "AAPL", 94.77, -1.11)
val microsoft = Stock("Microsoft,MSFT,21.20,-0.10")
val test = Stock("Test Stock", "TEST")

I guess that is ok. Becuase Stock is now a case class, you don't have to declare name and symbol as public vals. I kind of like using 'new' and I really don't like having to create both an object and a class just to get overloaded constructors. I think it is still a code smell.

Update: In the comments it was pointed out that the companion object pattern (object and class of the same name) has been around for a relatively long time. What was introduced in Scala 2.7 was allowing case classes to have companion objects. So the last version of the code will not compile on anything but Scala 2.7+, but if you make the Stock class a normal class then it will. Of course then you are back to the problem of having two different syntaxes for the constructor, one that needs the 'new' keyword and one that does not (and cannot.)

Friday, November 07, 2008

Pastrami Dog?

I was driving home tonight and noticed a local Wienerschnitzel advertising a pastrami ... thing ... on a (hot dog) bun. This reminded of exactly one thing: being a kid, in high school or college, and scrounging through the kitchen looking for something to eat. All you find is some sandwich meat and some hot dog buns. There is no bread to make a proper sandwich, so what do you do? You used the hot dog buns. Now Wienerschnitzel has taken this masterpiece and turned it into a product.

Wednesday, November 05, 2008

You Want XUL, You Got XUL

I've written a lot of articles for IBM over the last couple of years. I've covered a lot of topics. It was interesting for me to see which articles have been viewed the most. Last year I did a tutorial on XUL for beginners. To be honest, I really thought XUL was a very niche topic. If you are going to write a Firefox extension, you have to learn XUL. There are lots of resources out there about that. I did not want to write about that kind of XUL development. So instead I wrote about creating a XUL desktop application that used a lot of web development skills. Hey if Adobe can market AIR as a way for web developers to create desktop apps, then why shouldn't Mozilla do the same thing with XUL? It was a fun article to write, but I didn't expect it to be especially popular. Boy was I wrong!

That tutorial has been one of the most popular things I have written for IBM. So it made sense to update it this year. The tutorial used Firefox 3 as a XUL runtime. When I wrote it, Firefox 3 was in alpha stage. Obviously it has been released since then and is in wide use. So the opportunity for web developers to use XUL to create desktop applications is greater than ever. This week IBM published the updated tutorial, so go check it out.

Tuesday, November 04, 2008

Slides from AjaxWorld

Posted by somebody else! Thanks!
Netapp Michael Galpin
View SlideShare presentation or Upload your own. (tags: ajaxworld-2008)

Saturday, November 01, 2008

Vote by Mail

This year I voted by mail. It was very convenient. The state of California even sent me a nice little "I voted" sticker. So how did I vote? Most things will probably not be a surprise if you read my blog on a regular basis. There were a lot of things to vote on, so here is a quick summary.

President -- Barack Obama. I am no fan of his proposed economic policies, but the war remains the most important issue to me. Besides, most of McCain's proposed policies are so similar to Obama's. It is very sad.

Congress -- I voted against my congressman, Mike Honda. I wrote him about the bank bailout, but he still voted for it. Gotta vote against him for that.

Propositions -- These are the fun stuff here in California. The most debated is Proposition 8. This was a no brainer to me, I voted against it. Terry put together a nice post about Prop 8. I was surprised to find myself voting for Prop 2...