Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Data Engineering with Scala and Spark

You're reading from  Data Engineering with Scala and Spark

Product type Book
Published in Jan 2024
Publisher Packt
ISBN-13 9781804612583
Pages 300 pages
Edition 1st Edition
Languages
Authors (3):
Eric Tome Eric Tome
Profile icon Eric Tome
Rupam Bhattacharjee Rupam Bhattacharjee
Profile icon Rupam Bhattacharjee
David Radford David Radford
Profile icon David Radford
View More author details

Table of Contents (21) Chapters

Preface Part 1 – Introduction to Data Engineering, Scala, and an Environment Setup
Chapter 1: Scala Essentials for Data Engineers Chapter 2: Environment Setup Part 2 – Data Ingestion, Transformation, Cleansing, and Profiling Using Scala and Spark
Chapter 3: An Introduction to Apache Spark and Its APIs – DataFrame, Dataset, and Spark SQL Chapter 4: Working with Databases Chapter 5: Object Stores and Data Lakes Chapter 6: Understanding Data Transformation Chapter 7: Data Profiling and Data Quality Part 3 – Software Engineering Best Practices for Data Engineering in Scala
Chapter 8: Test-Driven Development, Code Health, and Maintainability Chapter 9: CI/CD with GitHub Part 4 – Productionalizing Data Engineering Pipelines – Orchestration and Tuning
Chapter 10: Data Pipeline Orchestration Chapter 11: Performance Tuning Part 5 – End-to-End Data Pipelines
Chapter 12: Building Batch Pipelines Using Spark and Scala Chapter 13: Building Streaming Pipelines Using Spark and Scala Index Other Books You May Enjoy

Understanding functional programming

Functional programming is based on the principle that programs are constructed using only pure functions. A pure function does not have any side effects and only returns a result. Some examples of side effects are modifying a variable, modifying a data structure in place, and performing I/O. We can think of a pure function as just like a regular algebraic function.

An example of a pure function is the length function on a string object. It only returns the length of the string and does nothing else, such as mutating a variable. Similarly, an integer addition function that takes two integers and returns an integer is a pure function.

Two important aspects of functional programming are referential transparency (RT) and the substitution model. An expression is referentially transparent if all of its occurrences can be substituted by the result of the expression without altering the meaning of the program.

In the following example, Example 1.1, we set x and then use it to set r1 and r2, both of which have the same value:

scala> val x: String = "hello"
x: String = hello
scala> val r1 = x + " world!"
r1: String = hello world!
scala> val r2 = x + " world!"
r2: String = hello world!

Example 1.1

Now, if we replace x with the expression referenced by x, r1 and r2 will be the same. In other words, the expression hello is referentially transparent.

Example 1.2 shows the output from a Scala interpreter:

scala> val r1 = "hello" + " world!"
r1: String = hello world!
scala> val r2 = "hello" + " world!"
r2: String = hello world!

Example 1.2

Let’s now look at the following example, Example 1.3, where x is an instance of StringBuilder instead of String:

scala> val x = new StringBuilder("who")
x: StringBuilder = who
scala> val y = x.append(" am i?")
y: StringBuilder = who am i?
scala> val r1 = y.toString
r1: String = who am i?
scala> val r2 = y.toString
r2: String = who am i?

Example 1.3

If we substitute y with the expression it refers to (val y = x.append(" am i?")), r1 and r2 will no longer be equal:

scala> val x = new StringBuilder("who")
x: StringBuilder = who
scala> val r1 = x.append(" am i?").toString
r1: String = who am i?
scala> val r2 = x.append(" am i?").toString
r2: String = who am i? am i?

Example 1.4

So, the expression x.append(" am i?") is not referentially transparent.

One of the advantages of the functional programming style is it allows you to apply local reasoning without having to worry about whether it updates any globally accessible mutable state. Also, since no variable in the global scope is updated, it considerably simplifies building a multi-threaded application.

Another advantage is pure functions are also easier to test as they do not depend on any state apart from the inputs supplied, and they generate the same output for the same input values.

We won’t delve deep into functional programming as it is outside of the scope of this book. Please refer to the Further reading section for additional material on functional programming. In the rest of this chapter, we will provide a high-level tour of some of the important language features that the subsequent chapters build upon.

In this section, we looked at a very high-level introduction to functional programming. Starting with the next section, we will look at Scala language features that enable both functional and object-oriented programming styles.

You have been reading a chapter from
Data Engineering with Scala and Spark
Published in: Jan 2024 Publisher: Packt ISBN-13: 9781804612583
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}