Download ebooks file Functional and Concurrent Programming Core Concepts and Features 1 / converted Edition Michel Charpentier all chapters
Download ebooks file Functional and Concurrent Programming Core Concepts and Features 1 / converted Edition Michel Charpentier all chapters
com
https://ebookmeta.com/product/functional-and-concurrent-
programming-core-concepts-and-features-1-converted-edition-
michel-charpentier/
OR CLICK HERE
DOWLOAD NOW
https://ebookmeta.com/product/concurrent-programming-algorithms-
principles-and-foundations-1st-edition-michel-raynal/
ebookmeta.com
https://ebookmeta.com/product/functional-analysis-fundamentals-and-
applications-2nd-edition-michel-willem/
ebookmeta.com
https://ebookmeta.com/product/fundamental-and-advanced-fetal-imaging-
ultrasound-and-mri-2nd-edition-beth-kline-fath/
ebookmeta.com
A Colorful Home Create Lively Palettes for Every Room
Susan Hable
https://ebookmeta.com/product/a-colorful-home-create-lively-palettes-
for-every-room-susan-hable/
ebookmeta.com
https://ebookmeta.com/product/zeven-ways-to-kill-your-lover-shannon/
ebookmeta.com
https://ebookmeta.com/product/heartsblood-omnibus-1st-edition-fenech-
selina-a/
ebookmeta.com
https://ebookmeta.com/product/sql-server-2022-administration-inside-
out-1st-edition-randolph-west/
ebookmeta.com
https://ebookmeta.com/product/the-android-malware-handbook-qian-han/
ebookmeta.com
The Afterlife of Ottoman Europe Muslims in Habsburg Bosnia
Herzegovina Stanford Studies on Central and Eastern Europe
1st Edition Amzi-Erdogdular
https://ebookmeta.com/product/the-afterlife-of-ottoman-europe-muslims-
in-habsburg-bosnia-herzegovina-stanford-studies-on-central-and-
eastern-europe-1st-edition-amzi-erdogdular/
ebookmeta.com
Functional, Object-Oriented, and
Concurrent Programming
Michel Charpentier
Table of Contents
Foreword
Preface
Acknowledgements
About the Author
Chapter 3. Immutability
A Brief Interlude
Glossary
Contents
Foreword
Preface
Why Scala?
Target Audience
How to Read This Book
Additional Resources
Acknowledgements
About the Author
Chapter 3. Immutability
3.1 Pure and Impure Functions
3.2 Actions
3.3 Expressions Versus Statements
3.4 Functional Variables
3.5 Immutable Objects
3.6 Implementation of Mutable State
3.7 Functional Lists
3.8 Hybrid Designs
3.9 Updating Collections of Mutable/Immutable Objects
Summary
A Brief Interlude
Glossary
Foreword
Before you start reading this book, it is important to think about the
distinction between programming languages and programming language
features. I believe that developers benefit from being able to rely on an
extensive set of programming language features, and that a solid
understanding of these features—in any language—will help them be
productive in a variety of programming languages, present or future.
The world of programming languages is varied, and continues to evolve all
the time. As a developer, you are expected to adapt, and to repeatedly
transfer your programming skills from one language to another. Learning
new programming languages is made easier by mastering a set of core
features that today’s languages often share, and that many of tomorrow’s
languages are likely to use as well.
Programming language features are illustrated in this book with numerous
code examples, primarily in Scala (for reasons that are detailed later). The
concepts, however, are relevant—with various degrees—to other popular
languages like Java, C++, Kotlin, Python, C#, Swift, Rust, Go, JavaScript,
and whatever languages might pop up in the future to support strong typing
as well as functional and/or concurrent programming.
As an illustration of the distinction between languages and features,
consider the following programming task:
Shift every number from a given list by a random amount between −10 and 10. Return a list of
shifted numbers, omitting all values that are not positive.
Although they are written in two different languages, both functions follow
a similar strategy: Create a new empty list to hold the shifted numbers, shift
each original number by a random amount, and add the new values to the
result list only when they are positive. For all intents and purposes, the two
programs are the same.
Other programmers might choose to approach the problem differently. Here
is one possible Java variant:
Java
The details of this implementation are not important for now—it relies on
functional programming concepts that will be discussed in Part I. What
matters is that the code is noticeably different from the previous Java
implementation.
You can write a similar functional variant in Python:
Python
The important concept to understand here is the use of map and filter (and
more generally higher-order functions, of which they are an example), not
list comprehension. You benefit from this understanding in two ways. First,
more languages support higher-order functions than have a comprehension
syntax. If you are programming in Java, for instance, you will have to write
map and filter explicitly (at least for now). Second, if you ever face a
language that uses a somewhat unusual syntax, as Python does with list
comprehension, it will be easier to recognize what is going on once you
realize that it is just a variation of a concept you already understand.
The preceding code examples illustrate a contrast between a program
written in plain imperative style, and one that leverages the functional
programming features available in many languages. I can make a similar
argument with concurrent programming. Languages (and libraries) have
evolved, and there is no reason to write today’s concurrent programs the
way we did 20 years ago. As a somewhat extreme example, travel back not
quite 20 years to 2004, the days of Java 1.4, and consider the following
problem:
Given two tasks that each produce a string, invoke both tasks in parallel, and return the first
string that is produced.
This implementation uses features with which you may not be familiar (but
which are covered in Part II of the book).1 Here are the important points to
notice:
1 One reason such old-fashioned features are still covered in this book is that I believe they help
us understand the richer and fancier constructs that we should be using in practice. The other reason
is that the concurrent programming landscape is still evolving, and recent developments, such as
virtual threads in the Java Virtual Machine, have the potential to make these older concepts relevant
again.
Why Scala?
As mentioned earlier, most of the code illustrations in this book are written
in Scala. This may not be the language you are most familiar with, or the
language in which you plan to develop your next application. It is a fair
question to wonder why I chose it instead of a more mainstream language.
Scala is a programming language that aims to combine object-oriented and
functional programming, with good support for concurrency as well.2 It is a
hybrid language—also called a multi-paradigm language. In fact, all three
versions of the random shifting program already written in Java and in
Python can be written in Scala:
2Different incarnations of Scala exist. This book uses the most common flavor of Scala, namely,
the one that runs on the JVM and leverages the JVM’s support for concurrency.
Scala
def randShift(nums: List[Int], rand: Random): List[Int] = {
val shiftedNums = List.newBuilder[Int]
for (num <- nums) {
val shifted = num + rand.between(-10, 11)
if (shifted > 0) {
shiftedNums += shifted
}
}
shiftedNums.result()
}
Target Audience
The target audience is programmers with enough experience to not be
distracted by simple matters of syntax. I assume prior Java experience, or
enough overall programming experience to read and understand simple Java
code. Concepts such as classes, methods, objects, types, variables, loops,
and conditionals are assumed to be familiar. A rudimentary understanding
of program execution—execution stack, garbage collection, exceptions—is
also assumed, as well as basic exposure to data structures and algorithms.
For other key terms, covered in depth in the book, the glossary provides a
basic definition, and indicates the appropriate chapter or chapters where the
concept is presented.
No prior knowledge of functional or concurrent programming is assumed.
No prior knowledge of Scala is assumed. Presumably, many readers will
have some understanding of functional or concurrent concepts, such as
recursion or locks, but no such knowledge is required. For instance, I do not
expect you to necessarily understand the functional Python and Java
programs discussed earlier, or the two Java concurrent programs, or the last
two Scala functions. Indeed, I would argue that if these programs feel
strange and mysterious, this book is for you! By comparison, the imperative
variant of the number-shifting program should be easy to follow, and I
expect you to understand the corresponding code, whether it is written in
Java, Python, or Scala. You are expected to understand simple Scala syntax
when it is similar to that of other languages, and to pick up new elements as
they are introduced.
The syntax of Scala was inspired by Java’s syntax—and that of Java by C’s
syntax— which should make the transition fairly straightforward for most
programmers. Scala departs from Java in ways that will be explained as
code examples are introduced. For now, I’ll highlight just three differences:
• Semicolon inference. In Scala, terminating semicolons are inferred by
the compiler and rarely used explicitly. They may still appear
occasionally—for instance, as a way to place two statements on the
same line.
• No “return” needed. Although a return keyword exists in Scala, it is
seldom used. Instead, a function implicitly returns the value of the last
expression evaluated in its body.
• Significant indentation. The curly braces used to define code blocks
can often be inferred from indentation, and are optional. The first Scala
randShift variant can been written:
Scala
end randShift
Additional Resources
The book’s companion website is hosted at https://fcpbook.org. It
contains additional resources, a list of errata, and access to the code
illustrations, which are available from GitHub. The code examples were
compiled and tested using Scala 3.2. The author welcomes comments and
discussions, and can be reached at author@fcbbook.org.
Acknowledgements
BEEHLER v. DANIELS
Supreme Court, Rhode Island, May 1, 1894.
Reported in 18 Rhode Island Reports, 563.
WINTERBOTTOM v. WRIGHT
In the Exchequer, June 6, 1842.
Reported in 10 Meeson & Welsby, 109.
HEAVEN v. PENDER
In the Court of Appeal, July 30, 1883.
Reported in 11 Queen’s Bench Division, 503.