Archive

Posts Tagged ‘scala’

Introduction to Scala – Presentation draft

July 4th, 2009 No comments

This is a draft of my “Introduction to Scala” presentation, to be used at the BEKK “fagdag” in September. The presentation is written using LaTeX and Beamer. Feel free to comment on errors or improvements :)

The presentation is available on scribd.com:

Cannot view the presentation? The whole file can be downloaded as PDF here:

Note! I will update the presentation when I find errors or make other changes.

LaTeX Listings for Scala

June 30th, 2009 3 comments

To get LaTeX Listings to work for Scala I ended up with the following definition in my .tex file:

\usepackage{listings}

% "define" Scala
\lstdefinelanguage{scala}{
  morekeywords={abstract,case,catch,class,def,%
    do,else,extends,false,final,finally,%
    for,if,implicit,import,match,mixin,%
    new,null,object,override,package,%
    private,protected,requires,return,sealed,%
    super,this,throw,trait,true,try,%
    type,val,var,while,with,yield},
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
  sensitive=true,
  morecomment=[l]{//},
  morecomment=[n]{/*}{*/},
  morestring=[b]",
  morestring=[b]',
  morestring=[b]"""
}

NOTE! After writing this I came across this page (with a downloadable style file), which is probably preferred to adding the definition to every single .tex document.

To add a nice IntelliJ-like color/look & feel and a border I added the following configuration:

\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

% Default settings for code listings
\lstset{frame=tb,
  language=scala,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  frame=single,
  breaklines=true,
  breakatwhitespace=true
  tabsize=3
}

Using the listings is then straightforward:

\begin{lstlisting}
val t = "hello" // a string
val x = 42 // an int
\end{lstlisting}

Resulting in the following pretty formatted code example in a Beamer presentation:
LaTeX Beamer Scala Listing

Scala with Maven and IntelliJ IDEA

May 8th, 2009 1 comment

I have just started looking at the Scala language. So far I’ve been doing smaller examples of code, just using Emacs or simply the Scala interpreter. Now, starting a bigger project, I was thinking how to get started with a “real” development project. It proved amazingly easy, with the tools I’m used to from the Java-world.

Project structure and build – Maven

Plugins have been made to make Maven work smooth with Scala. A great page describing the details here.

The project can simply be created with Maven archetype, like this:

mvn org.apache.maven.plugins:maven-archetype-plugin:1.0-alpha-7:create \
  -DarchetypeGroupId=org.scala-tools.archetypes \
  -DarchetypeArtifactId=scala-archetype-simple \
  -DarchetypeVersion=1.1 \
  -DremoteRepositories=http://scala-tools.org/repo-releases \
  -DgroupId=mygroupid -DartifactId=myprojectid

The directory structure matches that of Java projects. Scala code goes in src/main/scala of course. All the regular commands just work (mvn test, mvn package, mvn install…).

Development environment – IntelliJ IDEA

I’ve been a big fan of IDEA for years, so using it for Scala is the obvious choice for me. Firstly get yourself a version 8.x of IDEA, then install the Scala plugin.

Since IDEA supports Maven pom.xml files as project descriptors, simply open the project created with Maven archetype. The only extra thing I had to do was add the scala-compiler.jar as a library. That’s about it. Most of my IDEA stuff seems to work right out of the box: ctrl-alt-F10 runs my code (both tests and main), shift-F6 lets me rename methods etc.

Super easy. All ready to go. Hopefully this is the start of my path away from the Java-language :)

Links used:

Tags: , ,