Intro to Scala
What is Scala? <ul><li>Hybrid Language, blending OO and FP </li></ul><ul><li>Statically Typed </li></ul><ul><li>Runs on th...
OO Features
Classes <ul><li>No need to add getters/setters for parameters </li></ul>Default Constructor
Objects <ul><li>No Concept of Static in Scala </li></ul><ul><li>Standalone </li></ul><ul><li>Companion </li></ul>
Standalone Object Calling the method
Companion Object <ul><li>Object and Class must have the same name </li></ul><ul><li>Declared in the same file with the cla...
Methods Don’t need a return statement.  The last line of the method is  What gets returned.
Imports & Packages Import Statements Scoped Packages
Type System
Type System <ul><li>Java </li></ul><ul><li>List<String> </li></ul><ul><li>Contravariant </li></ul><ul><ul><li>List<T super...
Type System <ul><li>Covariant: narrower -> wider. </li></ul><ul><ul><li>Double -> float </li></ul></ul><ul><li>Contravaria...
Type System
Type System Call drawAll on Canvas with a list of circles
Type System <ul><li>Looks interesting but what's the catch? </li></ul><ul><ul><li>All examples used immutable types.  When...
Implicits
Implicit Conversion Implicit Conversions provide a nice way to extend libraries and functionality without  changing the or...
Implicit Conversion Must Import the Object in order to use the  new methods It looks like we are calling the repeat method...
Implicit Parameters Implicit parameters are used in a functions parameter list to denote that the parameter Is optional.  ...
Traits
Traits Like an interface with an implementation.  Traits are mixed into classes by using  the keyword  with.  Allowed to h...
Traits Version 2
Traits It is possible to have multiple traits that have the same method.  This is known  as stackable modifications  By ex...
Traits Output: 8 Output: 16
Traits If you remember from the last example the result between the two runs were different.  This is because we changed t...
Functional Features
Immutability <ul><li>First class concept in Scala </li></ul><ul><li>By default all Collection implementations are immutabl...
Concurrency <ul><li>Actor Based.  With each actor having a mailbox. </li></ul><ul><li>Message sends are asynch </li></ul><...
Concurrency Event Based Actors In event based actors a thread pool is used and each actor is given a thread to  execute.
Concurrency Thread Based Actors With thread based actors, every actor is tied to a thread.
Concurrency Starting an actor and sending a message:
Pattern Matching <ul><li>Allow for pattern matching on objects </li></ul><ul><li>“ added value” when you declare a case cl...
Pattern Matching Basic Example: Matching a String or Int Output: “Look a String!” Output: “Look an Int!” Simple instanceof...
Pattern matching Implementing the visitor pattern:
Functions
Functions Partial Function: Passing a function as a parameter: Returning a function from a method:
Other Notable Features
<ul><li>Named & Default Args </li></ul><ul><li>Continuations </li></ul><ul><li>Lazy evaluation (lazy keyword) </li></ul><u...
Upcoming SlideShare
Loading in...5
×

Intro To Scala

866

Published on

