A Brief Intro to Scala

A Brief Intro to Scala,[object Object],Tim Underwood,[object Object]
About Me,[object Object],Tim Underwood,[object Object],Co-Founder of Frugal Mechanic,[object Object],Software Developer,[object Object],Perl, PHP, C, C++, C#, Java, Ruby and now Scala,[object Object],Before Scaladefault languages were Ruby and Java,[object Object]
Dynamic vs. Static,[object Object],Dynamic (Ruby),[object Object],Static (Java),[object Object],Concise,[object Object],Scriptable,[object Object],Read-Eval-Print Loop (irb),[object Object],Higher Order Functions,[object Object],Extend existing classes,[object Object],Duck Typing,[object Object],method_missing,[object Object],Better IDE Support,[object Object],Fewer Tests,[object Object],Documentation,[object Object],Open Source Libs,[object Object],Performance,[object Object],JVM Tools (VisualVM),[object Object],True Multi-threading,[object Object]
Scala,[object Object],[object Object]
Scriptable
Read-Eval-Print Loop
Higher Order Functions
Extend existing classes
Duck Typing
method_missing
Better IDE Support
Fewer Tests
Documentation
Open Source Libs
Performance
JVM Tools (VisualVM)
True Multi-threading,[object Object]
Scalais a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way.,[object Object]
Scala,[object Object],Statically Typed,[object Object],Runs on JVM, full inter-op with Java,[object Object],Object Oriented,[object Object],Functional,[object Object],Dynamic Features,[object Object]
Scala is Practical,[object Object],Can be used as drop-in replacement for Java,[object Object],Mixed Scala/Java projects,[object Object],Use existing Java libraries,[object Object],Use existing Java tools (Ant, Maven, JUnit, etc…),[object Object],Decent IDE Support (NetBeans, IntelliJ, Eclipse),[object Object]
Scala is Concise,[object Object]
Type Inference,[object Object],val sum = 1 + 2 + 3,[object Object],valnums = List(1, 2, 3),[object Object],val map = Map("abc" -> List(1,2,3)),[object Object]
Explicit Types,[object Object],val sum:Int= 1 + 2 + 3,[object Object],valnums:List[Int]= List(1, 2, 3),[object Object],val map:Map[String, List[Int]] = ...,[object Object]
Higher Level,[object Object],// Java – Check if string has uppercase character,[object Object],booleanhasUpperCase = false;,[object Object],for(inti = 0; i < name.length(); i++) {,[object Object],if(Character.isUpperCase(name.charAt(i))) {,[object Object],hasUpperCase = true;,[object Object],break;,[object Object],    },[object Object],},[object Object]
Higher Level,[object Object],// Scala,[object Object],valhasUpperCase = name.exists(_.isUpperCase),[object Object]
Less Boilerplate,[object Object],// Java,[object Object],public class Person {,[object Object],private String name;,[object Object],private intage;,[object Object],public Person(String name, Intage) {  // constructor,[object Object],    this.name = name;,[object Object],this.age = age;,[object Object],  },[object Object],public String getName() {              // name getter,[object Object],    return name;,[object Object],  },[object Object],publicintgetAge() {                  // age getter,[object Object],    return age;,[object Object],  },[object Object],public void setName(Stringname) {     // name setter,[object Object],    this.name = name;,[object Object],  },[object Object],public void setAge(intage) {          // age setter,[object Object],this.age = age;,[object Object],  },[object Object],},[object Object]
Less Boilerplate,[object Object],// Scala,[object Object],class Person(varname: String, varage: Int),[object Object]
Less Boilerplate,[object Object],// Scala,[object Object],class Person(var name: String, privatevar _age: Int) {,[object Object],  defage = _age// Getter for age,[object Object],  defage_=(newAge:Int) {  // Setter for age,[object Object],println("Changing age to: "+newAge),[object Object],_age = newAge,[object Object],  },[object Object],},[object Object]
Variables and Values,[object Object],// variable,[object Object],varfoo = "foo",[object Object],foo = "bar"  // okay,[object Object],// value,[object Object],valbar = "bar",[object Object],bar = "foo"// nope,[object Object]
Scala is Object Oriented,[object Object]
Pure O.O.,[object Object],// Every value is an object,[object Object],1.toString,[object Object],// Every operation is a method call,[object Object],1 + 2 + 3    (1).+(2).+(3),[object Object],// Can omit . and ( ),[object Object],"abc" charAt 1    "abc".charAt(1),[object Object]
Classes,[object Object],// Classes (and abstract classes) like Java,[object Object],abstract classLanguage(valname:String) {,[object Object],override deftoString = name,[object Object],},[object Object],// Example implementations,[object Object],classScalaextendsLanguage("Scala"),[object Object],// Anonymous class,[object Object],valscala = newLanguage("Scala") { /* empty */ },[object Object]
Traits,[object Object],// Like interfaces in Java,[object Object],trait Language {,[object Object],valname:String,[object Object],// But allow implementation,[object Object],overridedeftoString = name,[object Object],},[object Object]
Traits,[object Object],traitJVM { ,[object Object],  override deftoString = super.toString+" runs on JVM" },[object Object],traitStatic {,[object Object],  override deftoString = super.toString+" is Static" },[object Object],// Traits are stackable,[object Object],classScalaextends Language with JVM with Static {,[object Object],val name = "Scala",[object Object],},[object Object],println(newScala) "Scala runs on JVM is Static",[object Object]
Singleton Objects,[object Object],// Replaces static methods from Java,[object Object],// Can extend/implement classes & traits,[object Object],object Hello {,[object Object],  def world = println("Hello World"},[object Object],},[object Object],Hello.world  Hello World,[object Object]
Scala is Functional,[object Object]
First Class Functions,[object Object],// Lightweight anonymous functions,[object Object],(x:Int) => x + 1,[object Object],// Calling the anonymous function,[object Object],valplusOne = (x:Int) => x + 1,[object Object],plusOne(5)    6,[object Object]
Closures,[object Object],// plusFoo can reference any values/variables in scope,[object Object],varfoo= 1,[object Object],valplusFoo = (x:Int) => x + foo,[object Object],plusFoo(5)      6,[object Object],// Changing foo changes the return value of plusFoo,[object Object],foo= 5,[object Object],plusFoo(5)      10,[object Object]
Higher Order Functions,[object Object],valplusOne = (x:Int) => x + 1,[object Object],valnums = List(1,2,3),[object Object],// map takes a function: Int => T,[object Object],nums.map(plusOne)       List(2,3,4),[object Object],// Inline Anonymous,[object Object],nums.map(x => x + 1)    List(2,3,4),[object Object],// Short form,[object Object],nums.map(_ + 1)         List(2,3,4),[object Object]
Higher Order Functions,[object Object],valnums = List(1,2,3,4),[object Object],// A few more examples for List class,[object Object],nums.exists(_ == 2)          true,[object Object],nums.find(_ == 2)            Some(2),[object Object],nums.indexWhere(_ == 2)      1,[object Object],nums.reduceLeft(_ + _)       10,[object Object],nums.foldLeft(100)(_ + _)    110,[object Object],// Many more in collections library,[object Object]
Higher Order Functions,[object Object],// functions as parameters,[object Object],defcall(f: Int => Int) = f(1),[object Object],call(plusOne)       2,[object Object],call(x => x + 1)    2,[object Object],call(_ + 1)         2,[object Object]
Higher Order Functions,[object Object],// functions as parameters,[object Object],defeach(xs: List[Int], fun: Int => Unit) {,[object Object],if(!xs.isEmpty) {,[object Object],fun(xs.head),[object Object],each(xs.tail, fun),[object Object],  },[object Object],},[object Object],each(List(1,2,3), println),[object Object],  1,[object Object],  2,[object Object],  3,[object Object]
Higher Order Functions,[object Object],// More complex example with generics & patternmatching,[object Object],@tailrec,[object Object],defeach[T](xs: List[T], fun: T => Unit): Unit = xsmatch{,[object Object],caseNil =>,[object Object],casehead :: tail => fun(head); each(tail, fun),[object Object],},[object Object],each(List(1,2), println),[object Object],  1,[object Object],  2,[object Object],each(List("foo", "bar"), println),[object Object],foo,[object Object],  bar,[object Object]
Pattern Matching,[object Object],def what(any:Any) = any match {,[object Object],casei:Int => "It's an Int",[object Object],cases:String => "It's a String",[object Object],case_ => "I don't know what it is",[object Object],},[object Object],what(123)       "It's an Int",[object Object],what("hello")   "It's a String",[object Object],what(false)     "I don't know what it is",[object Object]
Pattern Matching,[object Object],valnums = List(1,2,3),[object Object],// Pattern matching to create 3 vals,[object Object],valList(a,b,c) = nums,[object Object],a      1,[object Object],b  2,[object Object],c  3,[object Object]
Immutable Types,[object Object],// Immutable types by default,[object Object],varnums = Set(1,2,3),[object Object],nums+= 4  nums= nums.+(4),[object Object],// Mutable types available,[object Object],importscala.collection.mutable._,[object Object],valnums = Set(1,2,3),[object Object],nums+= 4  nums.+=(4),[object Object]
scala.collection,[object Object]
scala.collection.immutable,[object Object]
scala.collection.mutable,[object Object]
Or Use Existing Java Collections,[object Object],java.util,[object Object],Apache Commons Collections,[object Object],fastutil,[object Object],Trove,[object Object],Google Collections,[object Object],scala.collection.JavaConversion available to convert to and from java.util Interfaces,[object Object]
Scalais Dynamic,[object Object],(Okay not really, but it has lots of features typically only found in Dynamic languages),[object Object]
Scriptable,[object Object],// HelloWorld.scala,[object Object],println("Hello World"),[object Object],bash$scalaHelloWorld.scala,[object Object],Hello World,[object Object],bash$scala -e 'println("Hello World")',[object Object],Hello World,[object Object]
Read-Eval-Print Loop,[object Object],bash$scala,[object Object],Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22).,[object Object],Type in expressions to have them evaluated.,[object Object],Type :help for more information.,[object Object],scala>class Foo { def bar = "baz" },[object Object],defined class Foo,[object Object],scala>valf = new Foo,[object Object],f: Foo = Foo@51707653,[object Object],scala> f.bar,[object Object],res2: java.lang.String = baz,[object Object]
Structural Typing,[object Object],// Type safe Duck Typing,[object Object],def doTalk(any:{deftalk:String}) {,[object Object],println(any.talk),[object Object],},[object Object],class Duck { def talk = "Quack" },[object Object],class Dog  { def talk = "Bark"  },[object Object],doTalk(new Duck)    "Quack",[object Object],doTalk(new Dog)     "Bark",[object Object]
Implicit Conversions,[object Object],// Extend existing classes in a type safe way,[object Object],// Goal: Add isBlank method to String class,[object Object],classRichString(s:String) {   ,[object Object],defisBlank = null == s || "" == s.trim,[object Object],},[object Object],implicit deftoRichString(s:String) = newRichString(s),[object Object],// Our isBlank method is now available on Strings,[object Object]," ".isBlanktrue,[object Object],"foo".isBlankfalse,[object Object]
Implicit Conversions,[object Object],// Does not type check,[object Object],"abc".isBlank,[object Object],// Search in-scope implicitsdefs that take a,[object Object],// String & return a type with an isBlank method,[object Object],implicit deftoRichString(s:String):RichString,[object Object],// Resulting code that type checks,[object Object],newRichString("abc").isBlank,[object Object]
1 of 56

