Get Programming with Scala
eBook - ePub

Get Programming with Scala

  1. 560 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Get Programming with Scala

Book details
Book preview
Table of contents
Citations

About This Book

The perfect starting point for your journey into Scala and functional programming. Summary
In Get Programming in Scala you will learn: Object-oriented principles in Scala
Express program designs in functions
Use types to enforce program requirements
Use abstractions to avoid code duplication
Write meaningful tests and recognize code smellsScala is a multi-style programming language for the JVM that supports both object-oriented and functional programming. Master Scala, and you'll be well-equipped to match your programming approach to the type of problem you're dealing with. Packed with examples and exercises, Get Programming with Scala is the perfect starting point for developers with some OO knowledge who want to learn Scala and pick up a few FP skills along the way.Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.About the technology
Scala developers are in high demand. This flexible language blends object-oriented and functional programming styles so you can write flexible, easy-to-maintain code. Because Scala runs on the JVM, your programs can interact seamlessly with Java libraries and tools. If you're comfortable writing Java, this easy-to-read book will get you programming with Scala fast.About the book
Get Programming with Scala is a fast-paced introduction to the Scala language, covering both Scala 2 and Scala 3. You'll learn through lessons, quizzes, and hands-on projects that bring your new skills to life. Clear explanations make Scala's features and abstractions easy to understand. As you go, you'll learn to write familiar object-oriented code in Scala and also discover the possibilities of functional programming.What's inside Apply object-oriented principles in Scala
Learn the core concepts of functional programming
Use types to enforce program requirements
Use abstractions to avoid code duplication
Write meaningful tests and recognize code smellsAbout the reader
For developers who know an OOP language like Java, Python, or C#. No experience with Scala or functional programming required.About the author
Daniela Sfregola is a Senior Software Engineer and a Scala user since 2013. She is an active contributor to the Scala Community, a public speaker at Scala conferences and meetups, and a maintainer of open-source projects.Table of ContentsUnit 0 HELLO SCALA!
Unit 1 THE BASICS
Unit 2 OBJECT-ORIENTED FUNDAMENTALS
Unit 3 HTTP SERVER
Unit 4 IMMUTABLE DATA AND STRUCTURES
Unit 5 LIST
Unit 6 OTHER COLLECTIONS AND ERROR HANDLING
Unit 7 CONCURRENCY
Unit 8 JSON (DE)SERIALIZATION

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Get Programming with Scala by Daniela Sfregola in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Languages. We have over one million books available in our catalogue for you to explore.

Information

Publisher
Manning
Year
2021
ISBN
9781638352259

1 Why Scala?

After reading this lesson, you’ll be able to
  • Discuss the advantages of adopting Scala
  • Describe the execution flow of a typical Scala program
  • Define the key features of the Scala language
In this lesson, you’ll discover why Scala is an excellent language to learn and why its adoption is increasing so rapidly. You’ll see how Scala relates to the JVM and the key features that make it unique. You’ll also start looking at snippets of Scala code to get an idea of its appearance. After giving you an overview of the Scala language, you’ll continue with the next lesson, in which you’ll install the Scala REPL and use it to interpret snippets of code.

1.1 Why Scala?

