Tuesday, May 10, 2011

Do You Speak Cronish?

A while back, I spoke about writing a cron dsl. Well, some progress has been made.

Tuesday, April 12, 2011

99 Problems

I ran across an awesome blog post today titled Programming Problems To Improve Your Language Skills. I never actually wrote anything in Haskell yet, but I thought that solving the Ninety-Nine Lisp Problems was a great way to start. That's the point of this post. There is, however, a goofy Scala snippet that's in order first.

Friday, April 1, 2011

Had it All Wrong

Development comes in spurts. Even though I'm in the middle of several personal projects, sometimes a simple change in pace really helps. I took a break from coding to dive in some Haskell (some break). I ran across an online book, published by Miran Lipovaca, that looked fun and interesting enough to learn the language: Learn You a Haskell for Great Good.

I read through five chapters last night, only to discover that I should have learned Haskell before learning Scala. I now see how much concepts Scala has pulled from Haskell. My opinions on the experience thusfar will be expounded on after the jump.

For the Great Good, Indeed

The more I understand about Haskell, the more I love it. Due to my time learning Scala, I feel very at home with the language. The point of this post is to observe similarities with the two, as I go about learning.

The last chapter I read was about recursion, something I understand very well at this point. As Miran describes, the poster child for Haskell is the quicksort implementation.

quicksort :: (Ord a) => [a] -> [a]  
quicksort [] = []  
quicksort (x:xs) =   
    let smallerSorted = quicksort [a | a <- xs, a <= x]  
        biggerSorted = quicksort [a | a <- xs, a > x]  
    in  smallerSorted ++ [x] ++ biggerSorted

Now a side by side Scala implementation...

// Need this for Haskell like Ord type
implicit Ordered._

def quicksort[A: Ordering](ls: List[A]): List[A] = ls match {
  case Nil => Nil
  case x :: xs =>
    val (smaller, bigger) = xs partition(_ <= x)
    quicksort(smaller) ++ List(x) ++ quicksort(bigger)
}

The Breakdown

Time for a line by line comparison.

// Haskell
quicksort :: (Ord a) => [a] -> [a]  
// Scala
def quicksort[A: Ordering](ls: List[A]): List[A]

The first line is Haskell's way of defining a function. quicksort takes a single parameter, which is a list of type a defined to be Ord. It will return a list that is type a. More specifically, it will return a sorted one.

The Scala implementation does just as well.