More Related Content

What's hot(20)

Javascript arraysJavascript arrays
Javascript arrays
Hassan Dar6.5K views
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
datamantra1.7K views
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
LivePerson6.9K views
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
Ravi Kant Sahu31.1K views
OOP and FPOOP and FP
OOP and FP
Mario Fusco4K views
Datatype in JavaScriptDatatype in JavaScript
Datatype in JavaScript
Rajat Saxena1.2K views
Arrays in JavaArrays in Java
Arrays in Java
Abhilash Nair24K views
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA8.4K views
The Evolution of JavaThe Evolution of Java
The Evolution of Java
Fu Cheng12.4K views
Core JavaCore Java
Core Java
Prakash Dimmita2.6K views
collection framework in javacollection framework in java
collection framework in java
MANOJ KUMAR422 views
Java Collection frameworkJava Collection framework
Java Collection framework
ankitgarg_er14.3K views
Java IntroductionJava Introduction
Java Introduction
sunmitraeducation4K views
Fundamentals of JAVAFundamentals of JAVA
Fundamentals of JAVA
KUNAL GADHIA1.9K views
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha373 views

Viewers also liked(10)

Scala, just a better java?Scala, just a better java?
Scala, just a better java?
Giampaolo Trapasso981 views
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
Martin Odersky37.2K views
Apache spark IntroApache spark Intro
Apache spark Intro
Tudor Lapusan1.1K views
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Rahul Jain4.6K views
Scala and sparkScala and spark
Scala and spark
Fabio Fumarola4.7K views
Spark workshopSpark workshop
Spark workshop
Wojciech Pituła1.1K views

