Posts tagged with Scala

How to get enumerations when using Scala’s parser combinators

January 23rd, 2012

This took me a while to figure out, so I figure it’s worth sharing here. In the end it’s quite simple thanks to Parser’s ^? method, but it took me a while to figure out:

Some experiments with natural numbers in Scala without type class wizardry

November 14th, 2011

Scala doesn’t have dependent types but I wanted to see if I could wrassle up something that would approximate it for non-negative natural numbers. I’ve posted my results on GitHub.

Ruby-style trailing conditionals in Scala

November 12th, 2011

Ruby code often has code with trailing conditionals, which can look pretty neat.

person = Person.get(name) unless name.nil?

(Note: I really don’t know Ruby, I just saw syntax like this and thought was cool.)

So, of course I had to implement it in Scala:

You get optional values:

Pretty cool, no?

I tried and failed to come up with some sort of fancy extractor object that would let me use unless in pattern matching:

1001 match {
  case n unless n % 2 == 0 => println(n)
}

Can you figure out a way?

Connecting to a Mongo database with Casbah

November 9th, 2011

Because I’ve wasted too much time on Casbah tonight, I have to pass it on. Simply, not much is done for you automatically. Connect like this:

val uri = MongoURI("mongodb://username:password@server:port/database")
val mongo = MongoConnection(uri)
val db = mongo(uri.database)
db.authenticate(uri.username, uri.password.foldLeft("")(_ + _.toString))
val collection = db("something")

If you call db.authenticate() a second time it will throw an exception. Lovely.

Calculating the time I’ve spent outside the country

November 9th, 2011

I need to tell the Dutch government when renewing my permit if I’ve been outside of the country for more than three months. Here’s how I calculated it thanks to ScalaTime:

Mobile APIs Talk Next Wednesday

July 23rd, 2011

Daniel Salber and I will be giving the next Appsterdam Weekly Wednesday Lecture on mobile APIs. The talk is on Wednesday July 27 at 12:30 at Vijzelstraat 20, Amsterdam.

Here is our talk description:

Many mobile apps get data or send data to a server. What are the issues in implementing this kind of apps? In this talk, Peter Robinett and Daniel Salber will share their personal experiences designing and implementing both the server and client (iOS) sides of APIs for mobile apps, taking examples from their apps Smakelijk Amsterdam, Coffeeshoppr, iFebo, and JHM. Starting from questions to ask at the beginning of a project, continuing all the way through to debugging and performance, they will lead you through a broad overview of the implementation of a mobile API in your project.

Towards a Scala DSL for Javascript

June 16th, 2011

Reposting from the Lift mailing list.

For some time know I’ve been thinking about how cool it’d be to be able to essentially write Javascript but with all the Scala goodiness behind the scenes. Lift already has abstractions for more common Javascript language elements, so I have suspected that it could be used as the basic for something that looked more like plain Javascript.

This evening I was looking at PhantomJS and realized that to use it for the type of work I need I would have to control it with lots of Javascript files generated on the fly. So, I finally sat down and hacked around. This is what I’ve come up with:

As you can see, for Javascript language features I’ve basically just put j_ before what are also reserved words in Scala. So, JsFalse becomes j_false. The same is true for break, continue, and return.

As you can see with j_if and j_for, those case classes are declared such that you get a more natural syntax, one approaching Javascript’s. For instance, you can directly call a JsVar with 0 to n parameters, without writing Call("var_name", params0, ...). I guess that the experimental Dynamic trait could be added to the pimped JsVar (RichJsVar, of course) but that’s probably going a bit far! ;-)

I’ve also added a bunch of implicits, both for my pimped classes but also to convert JsCmd to JsExp. Lift already has a conversion the other way around but there isn’t one in this direction. I’m not sure if there is no implicit for a good reason. Perhaps this will break things?

Where is this wonderful code, you ask? Why, it’s a simple Gist: https://gist.github.com/1028437!

I look forward to your comments!

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>