// Haskell
quicksort [] = []  
// Scala
ls match {
  case Nil => Nil

The seeming redefinition of quicksort is simply constructing a pattern match. That bit of Haskell specifically states: Given an empty list, an empty list will be returned.

In Scala, you have to be a bit more explicit in your declaration of a pattern match, initiated by the keyword match. A match expression will return something (in this case, we've explicitly defined its type as a List[A], so we better do that). Staying true by our line by line analysis, should ls be Nil (or empty), return empty.

// Haskell
quicksort (x:xs) =   
// Scala
  case x :: xs =>

Should the input parameter be a non-empty list, extract the head of the list from its remainder, and do some work. A singleton list will be extracted thusly:

x:[]

Scala supports pattern matching and extraction in its pattern matching very similarly. If a Scala object defines an unapply method then you can take advantage of this behavior. Once again, we see how Scala caters to this way of thinking.

// Haskell
    let smallerSorted = quicksort [a | a <- xs, a <= x]  
        biggerSorted = quicksort [a | a <- xs, a > x]  
// Scala
    val (smaller, bigger) = xs partition(_ <= x)
// Optional rewritten like so
    val smaller = quicksort(xs.filter (_ <= x))
    val bigger = quicksort(xs.filter (_ > x))
// Scala for comprehensions
    val smaller = quicksort(for(a <- xs; if a <= x) yield(a))
    val bigger = quicksort(for(a <- xs; if a > x) yield(a))

Haskell is binding some variables to use in its in statement which we'll go into in a minute. Haskell has incredible list comprehensions. You can filter, pattern match, use let expressions, etc.

Scala's answer to Haskell's list comprehensions, is the for comprehension. For this example, though, it's a bit overkill. List's in Scala inherit from a Traversable type which comes with powerful operations: map, zip, filter, reduce, to name a few.

We're almost done.

// Haskell
    in  smallerSorted ++ [x] ++ biggerSorted
// Scala
    quicksort(smaller) ++ List(x) ++ quicksort(bigger)
// Or other implementations above
    smaller ++ List(x) ++ bigger

Haskell will now use the bindings set before and work. Haskell will do its work lazily, which is awesome.

Scala list concatenation with other Lists is done exactly the same way as Haskell.

Pretty cool. I'm looking forward to learning more.

-- Philip Cali

Saturday, March 26, 2011

Dropbox Documentation Server

What does this even mean? Well, it means I'm really cheap, and lazy. It probably means I'm silly, too, when I tell you what I'm doing.

First, a back story is in order. I was reading some internet articles the other day, when completely irrelevantly, I thought of making my Public Dropbox folder a static web site. Dropbox has a really neat desktop tool that syncs a folder on your machine to their cloud storage, automagically. Being that the site would be static, I started to wonder what I could even do with it. Then I realized: this is perfect for a documentation server! (Maybe not!)

Tuesday, March 15, 2011

Monido, What?

I've been working on a little project called Monido, which is simply a monitoring service for Scala. It can monitor anything from a file, a directory, a DB table, or web page. I think you get the picture.

The project, however, only ships with a single reference implementation to monitor the file system for changes. I'm using it for this very post at this moment. More on that later.

Wednesday, February 23, 2011

Scalendar v 1.0

Update

It is now hosted on both scalendar's github page, and on scala-tools. It was approved a couple of days ago, and hopefully, other people might find it useful.

As I worked on my wife's ipad app, I kept refining my Scala Calendar wrapper (which I coined Scalendar thanks to brad). It began to evolve into a really nice api, in which you work with immutable objects that wrap the Java Calendar api. While it completely interoperates with Java time, it works well enough as a stand-alone library.

The main functionality like date traversal and property getters haven't changed from the last post. If you're interested, read more about the changes after the jump.

Friday, February 11, 2011

Manipulating time

Handling time using the standard Java libraries can be a real pain. I am writing a webapp that uses pretty complicated date arithmetic, and while Java's Calendar object works, clients of the library are forced to work with a mutable object, thus being rather verbose to accomplish a simple task.

Wednesday, January 26, 2011

Three Programming Languages

I ran across this post a few months ago, that I recently given some more thought to while making supper. (Funny how the brain works isn't it?) I've come up with my "three" programming languages as of this writing, and I'd like to share them with you.

Sunday, January 23, 2011

Who needs a Template Engine?

My wife commissioned me for another web app this weekend, and we spec'd it out using an app on her iPad (We both had a lot of fun). I wanted to prototype this sucker as quick as possible, and to be frank, my template engine I wrote for Scalatra wasn't cutting it. It dawned on me rather suddenly, something my former co-woker mentioned to me in passing.

"Scala supports in-line XML literals... Why not just go with that?"

And here I am almost a year later, saying to myself: Huh... you have an excellent point....

Monday, January 17, 2011

Remote Control v0.1 Alpha

The remote control package has finally reached an alpha stage today. The library includes ways for a server program to give client programs a way to change its behavior on the fly, or to run arbitrary code on command, and receive values from those arbitrary commands.

Thursday, January 13, 2011

Dynamicism with Context Preservation

In my last post I talked about changing some code in real time. One of the problems with my current approach was the loss of the program context. If we are using my Scalabot example, this means that I can make no changes to ScalaBot from the dynamic portions, not dynamic calls, etc. I was limited to interacting with a constant data store. I have since tweaked my library which allows for context preservation, if context is important to the dynamic call.

Wednesday, January 12, 2011

Real-Time Solutions via ScalaBot

I was talking about a pseudo hot swapping solution in my last post. I briefly spoke of its potential, but at the time of its writing, I wasn't even sure it would solve problems. I decided a good way for me to find out if it was capable of solving those problems, would be to build something with it.

Tuesday, January 11, 2011

Scala Hot Swapping (Pseudo Hot Swap)

I wrote some library code recently, triggered by a post I read. The gist of the author's post was to compare Scala and Erlang. I actually agree with him. I want to bring your attention to where I come into this, Hot code swapping.