Scala - Scalable Language

50
PRAJWAL – “secret member of fsociety” PRAMOD – “the silent assassin” PRANAV – “datatype: short” PREETI – “miss meoooowwww”

Transcript of Scala - Scalable Language

Page 1: Scala - Scalable Language

PRAJWAL – “secret member of fsociety”PRAMOD – “the silent assassin”PRANAV – “datatype: short”PREETI – “miss meoooowwww”

Page 2: Scala - Scalable Language

2INTRODUCTION

Scalable Language “It grows, with you” Influenced by: Java, Haskell, Scheme, Smalltalk, Erlang, OCaml,

etc. Java interop Functions are objects

Page 3: Scala - Scalable Language

3PARADIGM

Object-Oriented Functional Imperative Concurrent Strong Static Typing

Page 4: Scala - Scalable Language

4HISTORY The design of Scala started in 2001 at EPFL, Switzerland by

Martin Odersky. Released publicly in early 2004 on the Java platform. Released on the .Net platform in June 2004. Scala version v2.0 was released in March 2006. On 12 May 2011, Odersky & collaborators launched Typesafe Inc.

a company to provide commercial support,training & services for Scala.

The current version of Scala in use is v2.11.7 released on 23 June 2015

Page 5: Scala - Scalable Language

5Java is good & Scala is a LOT like it! Scala and Java are (almost) completely interoperable. Scala classes are Java classes, and vice versa. Call Java methods from Scala? No problem! Call Scala methods from Java? Some restrictions, but mostly OK.

Scala on JVM Scala programs are saved with the file extension .scala scalac compiles Scala to Java bytecode Scala compiles to .class files, and can be run with either the Scala command or the java command. Any Java class can be used from Scala.

Page 6: Scala - Scalable Language

6INTEGRATED DEVELOPMENT ENVIRONMENT (IDE)

The best IDE for Scala is IntelliJ IDEA. It provides a high level of code assistance and developer tools for Scala. Main Features :

- Productive coding.

- Quality tracking.

- Integrated tools.

Few IDEs for Scala :

Page 7: Scala - Scalable Language

7DATA TYPES

Page 8: Scala - Scalable Language

8BASIC SYNTAX ANALYSIS Case sensitive Comments same as Java Class names to begin with uppercase letter Method names to begin with lowercase letter Allows Interactive Mode Programming

Page 9: Scala - Scalable Language

9JAVA vs SCALA

var – Immutable variableval – Mutable variable

unnecessary

Last executed statement is function’s valueimport java.io.* import java.io._array[i] array(i)List<String> List[String]void Unit

Page 10: Scala - Scalable Language

10SYNTAX ANALYSIS Everything, EVERYTHING is an expression FUNCTION CALLS:

foo() = foo | thread.send(signo) = thread send(signo) CONTROL STRUCTURE:

if (true) do this else do that

news match { case "good" => println("Good news!") case "bad" => println("Bad news!") }

Note the space

Page 11: Scala - Scalable Language

11CONTROL STRUCTURES for (i <- 1 to 4) or for (i <- 1 until 4) do…while & while same as Java SEQUENCE COMPREHENSIONS:

Page 12: Scala - Scalable Language

12BASIC DATA STRUCTURES

List(Linked Lists): Immutable; Generally preferred in Scala over Arrays which are mutable by default. Arrays used only for cases of fast random access.

Sets: Collection contains no duplicates; Immutable & mutable

Page 13: Scala - Scalable Language

13BASIC DATA STRUCTURES Tuple: Immutable; Unlike an array or list, it can hold objects of

different data types. No named accessors, accessed via positions.

Map: Collection of key-value pairs. Values retrieved based on key. Support both mutable and immutable.

Page 14: Scala - Scalable Language

14

Page 15: Scala - Scalable Language

15HIGH ORDER FUNCTIONS sum() is a high order function, something like callbacks we come

across in C and Java. Int => Int represents a function which accepts Int and returns

an Int

Page 16: Scala - Scalable Language

16ANONYMOUS FUNCTIONS We can write nameless-anonymous functions on the fly x => x*x is a function which takes an ”x” and returns x*x

Page 17: Scala - Scalable Language

17OBJECT-ORIENTED PROGRAMMING Standalone Objects vs. Classes Inheritance in Scala A look at abstract classes

Page 18: Scala - Scalable Language

18STANDALONE OBJECTS What is an Object???

It is a structure that encapsulates data and functions

What is so special about Scala's Standalone Objects? They are not instances of any class.(Hence the name Standalone) They can extend other classes.(Wow) They can contain only definitions of either variables or methods. No

declarations.

Problem? Cut-Paste duplication.

Page 19: Scala - Scalable Language

19CLASSES

Similar to java.

What's different in Scala? Constructors are not explicitly defined.( What?!) Rather, Scala allows us to define a set of parameters which act as

constructors during the instantiation of class objects.

Page 20: Scala - Scalable Language

20STANDALONE OBJECTS VS. CLASSES

Only definitions no declarations

Definitions and declarations.

Do not take parameters May or may not take parameters.

Do not belong to any class Class objects are instances of the class

Support Inheritace and function/variable overriding

Support Inheritacnce and function/variable overriding

Page 21: Scala - Scalable Language

21INHERITANCE

Again similar to java but more flexible Scala allows method overriding, variable/value overriding Overriding can be done using the 'override' modifier( similar to the @Override Notation in Java)

Page 22: Scala - Scalable Language

22ABSTRACT CLASSES What is an Abstract class?

An abstract class is a class that is declared abstract —it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed

Why do I need an Abstract class in Scala when I have traits? It is not a question of why but a question of when.

Page 23: Scala - Scalable Language

23ABSTRACT CLASSESSo to paraphrase, when do I need an Abstract Class in Scala ?

I will choose an abstract class over a trait when I want my base class to have constructor parameters

Overriding can be done using the 'override' modifier( similar to the @Override Notation in Java)

Page 24: Scala - Scalable Language

24TRAITS In its most basic use, a Scala trait is just like a Java interface. Just like Java classes can implement multiple interfaces, Scala

classes can extend multiple traits.

Page 25: Scala - Scalable Language

25TRAITS

Page 26: Scala - Scalable Language

26TRAITS If a class extends a trait but does not implement the abstract

methods defined in that, it must be declared abstract

Page 27: Scala - Scalable Language

27TRAITS

In other cases, one trait can extend another trait.

Page 28: Scala - Scalable Language

28TRAITS

If a class extends one trait, use extends keyword. If a class extends multiple traits, use extends for the first trait

and with to extend the other traits. If a class extends a class (or a abstract class ) and a trait, always

use extends before the class name and use with before the trait name(s)

Page 29: Scala - Scalable Language

29TRAITS: ABSTRACT AND CONCRETE FIELDS

Page 30: Scala - Scalable Language

30TRAITS: ABSTRACT AND CONCRETE FIELDS

Page 31: Scala - Scalable Language

31FILE HANDLING

Page 32: Scala - Scalable Language

32READING TEXT FILES

Page 33: Scala - Scalable Language

33WRITING TEXT FILES Scala doesn’t offer any special file writing capability, so fall back

and use the Java PrintWriter or FileWriter approaches.

Page 34: Scala - Scalable Language

34HANDLING EXCEPTIONS

Page 35: Scala - Scalable Language

35INTERACTION WITH DATABASES

With Scala, you can interact with traditional relational databases using their JDBC drivers, just like you do in Java.

Connecting to MySQL with JDBC:

Page 36: Scala - Scalable Language

36

Page 37: Scala - Scalable Language

37MATCH EXPRESSIONS

Match expressions (and pattern matching) are a major feature of the Scala programming language.

If you’re coming to Scala from Java, the most obvious uses are: • As a replacement for the Java switch statement • To replace unwieldy if/then statements. In general, a match expression used as the body of a function will

accept a parameter, as input, match against that parameter, and then return a value.

Page 38: Scala - Scalable Language

38

Page 39: Scala - Scalable Language

39

Page 40: Scala - Scalable Language

40PATTERNS IN STRINGS Create a Regex object by invoking the .r method on a String, and

then use that pattern with findFirstIn when you’re looking for one match, and findAllIn when looking for all matches.

Page 41: Scala - Scalable Language

41WEB SERVICES Creating a JSON String from a Scala Object:

Page 42: Scala - Scalable Language

42 Creating a GSON String from a Scala Object:

Page 43: Scala - Scalable Language

43CURRYING In computer science, currying, invented by Moses Schönfinkel

and Gottlob Frege, is 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).

Page 44: Scala - Scalable Language

44CURRYING: TO EXISTING METHODS

Page 45: Scala - Scalable Language

45GOOD PROGRAMMING TECHNIQUES Whitespace -Indent by two spaces. -Avoid lines greater than 100 columns in length. -Use one blank line between method, class, and object definitions.

Naming - Use short names for small scopes. - Use longer names for larger scopes. - Use common abbreviations. - Don't rebind names for different uses. - Use descriptive names for methods that return values. - Don't repeat names that are already encapsulated in package or object name.

Page 46: Scala - Scalable Language

46

Import - Sort import lines alphabetically. - Use braces when importing several names from a package. Eg: import com.twitter.concurrent.{Broker, Offer} - Do not use relative imports from other packages. - Put imports at the top of the file. Braces - Braces are only used to create compound expressions. - Avoid using braces for simple expressions. Comments - Document APIs but do not add unnecessary comments

GOOD PROGRAMMING TECHNIQUES

Page 47: Scala - Scalable Language

47MARKET TRENDS

Page 48: Scala - Scalable Language

48Scala Developer Jobs Demand Trend in London

Page 49: Scala - Scalable Language

49REAL WORLD APPLICATIONS In April 2009, Twitter announced that it had switched large portions of its

backend from Ruby to Scala and intended to convert the rest.

The New York Times revealed in 2014 that its internal content management system Blackbeard is built using Scala.

Swiss bank UBS approved Scala for general production usage.

LinkedIn uses the Scalatra microframework to power its Signal API.

Page 50: Scala - Scalable Language

50

GOT [email protected]