+ All Categories
Home > Documents > Scala-Ls1

Scala-Ls1

Date post: 21-Jan-2018
Category:
Upload: aniket-joshi
View: 187 times
Download: 1 times
Share this document with a friend
21
Learning Scala Aniket Joshi 1
Transcript
Page 1: Scala-Ls1

Learning Scala

Aniket Joshi

1

Page 2: Scala-Ls1

What is Scala?

• Scala(ska-lah) stands for Scalable Language

• Combines functional programming with

Object Oriented Programming

• Growing as per the demand of the users

• Write small scripts as well as build larger

systems

2

Page 3: Scala-Ls1

What is Scala? Contd.

• Runs on the java platform(Java Virtual Machine) as well as Dot net platform

• Designed by Martin Odersky in 2001 at EPFL, who was also one of the creators of javaccompiler and generic java

• Open source

• Released in 2004

• Second version v 2.0 in 2006

• Current version 2.11.5

3

Page 4: Scala-Ls1

What makes Scala scalable?

• Combining OOP with functional programming

4

Page 5: Scala-Ls1

Why OOP?

• Purely OO

• Every operation is method

• Every value is an object

• No primitive types, everything is an object

• 1+2 invokes a method + defined in the class

Int

5

Page 6: Scala-Ls1

What is Functional Programming(FP)?

• “In computer science, functional programming is a programming

paradigm, a style of building the structure and elements of computer

programs, that treats computation as the evaluation of mathematical

functions and avoids changing-state and mutable data.”—Wikipedia

• Basic fundamental is to evaluate an expression and use its result for some

other expression

• Made up of function that always return a value

• No side effects state does not change

• Immutable data: cannot change the value once set;no need to lock the

access of shared data

• Reuse same code in different parts of program

• Free order of code execution

6

Page 7: Scala-Ls1

What is FP in Scala like?

• Supports mutable and immutable data

• Consists of variables and values

• Pass functions as arguments

• Return functions as results from functions

• Define functions inside other function

• Store functions in variables

• Functions are fist class values just like integer or string

7

Page 8: Scala-Ls1

Syntax

• No semicolons

• No datatype specification required explicitly,

compiler will determine from context

• Variable definitions start with “var” and

definitions of values (i.e. read only variables)

start with “val”. Val is immutable where var is

mutable

8

Page 9: Scala-Ls1

Comparison with Java

• Compatible with existing java programs and we can use all java libraries

• Operates similar to Java. Its compiler generates byte code that is similar to java bytecode

• Runs on Eclipse,IntelliJ and Netbeans just as Java

• Only two things in Scala that are absent in Java are richer type systems and Functional programming

9

Page 10: Scala-Ls1

Using Scala REPL

• REPL-> Read,Eval,Print,Loop

10

Page 11: Scala-Ls1

Examples for..

• Defining variables

• String, ints

• Non-numeric types

• Tuples

• Expressions and conditions

• Match expression

• Loops

• Defining functions

11

Page 12: Scala-Ls1

Why Scala?

• Since Scala compiles to Java bytecode and runs on the JVM, programs written in Scala can benefit from the huge amount of library code already written in Java. However, by using Scala you get the following benefits:– Mandatory boilerplate code is gone – no getters and setters, no

checked exceptions.

– More powerful constructs, that allow you to do more with less code, such as case classes, option and tuples.

– More powerful code reuse – the elements of code that you can reuse are smaller. Rather than classes with single inheritance, you have traits and functions.

http://hedleyproctor.com/2012/04/why-java-developers-should-be-learning-scala/

12

Page 13: Scala-Ls1

A typical Java class vs. Scala class

example• Java classClass Person {

Private String name;

Private int age;

Person(String name,int age) {

this.name = name;

this.age = age;

}

Public void setAge(int age) {

this.age = age;

}

Public void setName(String name) {

this.name = name;

}

Public int getAge() {

Return this.age;

}

Public String getName() {

return this.name;

}

}

• Scala classPublic class Person {

Var Name = “”

Private Var theAge = 0

Def age = theAge

Def age_= (newAge: Integer): Unit = {if(newAge > 0) theAge = newAge}

}

13

Page 14: Scala-Ls1

Why Scala contd…

• Scala is compatible

• Scala is concise

• Scala is high level

• Scala is statically typed

14

Page 15: Scala-Ls1

Scala features in Java8

• Lambda expressions

• Higher order functions

• Parallel collections

• Function chaining

www.infoq.com/articles/java-8-vs-scala

15

Page 16: Scala-Ls1

Lambda Expressions

• Function literal

• A function with input parameters and function

body

• (type parameter)-> function body

• Eg: (String s1,String s2) -> s1.length() –

s2.length()

16

Page 17: Scala-Ls1

Limitations

• Syntax

Eg: val <identifier>[: <type>] = <data>

Val name :String = “foo”

Difficult to understand as reading backwards. The return value is at the end of the declaration,and the type comes after the name.

• Slow compilation and interpreter

Takes much time to compile and run.We can now use the FSC (Fast Scala compiler),which essentially stays in memory.

• Steep learning curve

17

Page 18: Scala-Ls1

Education

• Programming Scala

• Programming in Scala

• Why Scala?

• Scala Language Specification

18

Page 19: Scala-Ls1

Tools

• IDEs:NetBeans,Eclipse,IDEA

• Editors: Emacs,VIM,TextMate

19

Page 20: Scala-Ls1

Questions?

• Def ? = ???

20

Page 21: Scala-Ls1

Homework

• In the Scala REPL, convert the temperature value of 22.5 Centigrade to Fahrenheit. The conversion formula

is cToF(x) = (x * 9/5) + 32.

• Write a new centigrade-to-fahrenheit conversion (using the formula (x * 9/5) + 32), saving each step of the

conversion into separate values. What do you expect the type of each value will be?

• Using the input string "Frank,123 Main,925-555-1943,95122" and regular expression matching, retrieve the

telephone number. Can you convert each part of the telephone number to its own integer value? How would

you store this in a tuple?

• There is a popular coding interview question I’ll call "typesafe", in which the numbers 1 - 100 must be

printed one per line. The catch is that multiples of 3 must replace the number with the word "type", while

multiples of 5 must replace the number with the word "safe". Of course, multiples of 15 must print

"typesafe".

21


Recommended