Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Scala-Ls1

214 views

Published on

  • Be the first to comment

  • Be the first to like this

Scala-Ls1

  1. 1. Learning Scala Aniket Joshi 1
  2. 2. 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
  3. 3. 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 javac compiler and generic java • Open source • Released in 2004 • Second version v 2.0 in 2006 • Current version 2.11.5 3
  4. 4. What makes Scala scalable? • Combining OOP with functional programming 4
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. Using Scala REPL • REPL-> Read,Eval,Print,Loop 10
  11. 11. Examples for.. • Defining variables • String, ints • Non-numeric types • Tuples • Expressions and conditions • Match expression • Loops • Defining functions 11
  12. 12. 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
  13. 13. A typical Java class vs. Scala class example • Java class Class 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 class Public class Person { Var Name = “” Private Var theAge = 0 Def age = theAge Def age_= (newAge: Integer): Unit = {if(newAge > 0) theAge = newAge} } 13
  14. 14. Why Scala contd… • Scala is compatible • Scala is concise • Scala is high level • Scala is statically typed 14
  15. 15. Scala features in Java8 • Lambda expressions • Higher order functions • Parallel collections • Function chaining www.infoq.com/articles/java-8-vs-scala 15
  16. 16. 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
  17. 17. 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
  18. 18. Education • Programming Scala • Programming in Scala • Why Scala? • Scala Language Specification 18
  19. 19. Tools • IDEs:NetBeans,Eclipse,IDEA • Editors: Emacs,VIM,TextMate 19
  20. 20. Questions? • Def ? = ??? 20
  21. 21. 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

×