O'Reilly logo
live online training icon Live Online training

Fundamentals of Functional Programming - With Examples in Scala

Improve your coding skills by writing cleaner, reusable and easily testable code

James Cutajar

The difficult part about learning functional languages such as Scala is making the jump from an imperative programming language to a more functional one. The goal of this training is to make this transition easier. Apply the principles learnt in this training session to other languages such as Haskell, JavaScript, F#, Clojure and others.

Scala is a good language to pick to make shift to the functional world easier. This is because Scala does not force you to program in a functional manner but instead encourages you to do so. This allows for a gradual imperative to functional progression.

In this training course we start by explaining some of the principles in functional programming. These principles help us to write more readable, bug free and thread safe code. We’ll see an explanation of each principle and then see how they can be applied in practice. We’ll describe the usage of tools available in the Scala language through examples and exercises and using these same tools we’ll show how they can be used to follow described principles.

We then move on to show more features from the Scala language always keeping in mind functional principles. We step through different types of collections and various types of operations that we can perform on them. Later we explore the Option construct in the language and see how it can be used in practice.

In the second part of this training we explore more useful aspects from Scala, such as pattern matching, case classes and partial functions. All concepts are backed by clear examples and various activities. Again special care is taken to respect the described functional principles.

What you'll learn-and how you can apply it

  • Learn about Scala functions, lambdas, higher order functions and comprehensions, these are the building blocks of your functional code.
  • Scala Collections, by exploring Lists, Sets, and Maps and the various operations performed on them.
  • Learn how to effectively use options in Scala through theory and exercises.
  • Discover the various types of pattern matching available and utilise those learnings through the labs offered during the course.
  • See the advantages of using case classes through examples
  • Understand what are partial Functions and how they are used in practice.

This training course is for you because...

This training is aimed at developers who want to learn about functional programming Scala to improve their coding skills. The session is targeted to an audience that has minimal or zero Scala experience. Ideally the developer has some knowledge of programming in Java, although this is not strictly required.

Prerequisites

  • Java IDE (ideally intellij)
  • Java SDK
  • Scala SDK

Recommended preparation:

Sorting and Searching Algorithms in Scala (video)

Materials, downloads, or Supplemental Content needed in advance

Recommended followup:

Implementing Graph Algorithms Using Scala (book)

About your instructor

  • James Cutajar is a software developer with an interest in scalable, high-performance computing, and distributed algorithms. He has worked in the technology field for over 15 years in various industries. He is also an author of various video courses on Scala and a book on data structures and algorithms. Through his career, he has been an instructor, open source contributor, blogger, and a tech evangelist. When he is not writing software, he is riding his motorbike, surfing, scuba diving, or flying light aircraft. He was born in Malta, lived in London for almost a decade, and is now working in Portugal.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

DAY 1

Section 1: Fundamentals of Functional Programming (50 Mins)

  • What does it mean to be functional?
  • Functional principles
  • Why Scala?
  • Scala REPL
  • Scala IDE
  • First look at Scala

(10 min break)

Section 2: Everything is a function! (45 mins)

  • Lambdas
  • Higher Order functions
  • Ifs
  • Loops vs Comprehensions
  • Lab 1: Exercise (15 min - 10 min activity + 5 min break)
  • Activity to help understand how to use comprehensions

Section 3: Working with Functional Collections Part 1 (45 minutes)

  • List Collection
  • Set Collection
  • filter Function
  • exists Function
  • forAll Function
  • map Function

Lab 2: Exercise (15 min, 5 min break)

  • Function to solve a specific problem by using the collections and operations learned during the lecture.

Section 4: Working with Functional Collections Part 2 (45 minutes)

  • flatMap and flatten Functions
  • Tuples and zipWithIndex
  • Map Collection
  • Streams

Lab 3: Exercise (15 min + 5 min break)

  • Make use of streams and operations seen in this section to implement an algorithm

DAY 2

Section 5: Options (45 minutes)

  • Fundamentals
  • foreach Function
  • map and getOrElse Functions
  • filter Function example
  • Options in Comprehensions

Lab 4: Exercise (15 min + 5 min break)

  • Learn how to use options through a practical exercise

Section 6: Case Classes (45 minutes)

  • Constructors and apply methods
  • Default parameters
  • How to use Case classes

Lab 5: Exercise (15 min + 5 min break)

  • Grasp case classes usage by working through this activity

Section 7: Pattern Matching Part 1 (45 minutes)

  • Fundamentals
  • Matching on Value
  • Matching on Type
  • Case classes (and Options)

Lab 6: Exercise (15 min + 5 min break)

  • Practice the various types of pattern matching seen by completing this lab.

Section 8: Pattern Matching Part 2 (35 minutes)

  • What are Guards?
  • Matching using Lists

Lab 7: Exercise (15 min + 5 min break)

  • In this lab we pick another problem and write the solution using the pattern matching techniques described in this session.
  • Wrap-up and remaining Q&A (10 min)