Stubbisms – Tony’s Weblog

July 15, 2008

Scala LoveGame enspired Overview presentation

Filed under: Java, Scala — Tags: , — Antony Stubbs @ 2:38 am

These are the slides from the presentation I have been giving on Scala, enspired by the love game – as described in my previous post.

Unfortunatly I still don’t have any video of any of the presentations.

And for the sake of Google, here’s the text from the slide…

A brief Scala introduction
Or LoveGame in 4 lines of code

Move over Java, here comes Scala

Some questions

§ Please ask questions if you are curious! Interactive conversation!

§ 43 slides and counting less than two minutes per slide, so well be lucky! Just how keen are you?

§ Who knows or has used a functional language before?

§ Who has heard of Scala? Erlang, Lisp, Scheme?

§ Who knows what a POM is? (not pom poms 😉

§ I had tried out Groovy (Grails ++) but

§ I kept hearing something called Scala mentioned with a lot of respect, wanted to give it a go, just needed an excuse!

Quick love game run through

§ Who knows what the love game is?

§ Going to demonstrate the language through writing an algorithm

§ Demonstrate on the board

The Love Game

§ We will start off by going over the Love Game java pseudo code

( I actually had trouble fitting it all onto the slide!!)

Java – LoveGame

DISPLAY argument 0 + + argument 1 + + argument 2 + + argument 3 + + argument 4

FORMAT bothNames = argument 0 + argument 1 + argument 3 + argument 4

CHANGE bothNames toLowerCase()

FORMAT compWord = argument 2

CHANGE compWord toLowerCase()

//Creates two character arrays to enable comparison.

CHANGE FORMAT bothNames to character array bothNamesArray

CHANGE FORMAT compWord to character array compWordArray

//Creates an Integer array to store count results

FORMAT Integer Array tallyArray = new Integer Array [compWord.length]

FORMAT tallyArrayPointer = 0

FORMAT matchCounter = 0

//While loop counts the occurrences of each letter.

PROCESS WHILE tallyArrayPointer is less than compWord.length

PROCESS FOR i = 0 ; i is less than bothNames.length; increment i

FORMAT char nameLetter = element i in array bothNamesArray

FORMAT compLetter = element tallyArrayPointer in array compWordArray

IF nameLetter = compLetter

THEN increment matchCounter

ENDIF

tallyArray[tallyArrayPointer] = matchCounter

END FOR

RESET matchCounter

INCREMENT tallyArrayPointer

END WHILE

Java LoveGame 2

DECLARE tallyCounter

DECLARE totalAdder

//Calculates the percentage compatibility by taking consecutive elements in the array and

//adding them together.

PROCESS WHILE the third element of tallyArray is not equal to -1

RESET tallyCounter

PROCESS WHILE tallyCounter is less than length of tallyArray -1 & tallyArray[tallyCounter+1] is not equal to -1

totalAdder = tallyArray[tallyCounter] + tallyArray[tallyCounter + 1]

tallyArray[tallyCounter] = totalAdder

INCREMENT tallyCounter

END WHILE

tallyArray[tallyCounter] = -1

END WHILE

DECLARE finalPercentage = (1st element of tallyArray + 2nd element of tallyArray) * 2

DISPLAY Calculated compatibility = finalPercentage %

Scala – LoveGame

History

§ Scala was created at the École Polytechnique Fédérale de Lausanne (EPFL) in 2001 by Martin Odersky.

§ It was released publicly on the Java platform in January 2004, and on the .NET platform in June the same year. A second version of the language was released in March 2006.

§ General purpose

§ Express common programming patterns in a concise, elegant, and type-safe way

§ Stands for scalable language.

Designed to grow with the demands of its users.

Wide range of programming tasks, from small scripts to building large systems.

§ Smoothly integrates features of object-oriented and functional languages.

§ Interoperable with Java. Both ways.

§ Aims to make the most common case, easy.

§ It is both a scripting language and a compiled one.

§ Imutability and lack of side effects are key to distributed paralleism which are features of functional languages – a modern issue facing IT today with multi core process.

§ So – compatibility, brevity, high-level abstractions

Woah. (Scala Features)

§ Scala is object-oriented

Everything is an object!

every value, even primitives, are an object

Every function is an object

Most operators are actually methods! (operator overloading)

2 * (4 + 6) is actually compiled to (4.+(6)).*(2)

Types and behaviour of objects are described by classes and traits

Multiple inheritance solution with mixins of traits

Strong type system type inference, generics

singleton objects,

Traits

Uniform access principle – “…client code should not be affected by a decision to implement some attribute as a field or as a method.

Woah. (Scala Features)

§ Scala is functional

every function is a value

Higher-order functions

Anonymous functions

Function currying / partial functions

(multiple parameter lists)

Lazy evaluation

Infinite ranges

built-in support for pattern matching

Woah. (Scala Features)

§ Scala is statically typed

Not dynamic No speed trade offs (Compared to Groovy and Ruby), but less flexible at runtime (can be argued either way)

But – Local type inference mechanism dont need to specify types if they can be inferred

Generics (parameterised types)

§ Scala is extensible (scalable)

Implicit type conversions

Allows language extensions – empowers you to create new language constructs

The much beloved Pimp my Library pattern

Unique combination of language mechanisms makes it easy to smoothly add new language constructs in the form of libraries

§ Interoperates with Java and .NET (.NET support is rough atm)

§ Optional semi colons, often option parenthesis and dots

§ Built in language support for XML processing through natural extention of pattern matching

§ Great community

Active mailing list Martin himself replied to my query about Regex Parsers

Recursive version

§ For demonstration purposes, we will work with my original recursive (slightly longer) version

val initialList = “loves”.toList.map( x => “roger federer maria sharapova”.toList.count(x == _ ))

def loveReduce(numbers:List[Int]):Int = numbers match {

case head :: Nil => head * 2

case _ => loveReduce(numbers.zip(numbers.tail).map{case (a, b) => a+b})

}

println(“Compatibility = “ + loveReduce(initialList) + ” %”)

Line by line The Map Function and Higher-Order Functions

val initialList = “loves”.toList.map( F(x) )

[ 1, 2, 3, 4]

F(x) ——à
> [ f(1), f(2), f(3), f(4) ]

If F(x) = x^2 we get :

> [ f(1), f(4), f(9), f(16) ]

F(x) = “roger federer maria sharapova”.toList.count( H(y,x) )

H(y,x) = y == x

val initialList = “loves”.toList.map( x => “roger federer maria sharapova”.toList.count( y => y == x ))

Method Signatures

§ public int loveReduce(List<Integer> numbers) {…}

§ def loveReduce(…)…

§ def loveReduce(numbers:List[Int]):Int …

(Need to specify return type only for recursive functions, otherwise the compiler invokes the type inference mechanism)

§ def loveReduce(numbers:List[Int]):Int = numbers match {…}

Dont need surrounding braces as its a single statement

Recall Java Case statements

// checks if a number between 1 and 10 is prime

int number = 4;

switch (number) {

case 1: return true;

case 2: return true;

case 3: return true;

case 5: return true;

case 7: return true;

default: return false;

}

Recall Java Case statements

§ If each case statement doesnt return, need to also include break statements to prevent unwanted fall through

switch (number) {

case 1: object.operation1(number);

break;

case 2: object.operation2(number);

break;

case 3: object.operation3(number);

break;

case 5: object.operation4(number);

break;

case 7: object.operation5(number);

break;

default: object.defaultOp(number);

}

Pattern Matching in Scala Java case statements on steroids

§ Generalised form of case statement

But more powerfull

§ No fall through (common case)

// checks if a number between 1 and 10 is prime

number match {

case 1 => return true;

case 2 => return true;

case 3 => return true;

case 5 => return true;

case 7 => return true;

case _ => return false; //similar to Java’s default case

}

Case statements in Scala

§ But we can do better

§ Drop the semicolons, of course

§ Drop the return statements

Again, scalas type inference kicks in return value of function is simply last resolved statement

// checks if a number between 1 and 10 is prime

number match {

case 1 => true

case 2 => true

case 3 => true

case 5 => true

case 7 => true

case _ => false

}

Clean eh?

Pattern matching

§ List constructor the cons notation ::

head :: tail // matches 1 or more (can also use foo :: bar )

foo :: 2 // 2 element list where 2nd element is integer 2

head :: Nil // Nil (is an object extends List) represents an empty list matches 1 element list

x :: xs // matches 1 or more

§ Nil and :: are actually case Classes!

remember, there is nearly no restriction on characters used for class and method names etc

Any method which takes a single parameter can be used as an infix operator but :: (cons) when used in pattern matching is a special case

That is, the infix operator :: here, is treated as a constructor pattern ::(foo, bar)

From scala-lang.org: Case classes are regular classes which export their constructor parameters and which provide a recursive decomposition mechanism via pattern matching.

Checks the entire tree of the objects match

Can do things like: case Foo(x, Bar(y)) if x == y => true

numbers match {

case head :: Nil => head * 2

case _ => {…}

}

Recursion in Functional Scala

§ Who can describe what a recursive function is?

Recursion in computer programming defines a function in terms of itself.

Recursion: *see Recursion.

def loveReduce()…{

loveReduce(g(x))

}

Our reduce function

§ For g(x) we need to return a list one element smaller in order for our recursion to work (i.e. reduce)

Step 1

numbers.zip(numbers.tail)

Step 2

.map{x => x._1 + x._2 }

§ Nicer syntax using a match expresion in place of a function literal

.map{ case (a,b) => a + b }

A match expression can be used anywhere a function literal can be used. Essentially, a match expression is a function literal, only more general.

Recursion in Functional Scala

loveReduce(numbers.zip(numbers.tail)

.map{case (a, b) => a+b})

§ Each call, calls loveReduce, with a list one element smaller (due to the zip call)

Putting it all together -> Recursion

def loveReduce(numbers:List[Int]):Int =

numbers match {

case head :: Nil => head * 2

case _ => loveReduce(numbers.zip(numbers.tail)

.map{case (a, b) => a+b})

}

NB: Don’t need surrounding curly braces because it’s a single statement

Java – LoveGame

DISPLAY argument 0 + + argument 1 + + argument 2 + + argument 3 + + argument 4

FORMAT bothNames = argument 0 + argument 1 + argument 3 + argument 4

CHANGE bothNames toLowerCase()

FORMAT compWord = argument 2

CHANGE compWord toLowerCase()

//Creates two character arrays to enable comparison.

CHANGE FORMAT bothNames to character array bothNamesArray

CHANGE FORMAT compWord to character array compWordArray


//Creates an Integer array to store count results

FORMAT Integer Array tallyArray = new Integer Array [compWord.length]

FORMAT tallyArrayPointer = 0

FORMAT matchCounter = 0

//While loop counts the occurrences of each letter.

PROCESS WHILE tallyArrayPointer is less than compWord.length

PROCESS FOR i = 0 ; i is less than bothNames.length; increment i

FORMAT char nameLetter = element i in array bothNamesArray

FORMAT compLetter = element tallyArrayPointer in array compWordArray

IF nameLetter = compLetter

THEN increment matchCounter

ENDIF

tallyArray[tallyArrayPointer] = matchCounter

END FOR

RESET matchCounter

INCREMENT tallyArrayPointer

END WHILE

DECLARE tallyCounter

DECLARE totalAdder

//Calculates the percentage compatibility by taking consecutive elements in the array and

//adding them together.

PROCESS WHILE the third element of tallyArray is not equal to -1

RESET tallyCounter

PROCESS WHILE tallyCounter is less than length of tallyArray -1 & tallyArray[tallyCounter+1] is not equal to -1

totalAdder = tallyArray[tallyCounter] + tallyArray[tallyCounter + 1]

tallyArray[tallyCounter] = totalAdder

INCREMENT tallyCounter

END WHILE

tallyArray[tallyCounter] = -1

END WHILE

DECLARE finalPercentage = (1st element of tallyArray + 2nd element of tallyArray) * 2

DISPLAY Calculated compatibility = finalPercentage %

Scala – LoveGame

For the cynics out there smaller Java implementation

public static int loveMatch(String name1, String name2, String compWord) {

int [] tally = new int[compWord.length()];

for (char c : (name1 + name2).toCharArray())

for (int t = 0; t < tally.length; t++)

if (c == compWord.charAt(t)) tally[t]++;

for (int i = tally.length 1; i >= 0; i–)

for (int j = 0; j < i; j++)

tally[j] += tally[j + 1];

return tally[0] * 2;

}

And for the side ball.

§ C#.NET LINQ

var loves =loves.Select(x =>roger federer maria sharapova.Count(c => x == c)).ToList();

while (loves.Count > 1)

loves = Enumerable.Range(0, loves.Count 1).Select(i => loves[i] + loves[i + 1]).ToList();

Console.WriteLine(loves.Single() * 2);

More Scala to come

§ Pimp my library!

Implicit conversion functions

Examples:

MyRichString

E.g. JScience interface

Measure length = Measure.valueOf(50, SI.CENTI(SI.METER)).plus(Measure.valueOf(25.0, SI.METER));

Measure lengthInCentimeters = length.to(SI.CENTI(SI.METER));

System.out.println(“length in centimeters is ” + lengthInCentimeters.getEstimatedValue());

VS

var len = 50.centimeters + 25.meters

println(“length in centimeters is + len)

More Scala to come

§ Class properties VS instance variables

Static?

§ Currying

the technique of transforming a function that takes multiple arguments into a function that takes a single argument (the other arguments having been specified by the curry).

§ Advanced Pattern Matching and Case Classes

§ Actors Library

concurrent processes that communicate by exchanging messages.

Combinator Parsing

§ Define language parsers out of context free grammar definitions

§ 8 * ( 4 + 2 )

expr ::= term { ‘+’ term | term}.

term ::= factor { ‘*’ factor | ‘/’ factor}.

factor ::= numericLit | ‘(‘ expr ‘)’.

Combinator Parsing

object ArithmeticParsers extends StandardTokenParsers {

lexical.delimiters ++= List(“(“, “)”, “+”, “-“, “*”, “/”)

def expr: Parser[Any] = term ~ rep(“+” ~ term | “-” ~ term)

def term = factor ~ rep(“*” ~ factor | “/” ~ factor)

def factor: Parser[Any] = “(” ~ expr ~ “)” | numericLit

}

val tokens = new lexical.Scanner(args(0))

println(args(0))

println(phrase(expr)(tokens))

§ This of course also parses ( 36 / 8 * ( 4 + 2 ) ) / ( ( 5 3 ) * 6 )

Combinator Parsing

§ More complex and practical example:

§ (buy 100 IBM shares at max USD 45, sell 50 CISCO shares at min USD 25, buy 100 Google shares at max USD 800) for trading_account “SSS1234”

§ http://debasishg.blogspot.com/2008/04/external-dsls-made-easy-with-scala.html

Combinator Parsing

// ClientOrder.java

public class ClientOrder {

public enum BuySell {

BUY,

SELL

}

private String accountNo;

private List<LineItem> lineItems = new ArrayList<LineItem>();

// LineItem.java

public class LineItem {

private final String security;

private final int quantity;

private final ClientOrder.BuySell bs;

private final int price;

public LineItem(String security, int quantity, ClientOrder.BuySell bs, int price) {

this.security = security;

this.quantity = quantity;

this.bs = bs;

this.price = price;

Properties / Class Parameters

§ Scalas concise syntax

§ this is Java

class MyClass {

private int index;

private String name;

public MyClass(int index, String name) {

this.index = index;

this.name = name;

}

}

§ In Scala, you would likely write this instead:

class MyClass(index: Int, name: String)

Implicit Conversions (and mixins with traits)

class MyRichString(str:String) {

def countOccurances(haystack:String) =

str.toList.map( x => haystack.toList.count(x == _ ))

}

trait MyConversions {

implicit def string2MyRichString(str:String) = new MyRichString(str)

}

object MyConversionsTest extends Application with MyConversions {

val m = “cow”

println(m.countOccurances(“o”))

}

Working with XML with language level support

Consider the following XML document:

<html>

<head>

<title>Hello XHTML world</title>

</head>

<body>

<h1>Hello world</h1>

<p><a href=”http://scala-lang.org/”>Scala</a&gt; talks XHTML</p>

</body>

</html>

Working with XML with language level support

This document can be created by the following Scala program:

val page =

<html>

<head>

<title>Hello XHTML world</title>

</head>

<body>

<h1>Hello world</h1>

<p><a href=”scala-lang.org”>Scala</a> talks XHTML</p>

</body>

</html>;

println(page.toString())

Working with XML with language level support

§ It is possible to mix Scala expressions and XML:

object XMLTest2 extends Application {

import scala.xml._

val df = java.text.DateFormat.getDateInstance()

val dateString = df.format(new java.util.Date())

def theDate(name: String) =

<dateMsg addressedTo={ name }>

Hello, { name }! Today is { dateString }

</dateMsg>;

println(theDate(“John Doe”).toString())

}

Actors and Concurrency

class Ping(count: int, pong: Actor) extends Actor {

def act() {

var pingsLeft = count 1

pong ! Ping

while (true) {

receive {

case Pong =>

if (pingsLeft % 1000 == 0)

Console.println(“Ping: pong”)

if (pingsLeft > 0) {

pong ! Ping

pingsLeft -= 1

} else {

Console.println(“Ping: stop”)

pong ! Stop

exit()

}

class Pong extends Actor {

def act() {

var pongCount = 0

while (true) {

receive {

case Ping =>

if (pongCount % 1000 == 0)

Console.println(“Pong: ping “+pongCount)

sender ! Pong

pongCount = pongCount + 1

case Stop =>

Console.println(“Pong: stop”)

exit()

val pong = new Pong

val ping = new Ping(100000, pong)

ping.start

pong.start

Companion objects and Static

§ Singleton and Static considered harmful

Static methods cannot be polymorphic

Inheritance problems

So – all static members are now grouped in their own object

§ Scalas companion object’” (singleton object)

More and more and more.

§ Nested Functions

§ By-name parameters

Lazy evaluation

Logging no more stupid ( if ( log.isEnabled() ) protection! )

§ Private packages

§ LiftWeb (Rails/Grails like web app for Scala)

§ The Option type optional values Some or None

§ First-class Properties coming

Properties are objects (OO sense)

Govern access to a (possibly calculated) field.

Properties can have other methods in addition to get/set logic, such as registering event listeners.

§ Writing new control structures

val file = new File(“date.txt”)

withPrintWriter(file) {

writer => writer.println(new java.util.Date)

}

More Technologies are out there!

§ Expand your mind!

§ Online Ajax HelpRequestList program in 9 lines!!

Groovy on Grails

§ Maven the modular build system Connoisseur!

§ Groovy Builder and G-Strings

Groovy Command Line Interpreter

Groovy Wicket builder

§ Polygot programming

a computer program or script written in a valid form of multiple programming languages, which performs the same operations or output independently of the programming language used to compile or interpret it.

§ Git

a distributed revision control / software configuration management project created by Linus Torvalds, initially for the Linux kernel development.

The Full Article

§ Read the article at:

https://stubbisms.wordpress.com/2008/02/22/my-foray-into-the-world-of-scala/

§ More Scala links

scala-lang.org

§ Thanks for listening!

Advertisements

2 Comments »

  1. […] more presenting and teaching – I am still eager to make anyone who wants to learn, sit through my Scala presentation ;), even if I haven’t used Scala since I wrote the […]

    Pingback by Aaaaand we’re done. « Stubbisms - Tony’s Weblog — December 25, 2008 @ 4:09 am

  2. […] more presenting and teaching – I am still eager to make anyone who wants to learn, sit through my Scala presentation , even if I haven’t used Scala since I wrote the […]

    Pingback by Linus’ Git Talk Index Git Script to Show Largest Pack Objects and Trim Your Waist Line! Spring Modules Fork Eclipse and IPhone+Google Fail spring-modules-ehcache and ehcache issues you should be aware of Fighting Scala – Scala to Java List Conve — September 27, 2012 @ 10:32 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: