Scalable Generator: Using Scala in SIer Business (ScalaMatsuri)
Scala - Scalable Language
-
Upload
pranavsinghania -
Category
Education
-
view
255 -
download
0
Transcript of Scala - Scalable Language
PRAJWAL – “secret member of fsociety”PRAMOD – “the silent assassin”PRANAV – “datatype: short”PREETI – “miss meoooowwww”
2INTRODUCTION
Scalable Language “It grows, with you” Influenced by: Java, Haskell, Scheme, Smalltalk, Erlang, OCaml,
etc. Java interop Functions are objects
3PARADIGM
Object-Oriented Functional Imperative Concurrent Strong Static Typing
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
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.
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 :
7DATA TYPES
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
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
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
11CONTROL STRUCTURES for (i <- 1 to 4) or for (i <- 1 until 4) do…while & while same as Java SEQUENCE COMPREHENSIONS:
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
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.
14
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
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
17OBJECT-ORIENTED PROGRAMMING Standalone Objects vs. Classes Inheritance in Scala A look at abstract classes
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.
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.
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
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)
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.
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)
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.
25TRAITS
26TRAITS If a class extends a trait but does not implement the abstract
methods defined in that, it must be declared abstract
27TRAITS
In other cases, one trait can extend another trait.
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)
29TRAITS: ABSTRACT AND CONCRETE FIELDS
30TRAITS: ABSTRACT AND CONCRETE FIELDS
31FILE HANDLING
32READING TEXT FILES
33WRITING TEXT FILES Scala doesn’t offer any special file writing capability, so fall back
and use the Java PrintWriter or FileWriter approaches.
34HANDLING EXCEPTIONS
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:
36
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.
38
39
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.
41WEB SERVICES Creating a JSON String from a Scala Object:
42 Creating a GSON String from a Scala Object:
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).
44CURRYING: TO EXISTING METHODS
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.
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
47MARKET TRENDS
48Scala Developer Jobs Demand Trend in London
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.
50