yes scala can!

57
The place for sharing ideas and knowledge

Upload: amirmoulavi

Post on 06-May-2015

4.111 views

Category:

Education


0 download

DESCRIPTION

The presentation I had for Stockholm Knowledge Network. For demo code please refer to https://github.com/anderseriksson/yesscalacan

TRANSCRIPT

Page 1: Yes scala can!

The place for sharing ideas and knowledge

Page 2: Yes scala can!

Yes Scala Can!

Amir Moulavi Anders Eriksson @mamirm @anders_eriksson [email protected] [email protected]

Jayway Stockholm2012-05-30

Page 3: Yes scala can!

Why

● We've found Scala and like it! ● We would like to broaden the interest

amongst Java developers. ● Hopefully this presentation will inspire you to

look more into Scala!

Page 4: Yes scala can!

Agenda

● History and Background ● Scala vs. Java: Similarities and Differences ● Quick Journey to Scala ● Book Raffle!

Page 5: Yes scala can!

It has been a revolution!

Page 6: Yes scala can!

Little history and background

● 2001: ○ Design of Scala started

■ Goals: merge functional + object oriented paradigms

● 2004:○ First public release on JVM

Page 7: Yes scala can!

Little history and background

● 2006:○ major rewrite (v 2.0)

● 2011:

○ Company Typesafe is funded for professional support

Page 8: Yes scala can!

Java vs. Scala (similarities)

● Runs on JVM ● Statically typed ● Object oriented ● Garbage collected

Page 9: Yes scala can!

Java vs. Scala (differences)

● Pure object orientation ● Functional paradigm ● Pattern matching ● Mixin composition

Page 10: Yes scala can!

Java vs. Scala (differences)

● No checked exception ● Implicit conversions ● Flexible imports and access modifier ● Read Evaluate Print Loop (REPL) console

Page 11: Yes scala can!

Scala is a unifier

Page 12: Yes scala can!

Scala is Object-oriented

Page 13: Yes scala can!

Scala is Functional

Page 14: Yes scala can!

Scala is statically typed

Page 15: Yes scala can!

Scala is concisepublic class Person {

private final String name;

private final int age;

public Person(String name, int age) {

this.name = name;

this.int = int;

}

public String getName() {return name;

}

public int getAge() {

return age;

}}

Java

Page 16: Yes scala can!

Scala is conciseclass Person(val name:String, val age:Int)

Scala

Page 17: Yes scala can!

Scala is expressive (1 to 10) filter (_ > 7)

Page 18: Yes scala can!
Page 19: Yes scala can!

REPL

The command line for Scala

Page 20: Yes scala can!

Tuples

( , )( , , )( , , , )( , , , , )( , , , , , )

Page 21: Yes scala can!

Tuples

● Saves you the tedium of defining simplistic data-heavy classes

● Immutable ● Each value different type ● Can contain up to 22 values ● Indexed from 1

Page 22: Yes scala can!

REPL and Tuples

Page 23: Yes scala can!

Case classes

Page 24: Yes scala can!

Case classes

● Easier instantiation ● Immutability in constructor parameters by

default ● You get these methods for free

○ toString()○ equals()○ hashCode()○ apply()○ copy()

Page 25: Yes scala can!

Case classes

Page 26: Yes scala can!

Pattern Matching

Page 27: Yes scala can!

Pattern Matching

● Similar to Java's switch statement ● Let you match any sort of data ● First-match policy

Page 28: Yes scala can!

Pattern Matching

Page 29: Yes scala can!

Higher Order Functions

Page 30: Yes scala can!

Higher Order Functions

● Functions are first class citizens in Scala

F : A B ● Higher Order Functions are functions that

take functions as input parameters or return them as results

F : (A B) (R S)

Page 31: Yes scala can!

Higher Order Functions

map(f) filter(f)

Page 32: Yes scala can!

Powerful Collections

Page 33: Yes scala can!

Powerful Collections

● Easy to use

○ A small vocabulary of 20-50 methods to solve most collection problems

● Concise

○ A single word instead of several loops!

Page 34: Yes scala can!

Powerful Collections

● Safe○ Functional nature + Statically typed!

● Fast

○ Tuned and optimized collections' operations ● Universal

○ The same operation on any type

Page 35: Yes scala can!

Powerful Collections

Scala Collections

MutableImmutable

Page 36: Yes scala can!

Powerful Collections

● List○ Immutable and ListBuffer is mutable

● Array ○ Mutable

● Set○ Immutable and mutable

● Map○ Immutable and mutable

● ... and String!○

Page 37: Yes scala can!

Powerful Collections with Higher Order Functions

Page 38: Yes scala can!

Traits

Page 39: Yes scala can!

Traits

● Definition:

○ Provides a set of methods that implement behaviour

○ Requires a set of methods that parameterized the

provided behaviour ○ Does not specify any state variables

Page 40: Yes scala can!

Traits

● Interfaces with behaviors ● Fundamental unit of code re-use ● Extremely powerful tool in OO design

Page 41: Yes scala can!

Traits

Rich interfaces

Stackable modifications

Page 42: Yes scala can!

Traits

Page 43: Yes scala can!

Parallel Collections

Page 44: Yes scala can!

Parallel Collections

● Supports the same operations as sequential ones

● Easily convert sequential to parallel

collection

Page 45: Yes scala can!

Parallel Collections

Page 46: Yes scala can!

Scala and Java Integration

● Seamless integration ● Make full use of existing libraries and code

Scala Java

Page 47: Yes scala can!

Scala and Java Integration

● Familiar development environments○ Eclipse, IntelliJ, Netbeans, ...

● Familiar building tools like Maven

○ SBT ● If you are a Java developer, you're halfway

there!

Page 48: Yes scala can!

What's Scala advantage over Java as a language choice?

Page 49: Yes scala can!

● implicit parameters● pattern matching● case classes● type inferencing● higher-kinded types● monadic for comprehension● variance annotation● traits● default and named argument● abstract values● by-name terms● type aliasing● self types● path-dependent types● structural types● ...

Page 50: Yes scala can!

Conclusion

● Scala is not complex ● Express your ideas in a concise way ● Better code readability ● Scala = Functional + Object oriented

paradigms. ● Scala scales with programmers

Page 51: Yes scala can!

Where to go from here?

Programming in Scala, 2nd edition by Martin Odersky, Lex Spoon andBill Venners Scala for the Impatient byCay Hortsmann

Page 52: Yes scala can!

Where to go from here?

Scala Stockholm User Group http://www.meetup.com/Scala-Stockholm/

Page 53: Yes scala can!

Where to go from here?

Scala course "Fast-Track to Scala" from Typesafe 18-19 September, 2012

Page 54: Yes scala can!

Where to go from here?

Page 55: Yes scala can!

Questions?

Page 56: Yes scala can!

Thank you!

Page 57: Yes scala can!

The place for sharing ideas and knowledge