SlideShare a Scribd company logo
Introduction to Scala

       Suraj Atreya
 surajatreyac@gmail.com
       @suraj2585
First bits
• Scalable language pronounced
  as “Sca-lah”
• Developed by Martin Odersky in
  2001
• He is also famous for developing
  javac
• Currently owns his own
  company called Typesafe
Motivation

                       Java Code                                  Scala Code

public class DemoAdd {                     object DemoAdd {

public static void main(String[] args) {       def main(args: Array[String]): Unit = {

        int sum = 0;                               println((1 to 1000).reduceLeft( _ + _ ))

        for(int i = 1; i <= 1000; ++i){        }
             sum = sum + i;                }
        }
        System.out.println(sum);
    }
}
Motivation
                        Java Code                                         Scala Code

public class DemoList {                          object DemoList {
public static void main(String[] args) {
                                                     def main(args:Array[String]):Unit = {
         List<String> ls = new                        val ls = List("One", "Two", "Three", "Four")
               ArrayList<String>();
                                                         for(xs <- ls) println(xs)
         ls.add(“One");                              }
         ls.add(“Two");                          }
         ls.add(“Three");

         Iterator it = ls.iterator();

         while(it.hasNext()){
               String value=(String)it.next();
               System.out.println(value);
        }
    }
}
Motivation
•   Code size is much smaller compared to Java
•   Less verbose
•   Functional Programming(FP) + Object oriented
•   Statically typed
•   JVM based
•   .NET (CLR) support
•   Scalable – from small to large distributed
    systems
Motivation
• Seamless integration with Java
• Hot language 
• Facebook, Twitter, LinkedIn, Foursquare
IDE
• http://www.scala-lang.org/
• Eclipse plugin (http://www.eclipse.org)
• IntelliJ (http://www.jetbrains.com/idea/)
Classes and Objects
         class DemoClass {

             var name:String = ""

         }

         object Demo{

             def main (args:Array[String]):Unit = {

                 val hello = new DemoClass
                 hello name = "Hello"
                 println(hello name)
             }
         }
Classes and Objects
public class Person{                              class Person(age: Int, name: String) {
                                                    override def toString = age + " " + name
        private int m_age = 0;                    }
        private String m_name = "";
                                                  object DemoConstr {
    Person(int age, String name){
       m_age = age;                                   def main(args: Array[String]): Unit = {
      m_name = name;                                    val person = new Person(10, "John")
    }                                                   println(person)
                                                      }
    public static void main(String[] args) {      }

        Person person = new Person(10, "John");
        System.out.println(person.m_age + " " +
              person.m_name);
    }
}
Functional Programming (FP)
• Learning FP is challenging
• Functions are first-class citizens
• In FP, constraint is “no state” and “immutable”
  – Other words FP does not allow “side effects”
  – Reassigning a variable
  – Modifying DS in place
• FP allows programming using “pure functions”
FP Pure Functions
val x = "Hello, World"
         x: java.lang.String = Hello, World
val r1 = x.reverse
         r1: String = dlroW ,olleH
val r2 = x.reverse
         r2: String = dlroW ,olleH            x is referentially transparent

                                              After substitution, the output
Substitute “x” with its value                 remains same – “no side
                                              effects”
scala> val r1 = "Hello, World".reverse
        r1: String = dlroW ,olleH
val r2 = "Hello, World".reverse
        r2: String = dlroW ,olleH
FP Pure Functions
val x = new StringBuilder("Hello")
         x: java.lang.StringBuilder = Hello
val y = x.append(", World")
         y: java.lang.StringBuilder = Hello, World
val r1 = y.toString
         r1: java.lang.String = Hello, World
val r2 = y.toString
         r2: java.lang.String = Hello, World
FP Pure Functions
val x = new StringBuilder("Hello")
        x: java.lang.StringBuilder = Hello
val r1 = x.append(", World").toString
        r1: java.lang.String = Hello, World
val r2 = x.append(", World").toString
        r2: java.lang.String = Hello, World, World

                                              x is not referentially
                                              transparent

                                              After substitution, the output
                                              Is different– “side effects”
FP
object NumOfElems{

    def length[A](ls: List[A]): Int = ls match {
      case Nil => 0
      case _ :: tail => 1 + length(tail)
      case _ => throw new NoSuchElementException
    }

    def main(args: Array[String]): Unit = {
      println(List(1, 2, 4, 5, 98, 23, 53).length)
      println(length(List(1, 2, 4, 5, 98, 23, 53)))
    }

}
FP   1 + length(List(2, 4, 5, 98, 23, 53))

     1 + (1 + length(List(4, 5, 98, 23, 53)))

     1 + (1 + (1 + length(List(5, 98, 23, 53))))

     1 + (1 + (1 + (1 + length(List(98, 23, 53)))))

     1 + (1 + (1 + (1 + (1 + length(List(23, 53))))))

     1 + (1 + (1 + (1 + (1 + (1 + length(List(53)))))))

     1 + (1 + (1 + (1 + (1 + (1 + (1 + length(List())))))))

     1 + (1 + (1 + (1 + (1 + (1 + (1 + 0)))))))

     1 + (1 + (1 + (1 + (1 + (1 + 1)))))

     1 + (1 + (1 + (1 + (1 + 2))))

     1 + (1 + (1 + (1 + 3)))

     1 + (1 + (1 + 4))

     1 + (1 + 5)

     1+6

     7
FP
• This works for small lists, but what if you have a very large list ?!

• Don’t give up on “Recursion”, we have a solution for that

• Remember, “Recursion is the bread and butter of FP”
FP - Tail call optimisation
def length[A](ls: List[A]): Int = ls match {    def lengthTR[A](ls: List[A]):Int = {
   case Nil => 0
   case _ :: tail => 1 + length(tail)               def loop(ls:List[A], acc:Int):Int = ls match{
   case _ => throw new NoSuchElementException         case Nil => acc
 }                                                    case _ :: tail => loop(tail, acc + 1)
                                                    }

                                                    loop(ls,0)
                                                }
FP - Tail call optimisation
def lengthTR[A](ls: List[A]):Int = {                 loop(List(2, 4, 5, 98, 23, 53), 0 + 1)

                                                     loop(List(4, 5, 98, 23, 53), 1 + 1)
     def loop(ls:List[A], acc:Int):Int = ls match{
       case Nil => acc                               loop(List(5, 98, 23, 53), 2 + 1)
       case _ :: tail => loop(tail, acc + 1)
     }                                               loop(List(98, 23, 53), 3 + 1)

     loop(ls,0)                                      loop(List(23, 53), 4 + 1)
 }
                                                     loop(List(53), 5 + 1)

                                                     loop(List(), 6 + 1)

                                                     7
Pattern Matching
      object DemoRegEx {

          def main(args:Array[String]):Unit = {

              val Date = """(dd)/(dd)/(dddd)""".r

              val date = "01/02/2013"

              val Date(day, month, year) = date

              println(day)
              println(month)
              println(year)
          }

      }
Final thoughts
•   Scala is great for general purpose computing
•   Scales well and has an elegant syntax
•   Neat integration with Java
•   Great RegEx support
•   Actors replaces Threads in Java for
    concurrency
E0F
Ad

More Related Content

What's hot (19)

Scala for Java programmers
Scala for Java programmersScala for Java programmers
Scala for Java programmers
輝 子安
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
thnetos
 
JS OO and Closures
JS OO and ClosuresJS OO and Closures
JS OO and Closures
Jussi Pohjolainen
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
Jesper Kamstrup Linnet
 
Spark Schema For Free with David Szakallas
 Spark Schema For Free with David Szakallas Spark Schema For Free with David Szakallas
Spark Schema For Free with David Szakallas
Databricks
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
Baishampayan Ghose
 
concurrency with GPars
concurrency with GParsconcurrency with GPars
concurrency with GPars
Paul King
 
Making an Object System with Tcl 8.5
Making an Object System with Tcl 8.5Making an Object System with Tcl 8.5
Making an Object System with Tcl 8.5
Donal Fellows
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
Jesper Kamstrup Linnet
 
Optimizing Tcl Bytecode
Optimizing Tcl BytecodeOptimizing Tcl Bytecode
Optimizing Tcl Bytecode
Donal Fellows
 
GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)
Gagan Agrawal
 
Hw09 Hadoop + Clojure
Hw09   Hadoop + ClojureHw09   Hadoop + Clojure
Hw09 Hadoop + Clojure
Cloudera, Inc.
 
Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010
Derek Chen-Becker
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
elliando dias
 
Clojure, Plain and Simple
Clojure, Plain and SimpleClojure, Plain and Simple
Clojure, Plain and Simple
Ben Mabey
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
concurrency gpars
concurrency gparsconcurrency gpars
concurrency gpars
Paul King
 
Hello, Guava !
Hello, Guava !Hello, Guava !
Hello, Guava !
輝 子安
 
A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to Scala
Tim Underwood
 
Scala for Java programmers
Scala for Java programmersScala for Java programmers
Scala for Java programmers
輝 子安
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
thnetos
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
Jesper Kamstrup Linnet
 
Spark Schema For Free with David Szakallas
 Spark Schema For Free with David Szakallas Spark Schema For Free with David Szakallas
Spark Schema For Free with David Szakallas
Databricks
 
concurrency with GPars
concurrency with GParsconcurrency with GPars
concurrency with GPars
Paul King
 
Making an Object System with Tcl 8.5
Making an Object System with Tcl 8.5Making an Object System with Tcl 8.5
Making an Object System with Tcl 8.5
Donal Fellows
 
Optimizing Tcl Bytecode
Optimizing Tcl BytecodeOptimizing Tcl Bytecode
Optimizing Tcl Bytecode
Donal Fellows
 
GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)
Gagan Agrawal
 
Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010
Derek Chen-Becker
 
Clojure, Plain and Simple
Clojure, Plain and SimpleClojure, Plain and Simple
Clojure, Plain and Simple
Ben Mabey
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
concurrency gpars
concurrency gparsconcurrency gpars
concurrency gpars
Paul King
 
Hello, Guava !
Hello, Guava !Hello, Guava !
Hello, Guava !
輝 子安
 
A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to Scala
Tim Underwood
 

Similar to Scala (20)

(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
2.1 recap from-day_one
2.1 recap from-day_one2.1 recap from-day_one
2.1 recap from-day_one
futurespective
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
Vladimir Parfinenko
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
Werner Hofstra
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
BTI360
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
Mohsen Zainalpour
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
Meetu Maltiar
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
Knoldus Inc.
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
wpgreenway
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
Ashal aka JOKER
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
Christian Baranowski
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Matthew Farwell
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Aleksandar Prokopec
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
futurespective
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
Stuart Roebuck
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kirill Rozov
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
2.1 recap from-day_one
2.1 recap from-day_one2.1 recap from-day_one
2.1 recap from-day_one
futurespective
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
Werner Hofstra
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
BTI360
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
wpgreenway
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
Ashal aka JOKER
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Matthew Farwell
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
Stuart Roebuck
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kirill Rozov
 
Ad

Recently uploaded (20)

#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Ad

Scala

  • 1. Introduction to Scala Suraj Atreya [email protected] @suraj2585
  • 2. First bits • Scalable language pronounced as “Sca-lah” • Developed by Martin Odersky in 2001 • He is also famous for developing javac • Currently owns his own company called Typesafe
  • 3. Motivation Java Code Scala Code public class DemoAdd { object DemoAdd { public static void main(String[] args) { def main(args: Array[String]): Unit = { int sum = 0; println((1 to 1000).reduceLeft( _ + _ )) for(int i = 1; i <= 1000; ++i){ } sum = sum + i; } } System.out.println(sum); } }
  • 4. Motivation Java Code Scala Code public class DemoList { object DemoList { public static void main(String[] args) { def main(args:Array[String]):Unit = { List<String> ls = new val ls = List("One", "Two", "Three", "Four") ArrayList<String>(); for(xs <- ls) println(xs) ls.add(“One"); } ls.add(“Two"); } ls.add(“Three"); Iterator it = ls.iterator(); while(it.hasNext()){ String value=(String)it.next(); System.out.println(value); } } }
  • 5. Motivation • Code size is much smaller compared to Java • Less verbose • Functional Programming(FP) + Object oriented • Statically typed • JVM based • .NET (CLR) support • Scalable – from small to large distributed systems
  • 6. Motivation • Seamless integration with Java • Hot language  • Facebook, Twitter, LinkedIn, Foursquare
  • 7. IDE • http://www.scala-lang.org/ • Eclipse plugin (http://www.eclipse.org) • IntelliJ (http://www.jetbrains.com/idea/)
  • 8. Classes and Objects class DemoClass { var name:String = "" } object Demo{ def main (args:Array[String]):Unit = { val hello = new DemoClass hello name = "Hello" println(hello name) } }
  • 9. Classes and Objects public class Person{ class Person(age: Int, name: String) { override def toString = age + " " + name private int m_age = 0; } private String m_name = ""; object DemoConstr { Person(int age, String name){ m_age = age; def main(args: Array[String]): Unit = { m_name = name; val person = new Person(10, "John") } println(person) } public static void main(String[] args) { } Person person = new Person(10, "John"); System.out.println(person.m_age + " " + person.m_name); } }
  • 10. Functional Programming (FP) • Learning FP is challenging • Functions are first-class citizens • In FP, constraint is “no state” and “immutable” – Other words FP does not allow “side effects” – Reassigning a variable – Modifying DS in place • FP allows programming using “pure functions”
  • 11. FP Pure Functions val x = "Hello, World" x: java.lang.String = Hello, World val r1 = x.reverse r1: String = dlroW ,olleH val r2 = x.reverse r2: String = dlroW ,olleH x is referentially transparent After substitution, the output Substitute “x” with its value remains same – “no side effects” scala> val r1 = "Hello, World".reverse r1: String = dlroW ,olleH val r2 = "Hello, World".reverse r2: String = dlroW ,olleH
  • 12. FP Pure Functions val x = new StringBuilder("Hello") x: java.lang.StringBuilder = Hello val y = x.append(", World") y: java.lang.StringBuilder = Hello, World val r1 = y.toString r1: java.lang.String = Hello, World val r2 = y.toString r2: java.lang.String = Hello, World
  • 13. FP Pure Functions val x = new StringBuilder("Hello") x: java.lang.StringBuilder = Hello val r1 = x.append(", World").toString r1: java.lang.String = Hello, World val r2 = x.append(", World").toString r2: java.lang.String = Hello, World, World x is not referentially transparent After substitution, the output Is different– “side effects”
  • 14. FP object NumOfElems{ def length[A](ls: List[A]): Int = ls match { case Nil => 0 case _ :: tail => 1 + length(tail) case _ => throw new NoSuchElementException } def main(args: Array[String]): Unit = { println(List(1, 2, 4, 5, 98, 23, 53).length) println(length(List(1, 2, 4, 5, 98, 23, 53))) } }
  • 15. FP 1 + length(List(2, 4, 5, 98, 23, 53)) 1 + (1 + length(List(4, 5, 98, 23, 53))) 1 + (1 + (1 + length(List(5, 98, 23, 53)))) 1 + (1 + (1 + (1 + length(List(98, 23, 53))))) 1 + (1 + (1 + (1 + (1 + length(List(23, 53)))))) 1 + (1 + (1 + (1 + (1 + (1 + length(List(53))))))) 1 + (1 + (1 + (1 + (1 + (1 + (1 + length(List()))))))) 1 + (1 + (1 + (1 + (1 + (1 + (1 + 0))))))) 1 + (1 + (1 + (1 + (1 + (1 + 1))))) 1 + (1 + (1 + (1 + (1 + 2)))) 1 + (1 + (1 + (1 + 3))) 1 + (1 + (1 + 4)) 1 + (1 + 5) 1+6 7
  • 16. FP • This works for small lists, but what if you have a very large list ?! • Don’t give up on “Recursion”, we have a solution for that • Remember, “Recursion is the bread and butter of FP”
  • 17. FP - Tail call optimisation def length[A](ls: List[A]): Int = ls match { def lengthTR[A](ls: List[A]):Int = { case Nil => 0 case _ :: tail => 1 + length(tail) def loop(ls:List[A], acc:Int):Int = ls match{ case _ => throw new NoSuchElementException case Nil => acc } case _ :: tail => loop(tail, acc + 1) } loop(ls,0) }
  • 18. FP - Tail call optimisation def lengthTR[A](ls: List[A]):Int = { loop(List(2, 4, 5, 98, 23, 53), 0 + 1) loop(List(4, 5, 98, 23, 53), 1 + 1) def loop(ls:List[A], acc:Int):Int = ls match{ case Nil => acc loop(List(5, 98, 23, 53), 2 + 1) case _ :: tail => loop(tail, acc + 1) } loop(List(98, 23, 53), 3 + 1) loop(ls,0) loop(List(23, 53), 4 + 1) } loop(List(53), 5 + 1) loop(List(), 6 + 1) 7
  • 19. Pattern Matching object DemoRegEx { def main(args:Array[String]):Unit = { val Date = """(dd)/(dd)/(dddd)""".r val date = "01/02/2013" val Date(day, month, year) = date println(day) println(month) println(year) } }
  • 20. Final thoughts • Scala is great for general purpose computing • Scales well and has an elegant syntax • Neat integration with Java • Great RegEx support • Actors replaces Threads in Java for concurrency
  • 21. E0F