Saturday, May 29, 2010

The Difference a Space Can Make

How can you print the string value of a floating point value of 5 in Scala?

scala> 5.toString  // Wrong!
res16: java.lang.String = 5

scala> 5. toString
res17: java.lang.String = 5.0

In the first line scala counts the "." as syntax for "toString method of Integer 5".  The second line uses the "toString" method in infix notation, applying the toString method against the floating point value of 5.0.

Here's another example where a space can make a big difference in type:

scala> 4.+(1)
res20: Double = 5.0

scala> 4 .+(1)
res21: Int = 5

Abstract Types in Scala

Whenever I learn new programming languages, there is invariably features of the language that will surprise me.  One of those in Scala (one of many actually) is abstract types.  When I first read the textual description of an abstract type, I wondered what use it would ever entail.  However, the authors of Programming Scala gave a very clear example of proper use.

This code snippet comes from Chapter 2 of the book.

Starting with an abstract class, we can declare an abstract type.  In this case, type "In" on our class "BulkReader" is abstract.  It's there, but it does not have a concrete type associated with it.  Yet, our value "Source" is typed as type "In".  How can this be?

abstract class BulkReader {
  type In
  val source: In
  def read: String
The magic comes when we declare a concrete version of "BulkReader" and can assign a concrete type to "In":

class StringBulkReader(val source: String) extends BulkReader {
  type In = String
  def read = source

class FileBulkReader(val source: File) extends BulkReader {
  type In = File
  def read = {
    val in = new BufferedInputStream(new FileInputStream(source))
    val numBytes = in.available()
    val bytes = new Array[Byte](numBytes), 0, numBytes)
    new String(bytes)
Very cool!  In our two concrete instances of "BulkReader" we assigned two different types to our abstract type "In".  Note that the value typed as In, "source" is actually part of the concrete class's constructor which is, at this point for me, completely mind bending in terms of flexibility towards class design.

I have a feeling it will take me some time to get used to this notion and power before I really start to design code that makes use of it.

Wednesday, May 26, 2010

Language of the Summer: Scala?

I've been quite fascinated with Haskell as a language.  It's purely functional, ridiculously strict typing, and algebraic syntax were completely foreign to me before I began learning about it.  I wouldn't say that I learned Haskell, but I did learn about it.  I learned enough to know that it's a bit over my head at this point and that I need a better theoretical foundation before I attempt to tackle it again.

So in the mean time I wanted to take on something more transitional.  I had considered F#.  It seems like a great choice, especially since I already know C# and am familiar with the .NET platform.  But it's a Microsoft language and I was hoping for something more portable (actually, I'm harboring a secret desire to use a functional programming language to one day code for Lego Mindstorms NXT with).

I happened to stumble upon Lift again yesterday.  I had heard about it before when I was looking at other web frameworks after watching a keynote from the author of Seaside at Djangocon.

However, I realized something important about Lift and Scala which I had never caught before: Scala compiles to java bytecode.  Java can be run on Google App Engine.  I can use Scala/Lift to write for App Engine.

Suddenly I'm hooked.  Oh, awesome, and the book "Programming Scala" is available freely online.  Mmmm... looks like I have my reading for my traveling this summer.

Tuesday, May 25, 2010

Getting a Python/Django Job

Getting a job doesn't seem like a fun activity to many people.  I'm in the thick of it right now.  The problem is when you have a skill set that you really like and that you feel confident with that no company wants.  I feel like I'm in that place with Python/Django.

Sure, I've worked on the "corporaty" systems, ASP.NET, MVC, C#, etc., but what I'd really prefer to work on is python/Django on the web.  It doesn't seem like there are any companies hiring for that in Denver (or maybe I need to come up with a better way to find them?).  So I end up in an odd place where I wonder if it would be worth a.) going back to the corporaty things that get jobs (MSSQL, ASP, etc.) or b.) learning a newer but similar technology (RoR), and gamble that someone will hire me because I have previous experience in Django.

No one in Colorado needs Django work done?

Tuesday, May 11, 2010

I Bought It at Ross!

I was waiting on the wife at Ross Dress for Less the other day, when I happened to notice something:  They sell laptop bags!  Since when did Ross start carrying those?  Surprisingly they had a selection of name brands at, you guessed it, discount prices.

I ended up buying a Targus Messenger Fusion in dark gray/green for the Toshiba 13.3" I got a few months ago.  It was marked down to $18.99 (compared to $26.50 online).  It's a pretty nice looking laptop bag with built-in padding and tons of pockets/storage.  It has an interesting feature in that it comes with multiple straps so you can keep the green one out for a young/hip look, or put the dark one one for a more serious professional look.

  © Blogger template 'Minimalist G' by 2008

Back to TOP