Similar to A Brief Intro to Scala(20)

Intro to scalaIntro to scala
Intro to scala
Joe Zulli330 views
Scala in Places APIScala in Places API
Scala in Places API
Łukasz Bałamut629 views
Scala for curiousScala for curious
Scala for curious
Tim (dev-tim) Zadorozhniy514 views
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
Venkateswaran Kandasamy221 views
Scala introductionScala introduction
Scala introduction
Yardena Meymann999 views
Scala ntnuScala ntnu
Scala ntnu
Alf Kristian Støyle493 views
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009
David Pollak855 views
ScalaScala
Scala
Sven Efftinge1.8K views
ES6 and BEYONDES6 and BEYOND
ES6 and BEYOND
Brian Patterson224 views
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
Isaias Barroso1.8K views
Workshop ScalaWorkshop Scala
Workshop Scala
Bert Van Vreckem1.4K views
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Lorenzo Dematté259 views
The Great Scala MakeoverThe Great Scala Makeover
The Great Scala Makeover
Garth Gilmour177 views
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Raúl Raja Martínez1.3K views
Scala introductionScala introduction
Scala introduction
Alf Kristian Støyle2K views

Recently uploaded(20)

Java Platform Approach 1.0 - Picnic MeetupJava Platform Approach 1.0 - Picnic Meetup
Java Platform Approach 1.0 - Picnic Meetup
Rick Ossendrijver24 views
METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...
METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...
Prity Khastgir IPR Strategic India Patent Attorney Amplify Innovation24 views
Green Leaf Consulting: Capabilities DeckGreen Leaf Consulting: Capabilities Deck
Green Leaf Consulting: Capabilities Deck
GreenLeafConsulting177 views
ChatGPT and AI for Web DevelopersChatGPT and AI for Web Developers
ChatGPT and AI for Web Developers
Maximiliano Firtman161 views
CXL at OCPCXL at OCP
CXL at OCP
CXL Forum203 views

A Brief Intro to Scala