High level overview of some Scala concepts.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
866
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Intro To Scala"

  1. 1. Intro to Scala
  2. 2. What is Scala? <ul><li>Hybrid Language, blending OO and FP </li></ul><ul><li>Statically Typed </li></ul><ul><li>Runs on the JVM </li></ul>
  3. 3. OO Features
  4. 4. Classes <ul><li>No need to add getters/setters for parameters </li></ul>Default Constructor
  5. 5. Objects <ul><li>No Concept of Static in Scala </li></ul><ul><li>Standalone </li></ul><ul><li>Companion </li></ul>
  6. 6. Standalone Object Calling the method
  7. 7. Companion Object <ul><li>Object and Class must have the same name </li></ul><ul><li>Declared in the same file with the class def is contained within the object def </li></ul>
  8. 8. Methods Don’t need a return statement. The last line of the method is What gets returned.
  9. 9. Imports & Packages Import Statements Scoped Packages
  10. 10. Type System
  11. 11. Type System <ul><li>Java </li></ul><ul><li>List<String> </li></ul><ul><li>Contravariant </li></ul><ul><ul><li>List<T super Foo> </li></ul></ul><ul><li>Covariant </li></ul><ul><ul><li>List<T extends Foo> </li></ul></ul><ul><li>Supplied when a class is used </li></ul><ul><li>Scala </li></ul><ul><li>List[String] </li></ul><ul><li>Contravariant </li></ul><ul><ul><li>List[-T] </li></ul></ul><ul><li>Covariant </li></ul><ul><ul><li>List[+T] </li></ul></ul><ul><li>Supplied when a class is defined </li></ul>
  12. 12. Type System <ul><li>Covariant: narrower -> wider. </li></ul><ul><ul><li>Double -> float </li></ul></ul><ul><li>Contravariant: wider -> narrower </li></ul><ul><ul><li>Float -> double </li></ul></ul><ul><li>Invariant: Cannot convert </li></ul>
  13. 13. Type System
  14. 14. Type System Call drawAll on Canvas with a list of circles
  15. 15. Type System <ul><li>Looks interesting but what's the catch? </li></ul><ul><ul><li>All examples used immutable types. When working with mutable types in Scala they have to be invariant. </li></ul></ul>Immutable Mutable
  16. 16. Implicits
  17. 17. Implicit Conversion Implicit Conversions provide a nice way to extend libraries and functionality without changing the original code. Example: Add methods to String
  18. 18. Implicit Conversion Must Import the Object in order to use the new methods It looks like we are calling the repeat method on the string class!
  19. 19. Implicit Parameters Implicit parameters are used in a functions parameter list to denote that the parameter Is optional. Declare the parameter to be implicit Label the list as implicit so the compiler knows Which list to use.
  20. 20. Traits
  21. 21. Traits Like an interface with an implementation. Traits are mixed into classes by using the keyword with. Allowed to have abstract as well as concrete implementation of methods. Version 1
  22. 22. Traits Version 2
  23. 23. Traits It is possible to have multiple traits that have the same method. This is known as stackable modifications By extending Arithmetic we are saying that this is only valid for classes that extend Arithmetic Calls the next compute method in line A hint to the compiler that you are “stacking” traits
  24. 24. Traits Output: 8 Output: 16
  25. 25. Traits If you remember from the last example the result between the two runs were different. This is because we changed the order of the traits between the two executions. Ordering of traits makes a difference! By using something called linearization classes and all of the traits are put into a linear order (right to left).  By putting them in this order, each time you call super  the next method up the chain is invoked. 2 + 2 = 4 4 * 4 = 16 2 * 2 = 4 4 + 4 = 8
  26. 26. Functional Features
  27. 27. Immutability <ul><li>First class concept in Scala </li></ul><ul><li>By default all Collection implementations are immutable </li></ul><ul><li>val & var </li></ul><ul><ul><li>var can be reassigned </li></ul></ul><ul><ul><li>val cannot, and must be assigned to something when first declared. </li></ul></ul>
  28. 28. Concurrency <ul><li>Actor Based. With each actor having a mailbox. </li></ul><ul><li>Message sends are asynch </li></ul><ul><li>Implemented on top of Java Threads </li></ul>
  29. 29. Concurrency Event Based Actors In event based actors a thread pool is used and each actor is given a thread to execute.
  30. 30. Concurrency Thread Based Actors With thread based actors, every actor is tied to a thread.
  31. 31. Concurrency Starting an actor and sending a message:
  32. 32. Pattern Matching <ul><li>Allow for pattern matching on objects </li></ul><ul><li>“ added value” when you declare a case class </li></ul><ul><ul><li>Factory method with name of class </li></ul></ul><ul><ul><li>Parameters are maintained as fields </li></ul></ul><ul><li>Patterns are meant to be linear </li></ul>
  33. 33. Pattern Matching Basic Example: Matching a String or Int Output: “Look a String!” Output: “Look an Int!” Simple instanceof check
  34. 34. Pattern matching Implementing the visitor pattern:
  35. 35. Functions
  36. 36. Functions Partial Function: Passing a function as a parameter: Returning a function from a method:
  37. 37. Other Notable Features
  38. 38. <ul><li>Named & Default Args </li></ul><ul><li>Continuations </li></ul><ul><li>Lazy evaluation (lazy keyword) </li></ul><ul><li>REPL </li></ul><ul><li>Scoped imports </li></ul><ul><li>Collections API </li></ul>

×