An Intro to Scala for PHP Developers

12,931 views
12,602 views

Published on

Joseph Price, a software engineer at The Huffington Post gave a talk about the basics of Scala. These slides are a great resource for anyone who's interested in learning more about Scala. PHP knowledge is not required.

Published in: Software
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
12,931
On SlideShare
0
From Embeds
0
Number of Embeds
88
Actions
Shares
0
Downloads
20
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

An Intro to Scala for PHP Developers

  1. 1. SCALA FOR PHP DEVELOPERS JOSEPH PRICE
  2. 2. ● ‘scalable language’ o scales from small to large problems ● object-functional ● runs on JVM ● performant ● concise o type inference o reduced repetition Scala
  3. 3. Object Orientation ● Everything’s an object o 1.toDouble ● Supports typical OO functionality - classes, methods, interfaces, inheritance, polymorphism, encapsulation, access modifiers ● Traits o interface with implementation o allows multiple inheritance o avoids ‘diamond problem’ through a technique called linearization
  4. 4. Functional programming ● ‘first-class’ functions o anonymous o function values o passed to and returned from functions o higher order functions  map, filter, flatMap ● immutable data structures ● isolation of state, side effects
  5. 5. Immutability ● most methods return a new object o immutable data structures ● val / var ● input params cannot change ● iteration - contrast to imperative style o prefer map / fold over stateful looping
  6. 6. Type safety ● more confident in changes o library / api updates o refactoring ● static analysis ● find errors more quickly ● avoid common errors ● compiler can optimize typed code
  7. 7. Type inference Typing required for ● Method signatures ● Class members ● Recursive methods Optional elsewhere Should be annotated on public methods ● Avoid returning too generic/specific type ● Api stability
  8. 8. Concurrency ● immutability enables thread safety o concurrent access to shared mutable state ● Promises / Futures ● actors ● parallel collections
  9. 9. val - immutable and immediately evaluated var - mutable and immediately evaluated def ● calculates value on each access (same as a method) ● uniform access principle lazy val ● calculated on first access ● avoid recalculating Variables vs Values
  10. 10. Classes, Case classes, Objects classes ● body is the constructor ● getters / setters generated ● public by default o private, protected, restricted private[controllers] case classes ● pattern match ● simplify common operations when working with classes as data ● generated apply, toString, equals, copy methods singleton objects ● common pattern that can be implemented in many (mostly incorrect) ways ● companion objects - shares name and file, holds ‘static’ methods and has privileged access
  11. 11. Pattern Matching
  12. 12. Option represents presence/absence of a result algebraic data structure ● can be either Some or None null handling ● empty(), is_null(), isset() ● if x != null ● callers are forced to handle absence
  13. 13. Try ● represents success or failure of an expression ● Success / Failure ● handle result by calling map, handle failure by calling recover ● moves try/catch control structure into type system
  14. 14. Future ● simplifies asynchronous computation ● similar interface to Try ● read-only view of a promise’s result ● Promises produce values asynchronously, future allows binding of callbacks ● similar to jQuery Deferred / Angular promise
  15. 15. Collections immutable / mutable ● immutable preferred Most commonly used are Map, List, Seq
  16. 16. Magic / dragons ● can be abused, affect compile times ● simplify apis by reducing overhead implicit parameters ● avoid passing parameters repetitively extension methods ● via implicit classes / conversions ● ad-hoc polymorphism Implicit scope
  17. 17. DSLs domain specific language internal ● Slick, Scalikejdbc, sbt ● make use of implicits, operator overloading, macros, advanced type system features external ● library support via parser combinators
  18. 18. DSLs - ScalikeJDBC val programmers: List[Long] = DB readOnly { implicit session => withSQL { select .from(Programmer as p) .leftJoin(Company as c).on(p.companyId, c.id) .where.eq(p.isDeleted, false) .orderBy(p.createdAt) .limit(10) .offset(0) }.map(Programmer(p, c)).list.apply()} http://scalikejdbc.org/ Typesafe api that compiles to SQL Makes use of advanced features like Macros and Type Dynamic (like php’s ‘magic methods’ or Ruby’s method_missing)
  19. 19. DSLs - Slick slick.typesafe.com ‘Functional relational mapping’ makes use of familiar collections api can drop down to raw SQL
  20. 20. DSLs - sbt http://www.scala-sbt.org/
  21. 21. Actor Model asynchronous message passing between lightweight processes isolate mutable state lifecycle ● let it crash™ ● fault tolerance ● remoting
  22. 22. Play ● MVC web framework ● Asynchronous ● Non-blocking ● Built on Akka, Netty ● Highly customizable ● Industry support
  23. 23. Environment JVM ● garbage collection, JIT compilation ● reuse existing libraries ● Java interop - call Java code from Scala and vice versa ● tooling, instrumentation SBT IDEs ● IntelliJ, Eclipse o weak support initially, but steadily improving
  24. 24. Resources http://blog.jetbrains.com/scala/2012/12/04/scala-worksheet/ - for IntelliJ https://github.com/scala-ide/scala-worksheet/wiki/Getting-Started - for Eclipse http://www.scala-sbt.org/ - scala’s build tool http://mvnrepository.com/ - find dependencies http://slick.typesafe.com/ - db access using collections-like interface https://github.com/slick/slick-examples/blob/master/src/main/scala/com/typesafe/slick/examples/lifted/FirstExample.scala - example slick code http://scalikejdbc.org/ - sql dsl http://akka.io/ - actors, software transactional memory http://letitcrash.com/ - akka blog https://typesafe.com/activator/templates - many example projects to get you started http://docs.scala-lang.org/overviews/collections/performance-characteristics.html - collections complexity http://docs.scala-lang.org/resources/images/collections.immutable.png - immutable collections class hierarchy http://docs.scala-lang.org/resources/images/collections.mutable.png - mutable collections class hierarchy
  25. 25. Thanks!

×