11

Click here to load reader

Introduction to Scala - Shimi Bandiel, Trainologic

Embed Size (px)

Citation preview

Page 1: Introduction to Scala - Shimi Bandiel, Trainologic

copyright 2009 Trainologic LTD

Yet Another Web Framework

Spring MVC

copyright 2014 Trainologic LTD

The Road for Functional Programming

Introduction to Scala

Page 2: Introduction to Scala - Shimi Bandiel, Trainologic

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

Page 3: Introduction to Scala - Shimi Bandiel, Trainologic

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

Page 4: Introduction to Scala - Shimi Bandiel, Trainologic

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

Page 5: Introduction to Scala - Shimi Bandiel, Trainologic

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

Page 6: Introduction to Scala - Shimi Bandiel, Trainologic

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]]]

Page 7: Introduction to Scala - Shimi Bandiel, Trainologic

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

Page 8: Introduction to Scala - Shimi Bandiel, Trainologic

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

Page 9: Introduction to Scala - Shimi Bandiel, Trainologic

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

Page 10: Introduction to Scala - Shimi Bandiel, Trainologic

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

Page 11: Introduction to Scala - Shimi Bandiel, Trainologic

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