Why should you spend time and effort in learning Scala? Why is it becoming so popular? What are the advantages of adopting it? Let’s discuss its main selling points.
The JVM The JVM is the standard platform for running Scala programs. Sun Microsystems introduced it in 1994—more than 25 years ago. Since then, the industry has been extensively relying on it. The Java community has also been extremely active, and it has produced an impressive amount of libraries. Thanks to its integration with Java, you can use all these libraries in your Scala programs; this is also true for any Java legacy code you or your company may have.
A Hybrid Language Scala manages to combine two programming techniques that are considerably different: the object-oriented style with the functional one. When executing code on the JVM, the object-oriented approach can be more performant but prone to errors. When using mutable state, your program will re-allocate its memory: every time a change occurs, it will change the data in place. However, sharing state can cause your application to suffer from data inconsistency issues due to multiple processes accessing and modifying the same portion of data.
A functional approach can be more readable and reusable but not as performant. Thanks to immutable data and structures, you can guarantee your program’s correctness when dealing with concurrency: your data never changes, so it is safe to share it. Your code will also be easier to understand and reuse because all its components will be independent of external factors outside its control. But recreating data rather than updating could be a memory-expensive operation, even though it has massively improved thanks to numerous optimizations and efficient garbage collection strategies in recent years.
In Scala, you do not have to stick to a particular style, but you can take advantage of one or the other paradigm depending on the specific task you are solving. Look at figure 1.1 for a comparison of how the two approaches tackle different programming tasks.
Figure 1.1 Comparison of the object-oriented and functional programming styles and how they handle different programming tasks
Concise Syntax Scala’s programming style is relatively concise, particularly when compared to other languages like Java. Having a compact syntax can increase both the productivity and readability of your program. At the beginning of your journey with Scala, you may find it quite overwhelming. In this book, syntax diagrams will help you master new topics and make your learning path smoother.
Flexibility Scala is extremely flexible: you can achieve the same goal in more than one way, making the language extremely fun and exciting to use. The opportunity to choose between different programming paradigms allows you to gradually shift your mindset from one approach to another without committing to a specific style since day one. For example, you can dip your toe in the functional programming world without any long-term commitment. In this book, you’ll start writing Scala code using an object-oriented style and then gradually move to a more functional one.
Concurrency Thanks to its use of immutable data structures and expressive type system, dealing with concurrency is less prone to errors than in other languages. As a result, Scala programs tend to utilize resources more efficiently, and they usually perform better under pressure.
Big Data and Spark Thanks to Scala’s features and optimizations at its compile level, the community has developed new performant tools for big data processing. Apache Spark is the most popular of these tools. Thanks to Scala’s lazy evaluation, which is a topic you are going to discover in unit 8, Spark can perform optimizations at compile time that have huge impacts on its runtime performance:
“Run programs up to 100× faster than Hadoop MapReduce in memory, or 10× faster on disk.&#8221.
Source: https://spark.apache.org

1.2 Scala and the JVM

Scala takes its name from the word scalable. Martin Odersky and his team designed it in 2004 with the intent of creating a language for the Java Virtual Machine (JVM) that can easily handle high volumes of requests.
To understand the execution of a Scala program, let’s compare it with the typical execution flow of a Java program; figure 1.2 provides a visual representation of the two processes. The JVM is a machine to perform tasks by executing a well-defined set of operations, called bytecode. A JVM language aims to translate its code into executable JVM bytecode, usually formed by multiple files with extension *.class. When coding in Java, you save your source files with extension *.java and use the compiler javac to prod...

Table of contents

  1. inside front cover
  2. Get Programming with Scala
  3. Copyright
  4. dedication
  5. Contents
  6. front matter
  7. Unit 0. Hello Scala!
  8. 1 Why Scala?
  9. 2 The Scala environment
  10. 3 Scala Build Tool (sbt)
  11. Unit 1. The Basics
  12. 4 Values and variables
  13. 5 Conditional constructs and loops
  14. 6 Function as the most fundamental block of code
  15. 7 Classes and subclasses to represent the world
  16. 8 The vending machine
  17. Unit 2. Object-oriented fundamentals
  18. 9 Import and create packages
  19. 10 Scope your code with access modifiers
  20. 11 Singleton objects
  21. 12 Traits as interfaces
  22. 13 What time is it?
  23. Unit 3. HTTP server
  24. 14 Pattern matching
  25. 15 Anonymous functions
  26. 16 Partial functions
  27. 17 HTTP API with http4s
  28. 18 The time HTTP server
  29. Unit 4. Immutable data and structures
  30. 19 Case classes to structure your data
  31. 20 Higher order functions
  32. 21 What is purity?
  33. 22 Option
  34. 23 Working with Option: map and flatMap
  35. 24 Working with Option: For-comprehension
  36. 25 Tuple and unapply
  37. 26 Rock, Paper, Scissors, Lizard, Spock!
  38. Unit 5. List
  39. 27 List
  40. 28 Working with List: map and flatMap
  41. 29 Working with List: Properties
  42. 30 Working with List: Element selection
  43. 31 Working with List: Filtering
  44. 32 Working with List: Sorting and other operations
  45. 33 The movies dataset
  46. Unit 6. Other collections and error handling
  47. 34 Set
  48. 35 Working with Set
  49. 36 Map
  50. 37 Working with Map
  51. 38 Either
  52. 39 Working with Either
  53. 40 Error handling with Try
  54. 41 The library application
  55. Unit 7. Concurrency
  56. 42 Implicit and type classes
  57. 43 Future
  58. 44 Working with Future: map and flatMap
  59. 45 Working with Future: For-comprehension and other operations
  60. 46 Database queries with Quill
  61. 47 The quiz application: Part 1
  62. Unit 8. JSON (de)serialization
  63. 48 JSON (de)serialization with circe
  64. 49 Lazy evaluation
  65. 50 The IO type
  66. 51 Working with the IO type
  67. 52 Testing with ScalaTest
  68. 53 The quiz application: Part 2
  69. Index
  70. inside back cover