May 222012

I’ve been hacking up a remote shell for our actor based broadcasting system. It’s a read only shell with commands like cd, ls, stat etc. It uses Akka, Netty and Scala with its parser combinators. I was thinking a bit about how to resolve a path. By resolve I mean how to combine the current path with one provided for one of the commands above and making it absolute. My goals was (as always) to only work with immutable data structures (if possible) and make it as tight as possible. By the way, the path that I’m parsing looks like a regular UNIX path. I.e. /foo/bar/../xyz. Here’s the implementation I chose:

def resolvePath(path: List[String]): List[String] = {
  path match {
    case Nil | ".." :: Nil => Nil // Root
    case x @ _ :: Nil => x // Only one element. Not '..'
    case xs =>
      (xs :\ 0 -> List.empty[String]) { (x, acc) =>
        val (skip, ys) = acc
        if (x == "..") skip + 1 -> ys // An element to skip
        else if (skip == 0) 0 -> (x :: ys)  // Prepend element to list
        else skip - 1 -> ys // Decrement skip count
      }._2 // Return path

As you can see the method takes the path as a list of strings. The path /foo/bar would be the list "foo" :: "bar" :: Nil after it’s been parsed. I use a right fold when building up the resolved path. I could have used a left fold which would have made it a bit tighter, but then I would have to reverse the list when done to get the path elements in the correct order. It wouldn’t be that much of an overhead in most cases but I think that a right fold is more appropriate. Why don’t you show me your implementation? Choose your language of choice.

May 012012

One way to be creative and find solutions to a problem is to use provocation. A provocation is basically a some weird, probably unrealistic statement that is thrown out. The statement in it self is not useful but it helps us to come up with new ideas. It is important that we don’t start to analyse the provocation and block our selfs but instead accept any ideas that follow. Just go with the flow, and record all you come up with. Analyzing and jugement can be done later.

Three easy ways to create provocations are
1. Reverse something we take for granted. We expect cars to be mobile: “Lets build a car that is stationary”
2. Exaggerate. “Everyone is a millionaire”, “Each phone call can only last 10 seconds”
3. Distortion of a normal sequence of events. “You close the letter after you post it”

Edward de Bono has written lots of books on the subject.