
Explore Scala through 65 practical examples and learn how statements become expressions, with first-class functions and rich collections for functional programming, especially for developers who know Java.
A quick run-through of the reasons why people are so excited about Scala
Let's plunge in! Setting up Scala is surprisingly simple. You do need Java though.
Explain how to write a hello world in Scala using two approaches: a main method inside an object, and an object extending App, showing compile, run, and print output.
Immutable variables are a big part of Scala - understand why.
Scala aims to achieve the best of both worlds: rapid prototyping like Python, type safety like Java, C++ and other statically typed languages.
Scala has 3 types that unify the type system: Any (literally anything!), AnyRef (supertype to all reference types) and AnyVal (supertype to all value types)
Nil, null, Null, Nothing, Unit..Scala has many ways to skin a non-existent cat!
This is an important semantic difference - statements don't return a value, while expressions do. Scala converts many familiar constructs into expressions - which allows them to be composed.
Expression blocks are prototypical "r-values", i.e. they sit on the right of assignment statements.
Explore how nested scopes in Scala expression blocks determine variable values and the final return, using pi and area to illustrate shadowing and scope rules.
Scala has if-else expressions (not if-else statements). This is a fine point, and an important one.
Pattern matching is very easy in Scala - this is a more popular language construct than if/else expressions
Explore match expressions in Scala, including pattern guards and or-ed expressions, to extend conditions with boolean expressions and value bindings.
Discover how scala match expressions enable downcasting with pattern variables to replace nested ifs and improve over java switch, using type-based cases and placeholders.
Scala has a way to convert for loop statements into for loop expressions - using 'yield'
Explore two for loop styles in Scala: value binding over a list and numeric ranges using until or to; and learn how foreach, map, and flatMap offer alternatives.
While loops are pure statements, and are rather clunky. See why they are not the most popular construct in town.
Explore the differences between functions and methods and learn functional programming concepts such as partially applied functions and first-class functions.
Functions are 'first class citizens' in Scala - this actually has a technical term!
Discover how Scala treats functions as objects and entities, defined with the value keyword and stored in val or var, while methods use def and stay tied to a class.
Learn that functions are named, reusable expression blocks with parameters and return values, behaving like first-class functions and enabling closures across scope in Scala.
Explore the difference between methods and functions in Scala and learn how to convert a method to a function object using explicit signatures or the underscore operator.
Scala allows parameters to be specified out of order - using their names
Yet another mechanism to encourage code re-use
Scala loves using formidable terms for everyday concepts:-) Think generics in Java.
Learn how procedures are named, reusable statements in Scala that are functions without a return value, must produce a side effect to change state, and use def with Unit.
Explore Scala methods with and without parentheses, where parentheses denote function-like behavior and methods without parentheses look like properties, and learn how the compiler enforces their invocation.
Functions that take in other functions as parameters, or return function objects, are called Higher Order Functions
Discover scala's placeholder syntax using the underscore for pattern matching, method to function conversion, and anonymous function invocation. Learn how underscores fill parameters in order for concise higher-order calls.
Partially Applied Functions are a really cool code re-use trick: fix some (but not all) parameters of a function and you have a new function!
Explore by-name parameters in Scala, showing how the first evaluation is lazy and every subsequent reference triggers ego evaluation for both functions and methods.
Explore closures in Scala, where nested functions capture their referencing environment and outlive their outer scope. Learn how higher-order functions return language-specific greetings.
Discover how Scala supports collections with higher-order functions, where container classes provide methods that apply function objects to their elements in powerful ways.
Learn Scala tuples as ordered, immutable containers of heterogeneous values. Create with parentheses, access with _1, _2, or deconstruct into variables; they are not true collections, making iteration awkward.
Explore creating and manipulating lists in Scala, including immutable lists, Nil, the cons operator ::, the apply method, and concatenation with ::: and ++, plus zip and flatten.
Learn simple list operations in Scala, including head, tail, size, reverse, distinct, drop, slice, split, and sort, with a weekdays example and notes on for loops, while loops, and nil.
Explore higher order functions on Scala collections, including map, for each, and filter, to transform data and build functional pipelines.
Explore fold, foldLeft, foldRight and scan, scanLeft, scanRight in Scala. Understand how they relate and that they operate from right to left, with results shaped by leftmost and rightmost elements.
Explore reduce, reduceLeft, and reduceRight in Scala, focusing on initial values and left versus right associative behavior, and compare them with scan.
Explore mutable collections in Scala, learn to grow, shrink, and modify elements, and convert between mutable and immutable lists, maps, and sets using builders and the for each pattern.
Explore option collections in Scala, a type-safe alternative to null, using Some and None, and handle division by zero with get, getOrElse, and pattern matching.
Explore error handling in Scala using Try, comparing success and failure patterns with option collections to gracefully manage exceptions and specify a plan B.
Explore classes and objects as the core of object oriented programming in Scala, and review the week’s differences discussed on this subject.
Explore primary and auxiliary constructors in Scala, including class parameters, the primary constructor's default values, and base-class initialization without a super keyword.
Explore inheritance from classes in Scala by building a base shape and derived rectangles and squares, showing how derived classes invoke the base constructor via the primary constructor and polymorphism.
Explore anonymous classes in Scala to quickly instantiate abstract classes by redefining methods in braces and using new, with shape and sheep examples returning the value 100 for get area.
Explore type parameters and parametric polymorphism in Scala with a generic area calculator, using square brackets to define a placeholder type for shapes and enable type-safe, reusable code.
Explore lazy values in Scala, marking heavy fields as lazy to defer initialization until referenced, contrasting eager initialization and showing performance benefits when objects may not be used.
Learn how Scala uses a default apply method to treat an instance as a function, demonstrated with an area calculator invoking apply on a rectangle.
Explore Scala access modifiers, including public by default, private, and protected. Learn how final makes a class a leaf in inheritance, and how sealed restricts extensions to the same file.
Learn how singleton objects in Scala, defined with the object keyword, provide static-like behavior via companion objects, explain why Scala omits static, and define the entry point.
Explore companion objects in Scala, where a singleton object and a class share a name in the same file to access private data and implement a factory pattern via apply.
Explore Scala case classes as lightweight storage objects with auto-generated fields and a companion apply method, enabling easy instantiation and pattern matching, while avoiding inheritance.
Explore Scala self types and how traits enforce dependencies through self-typed annotations, enabling on-the-fly dependency injection and flexible mixing of traits to satisfy interfaces.
These 65 examples will make this cool-new-kid-on-the-block your steady, reliable friend
Let's parse that.
What's Included: