Scala Lesson 21 – FP Basics | Dataplexa

Functional Programming Basics

In this lesson, you will learn the fundamentals of Functional Programming (FP) in Scala. Functional programming is a core strength of Scala and is widely used in real-world Scala applications.

Scala combines Object-Oriented Programming and Functional Programming, allowing developers to write clean, safe, and expressive code.


What Is Functional Programming?

Functional Programming is a programming paradigm where computation is treated as the evaluation of mathematical functions.

In functional programming:

  • Functions are first-class values
  • Data is immutable by default
  • Side effects are minimized
  • Programs are built by composing functions

Why Scala Supports Functional Programming

Scala was designed with functional programming in mind. It provides language features that make functional code natural and readable.

Some of these features include:

  • Immutable variables using val
  • First-class and higher-order functions
  • Pattern matching
  • Powerful collections API

Immutable Values

In functional programming, data should not change after it is created. Scala encourages immutability using val.

val x = 10
// x = 20   // This will cause a compilation error

Once assigned, the value of x cannot be changed. This helps prevent bugs and makes code easier to reason about.


Functions as Values

In Scala, functions are treated as values. You can assign them to variables, pass them as arguments, and return them from other functions.

val add = (a: Int, b: Int) => a + b

println(add(3, 5))

Here, add is a function value that takes two integers and returns their sum.


Pure Functions

A pure function:

  • Always returns the same output for the same input
  • Does not modify external state

Example of a pure function:

def square(n: Int): Int = n * n

Pure functions make programs predictable and easier to test.


Higher-Order Functions

A higher-order function is a function that:

  • Takes another function as a parameter, or
  • Returns a function

Example using a function as a parameter:

def applyOperation(a: Int, b: Int, op: (Int, Int) => Int): Int = {
  op(a, b)
}

val result = applyOperation(4, 6, (x, y) => x + y)
println(result)

This allows flexible and reusable logic.


Using Functional Style with Collections

Scala collections support functional operations like map, filter, and reduce.

val numbers = List(1, 2, 3, 4, 5)

val doubled = numbers.map(_ * 2)
val even = numbers.filter(_ % 2 == 0)

println(doubled)
println(even)

These operations avoid explicit loops and produce clean, readable code.


Benefits of Functional Programming

  • Less mutable state → fewer bugs
  • Easier parallel and concurrent programming
  • Improved code readability
  • Better testability

📝 Practice Exercises


Exercise 1

Create an immutable variable and try modifying it.

Exercise 2

Write a function that returns the cube of a number.

Exercise 3

Use map to square all numbers in a list.


✅ Practice Answers


Answer 1

val a = 5
// a = 10  // Error: reassignment to val

Answer 2

def cube(n: Int): Int = n * n * n

Answer 3

val nums = List(1, 2, 3)
val squared = nums.map(n => n * n)
squared

What’s Next?

In the next lesson, you will dive deeper into Higher-Order Functions and how Scala uses them extensively in real applications.