23
Scala For the uninitiated 1

Beginning scala 02 15

Embed Size (px)

Citation preview

1

ScalaFor the uninitiated

2

why do we need yet another language?

“In theory, there’s no difference between theory and practice, in practice there is.”

Yogi Berra

3introduction

Martin Odersky, a contributor of Javac, frustrated by Java’s constraints set out to improve Java.

4introduction

Martin Odersky, a contributor of Javac, frustrated by Java’s constraints set out to improve Java.

This desire was further hampered by the constraint of needing to maintain backwards compatibility.

5introduction

Martin Odersky, a contributor of Javac, frustrated by Java’s constraints set out to improve Java.

This desire was further hampered by the constraint of needing to maintain backwards compatibility.

In the end, he decided to design a language that would be different from Java, yet one that would utilize the JVM as well as leverage existing libraries.

6scala is born

Scala means Scalable Language.

“A language so named because it was designed to grow with its users.”

Martin Odersky

7scala is born

Scala means Scalable Language.

“A language so named because it was designed to grow with its users.”

Martin Odersky

Scala is a type-safe JVM language that incorporates object-oriented and functional programming.

8scala is compatible

Designed for seamless interoperability with Java.

Scala allows you to add value to existing code.

You can build on what you already have.

9scala is concise

Reductions in line of code up to a factor of ten have been reported.

A conservative estimate would be a typical Scala program should have about half the lines of the same program written in Java.

Programming in Scala 2nd ed.

10scala is concise

“In general, if you can accomplish a task with fewer lines of code, you can complete the task more quickly.

The average number of lines of code a programmer writes per day is constant no matter the language…

Fred Brooks The Mythical Man Month

11scala is concise //this is a typical constructor Javaclass MyClass {

private int index;private String name;public MyClass(int index, String name) {

this.index = index;this.name = name;

}}

// equivalent code in Scalaclass MyClass(index: Int, name: String)

12scala is high level

“Overly complex code ha[s] been the downfall of many a software project.

Unfortunately, important software usually has complex requirements.

Since complexity cannot be avoided, it must instead be managed.”

Programming in Scala 2nd ed.

13scala is high level

//this is a JavaBoolean nameHasUpperCase = false;for (int i = 0; i< name.length(); ++i){

if (Character.isUpperCase(name.charAt (i))) {nameHasUpperCase = true;break;

}}

// equivalent code in Scalaval nameHasUpperCase = name.exists(_.isUpper)

14scala is statically typed

A static typed system classifies variables and expressions according to the kind of values they hold and compute.

15scala has advanced static typing

A static typed system classifies variables and expressions according to the kind of values they hold and compute.

Scala can infer most types.

Scala allows you to parameterize types with generics.

Hide details of types using abstract types.

16

All but the most trivial programs need some structure.

The most straightforward way to do this is to put data and operations into objects.

Few languages follow this principal to its logical conclusion.

scala is object oriented

17scala is pure form object oriented

EVERY value is an object.

EVERY operation is a method call.

1+2 in Scala invokes a method named ‘+’ defined in class Int.

18scala is functional

In addition to being pure form Object Oriented, Scala is a functional language.

Functions are values.

Data is immutable.

Optional Referential Transparency*

19functions are values

A function is a value of the same status as an integer or string.

You can pass functions as arguments to other functions, return them as results of functions or store them in variables.

20immutable data

Program operations should map input values to output values rather than change data in place.

Scala has immutable lists, tuples, maps and sets.

scala> List(1,2,3) res13: List[Int] = List(1, 2, 3)

scala> res13.map(_ + 1) res14: List[Int] = List(2, 3, 4)

21

import java.util.*;public class javaKey{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); Map <Integer, String> hm = new HashMap<Integer, String>(); hm.put(0,"C"); hm.put(1,"G"); hm.put(2,"D"); hm.put(3,"A"); hm.put(4,"E"); hm.put(5,"B"); hm.put(6,"F#"); hm.put(7,"C#"); System.out.println("Please enter the number of sharps in the song: "); int input = sc.nextInt(); System.out.println("You entered: " + input ); System.out.println("The Key Signature is " + hm.get(input)); }} def f(){

println("Enter the number of sharps in the song: " )val input = readLine() println (input)val numSharps = Map ( 0-> "C", 1-> "G", 2-> "D", 3-> "A", 4-> "E", 5-> "B", 6-> "F#", 7-> "C#")println("The Key Signature is " + numSharps(input.toInt))}

22observation

Coming from Java, the most challenging aspect of learning Scala so far has been Scala’s support for functional language.

23Thank You!

Questions?