Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: Suggestion for an idea for a hands on session

I want to give a Scala presentation and I want to do it by taking an application and evolve it from something which uses java idioms to something that uses the power of scala (traits, pattern matching, implicit convertions, functional programming).

I'm especially interested in something that demonstrates a design change, rather than syntactic sugar. Something where the end scala code is evidently easier to maintain and extend.

So any ideas? (I'm not asking for code examples, just the rough ideas of what example to use and what design principles can be demonstrated).

like image 474
IttayD Avatar asked Jan 10 '10 10:01

IttayD


1 Answers

A wonderful example is developing a little interpreter for a dynamic mini-language.

The basic java implementation requires the classical interpreter design pattern, whereas a functional scala-approach can use many wonderful functional idioms like

  • case classes
  • pattern matching
  • higher-order functions

or maybe even monads in order to produce very clean and easily understandable code.

Just compare

class Number implements Expression {
    private int number;
    public Number(int number)       { this.number = number; }
    public int interpret(HashMap<String,Integer> variables)  { return number; }
}

with

case NumberLiteral(i) => Integer(i)

See the interpreter examples at the scala page.

like image 184
Dario Avatar answered Oct 28 '22 12:10

Dario