Posts tagged with Scala

Hacking in Scala

June 2nd, 2011

Two weeks ago I attended CPH Scala and gave a little presentation on ‘hacking in Scala’. As I see it, that means just using Scala for all sorts of projects, from bigger ones to little utilities thrown together in an hour or two. In general I’m quite happy with things, though I am always looking for was to speed up my process. sbt and lifty have helped a lot, though I think there’s still room for improvement.

Here’s the slides I threw together right before the meetup:

Just the other day I saw that Richard gave a presentation to Functional Brighton about his everyday functional programming in Scala. I think it’s quite nice and in a similar spirit of mine.

Today I put together a new tool I call sketch that I hope will help me become even more productive in quickly doing little things in Scala. Put simply, it’s just a bash command that launches the scala REPL with a bunch of JARs I commonly use (dispatch, lift-common, lift-json, etc). The JARs are in ~/.sketch/ and this is my command:

# scala interpreter with usual jars included in the classpath
sketch () {
    SKETCH_JAR_HOME=/Users/peter/.sketch
 
    for f in $SKETCH_JAR_HOME/*.jar
    do
        SKETCH_JAR_CLASSPATH=$SKETCH_JAR_CLASSPATH:$f
    done
 
    scala -classpath $SKETCH_JAR_CLASSPATH
}

Introducing Chaos Monkey

May 13th, 2011

At the Next Web Hackathon I whipped a fun little tool called Chaos Monkey. Inspired by Netflix, I made a Scala program that randomly takes up and down your AWS EC2 instances. Why would anyone do that? Simple, to design for and work around failure.

And it’s kinda funny!

It’s early days but perhaps you want to help make it better?

Setting the run mode and context of Jetty launched via sbt

March 8th, 2011

For my own future reference. In WEB-INF/jetty-web.xml place the following:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
  <Call class="java.lang.System" name="setProperty">
    <Arg>run.mode</Arg>
    <Arg>production</Arg>
  </Call>
  <Set name="contextPath">/sso</Set>
</Configure>

Amsterdam Lift Meetup

January 24th, 2011

If you’re not on the Lift mailing list you might not know that I am organizing a little meetup this Wednesday January 26 at 19:00 at Café Kobalt here in Amsterdam. If you’re interested in Lift, Scala, or just web frameworks in general, come on by!

Displaying Unicode Characters in the Scala REPL on OS X

October 25th, 2010

So simple but unforuntately I missed it until now:

scala -Dfile.encoding="UTF-8"

Now you can go to town with characters like ø and ∫! Oh, and you may want to alias this command in your ~/.profile:

alias scala='scala -Dfile.encoding="UTF-8"'

BFCollections

August 20th, 2010

I’ve been doing a bunch of PHP programming the last few weeks for a client and it’s been such a pain to use PHP’s archaic array_* methods. And don’t get me started on having to return arrays like array('result' => 'not_ok', 'message' => 'User not found') to indicate success or failure. I had some time to kill at the airport yesterday, so today I am happy to bring you BFCollections. The collections included are:

  • BFArray: A better array. It can be used just like a native array but it also has methods for common array operations such as map, filter, and reduceLeft.
  • Option: Indicates an optional return result, via the form of an instance of the Some class or the global $None. Inspired by Scala’s Option (naturally) and Haskell’s Maybe. There are basic methods to operate on the values.
  • Box: Again indicates an optional return result, though the lack of a value can also be indicated and even chained. Inspired by the Lift framework’s Box.

If there’s interest I’ll see about improving them and even adding additional collections.

Everyone’s Using Scala

August 13th, 2010

Twitter, LinkedIn, TomTom, Novell, Yammer, Meetup, Remember the Milk – the list goes on! Cool!

Lift Presentation at DuSE VI

July 1st, 2010

I gave an introductory presentation on Lift last night at DuSE VI. You can download the pretty PDF or view in on Slideshare. And guess what, it’s already out of date: 2.0 is now out!

Fun with Scala Implicits

June 21st, 2010

Last night I discovered that Scala’s RichString has a format method quite similar to  Python’s. However, in Python I’m used to using the % operator and I wondered if I could use the same syntax in Scala. With a little library pimping and help from my friends on Stack Overflow, I can:

class BFString(s: String) {
  def %(in: Any*) = s.format(in: _*)
}
 
implicit def add_%(s: String) = new BFString(s)
scala> "The %s costs $%d." % ("dog", 255)
res2: String = The dog costs $255.

Pretty cool!

Comparing Software Version Numbers

April 16th, 2010

Software version numbers tend to be in the format ‘Major.minor.point’, which makes sorting versions require a little bit of work. If we only had a single dot we could just convert them to numbers and use < and >. However, we often don’t and sometimes people put letters in their versions also, like ’1.2.A’.

Here’s my solution, in Scala:

def compAsc(a: String, b: String): Boolean = {
  val separator = '.'
  val aSplit = List.fromString(a, separator)
  val bSplit = List.fromString(b, separator)
 
  def comp(a: List[String], b: List[String]): Boolean = {
    !a.isEmpty match {
      case true if !b.isEmpty => {
        if (a.head == b.head) {
          // if both equal, keep comparing
          comp(a.tail, b.tail)
        }
        else {
          a.head < b.head
        }
      }
      // if a has a value but b doesn't then a is larger
      case true if b.isEmpty => false
      // if a doesn't have a value but b does then b is larger
      case false if !b.isEmpty => true
      // in all other cases assume a is larger
      case _ => true
    }
  }
 
  comp(aSplit, bSplit)
}
 
def compDesc(a: String, b: String): Boolean = {
  val separator = '.'
  val aSplit = List.fromString(a, separator)
  val bSplit = List.fromString(b, separator)
 
  def comp(a: List[String], b: List[String]): Boolean = {
    !a.isEmpty match {
      case true if !b.isEmpty => {
        if (a.head == b.head) {
          // if both equal, keep comparing
          comp(a.tail, b.tail)
        }
        else {
          a.head > b.head
        }
      }
      // if a has a value but b doesn't then a is larger
      case true if b.isEmpty => true
      // if a doesn't have a value but b does then b is larger
      case false if !b.isEmpty => false
      // in all other cases assume a is larger
      case _ => false
    }
  }
 
  comp(aSplit, bSplit)
}

Some examples:

scala> val l = List("1.24", "3.2", "3.0", "3.0.1 beta")
l: List[java.lang.String] = List(1.24, 3.2, 3.0, 3.0.1 beta)
 
scala> l.sort(compAsc)
res0: List[java.lang.String] = List(1.24, 3.0, 3.0.1 beta, 3.2)
 
scala> l.sort(compDesc)
res1: List[java.lang.String] = List(3.2, 3.0.1 beta, 3.0, 1.24)

And why, do you ask, am I working with software version numbers? Let’s just say it has something to do with www.mobtest.com. More soon!