introduction to scala - shimi bandiel, trainologic

Post on 15-Jul-2015

102 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

copyright 2009 Trainologic LTD

Yet Another Web Framework

Spring MVC

copyright 2014 Trainologic LTD

The Road for Functional Programming

Introduction to Scala

copyright 2009 Trainologic LTD

Yet Another Web Framework

Spring MVC

2copyright 2014 Trainologic LTD

Introduction to Scala

• Scala is yet another language for the JVM.

• The first question that comes into mind is: “why do we need it”?

• After all, we have Java which is an excellent language with great performance and many libraries.

• And if we need some dynamic power, we could use Groovy, Jython, JRuby or Clojure.

• So, where does Scala fit into this JVM language picture?

Scala

2

copyright 2009 Trainologic LTD

Yet Another Web Framework

Spring MVC

3copyright 2014 Trainologic LTD

Introduction to Scala

• The main reasons for choosing Java are:

• Portability (cross platform).

• Available libraries (Spring, Hibernate, etc…).

• Easy to use (syntax).

• Many developers are available.

• Performance.

Why do we use Java?

3

copyright 2009 Trainologic LTD

Yet Another Web Framework

Spring MVC

4copyright 2014 Trainologic LTD

Introduction to Scala

• Well, not at all.

• Portability – JVM power (provided to every language that can compile to JVM bytecode).

• Libraries – You can use the libraries from (almost) any language compiled to JVM.

• Easy to use (syntax) – Well, we will see Scala’s power here.

• Performance – JVM Hotspot does great for non-reflection usage (Scala fits here great).

• Developers Availability – this is indeed a strength in Java.

Is it Really Java?

4

copyright 2009 Trainologic LTD

Yet Another Web Framework

Spring MVC

5copyright 2014 Trainologic LTD

Introduction to Scala

• Statically-typed language for the JVM.

• Pure Object Oriented.

• Very strong type system.

• 100% Java inter-operability.

• Powerful language features (e.g.: pattern-matching, implicits, macros, for-expressions).

• Provides great support for creating DSLs (Domain Specific Languages).

• Is being used by many companies (e.g.: Twitter, Linkedin, Coursera, Foursquare, Cisco, NICE).

Presenting Scala

5

copyright 2009 Trainologic LTD

Yet Another Web Framework

Spring MVC

6copyright 2014 Trainologic LTD

Introduction to Scala

• The Scala compiler has very strong type inference.

• E.g.:

Type Inference

6

def foo(i: Int) = Map("hello" -> List(5 + i, 3 + i))

val i = List(foo(6), foo(4))

/> i : List[scala.collection.immutable.Map[String,List[Int]]]

copyright 2009 Trainologic LTD

Yet Another Web Framework

Spring MVC

7copyright 2014 Trainologic LTD

Introduction to Scala

• No Primitives (everything is an object. The compiler will do the magic).

• No operators. Only Methods:

Pure OO

7

val a = 3

val b = 5

val c = a.+(b)

val c2 = a + b

copyright 2009 Trainologic LTD

Yet Another Web Framework

Spring MVC

8copyright 2014 Trainologic LTD

Introduction to Scala

• A case class provides a lot of syntactic-sugar e.g.:

• Built-in implementations for hashCode, equals and toString.

• A copy method

• Immutable fields + getters

• Support for pattern matching

Case Classes

8

copyright 2009 Trainologic LTD

Yet Another Web Framework

Spring MVC

9copyright 2014 Trainologic LTD

Introduction to Scala

• Like LINQ in the .NET environment.

• Supported for every class implementing flatMap, map, withFilter (filter).

• Out-of-box impls:

• Collections.

• Option.

• Future.

• But there are hundreds more (monads).

For Expressions

9

copyright 2009 Trainologic LTD

Yet Another Web Framework

Spring MVC

1

0copyright 2014 Trainologic LTD

Introduction to Scala

• Scala has many, many more features.

• It is considered a complex language.

• This is false, because everything that is available in Java is made simpler with Scala.

• Scala provides many features that Java lacks which provide type-safety, correct abstractions and less boiler-plate.

• And most important, it is now possible to practically do Functional Programming on the JVM with correct abstractions.

Functional Programming

10

copyright 2009 Trainologic LTD

Yet Another Web Framework

Spring MVC

1

1copyright 2011 Trainologic LTD

Introduction to Scala

• Functional Programming is all about writing (and using)

pure functions.

• A function is pure iff it returns the same result for the

same arguments and if it doesn’t introduce side-effects.

• The gain:

• Easier reasoning.

• Parallelism.

• Optimization opportunities.

• Infinite Scalability.

Functional Programming

11

top related