Functional and Concurrent Programming Core Concepts and Features 1st Edition Charpentier 2024 Scribd Download
Functional and Concurrent Programming Core Concepts and Features 1st Edition Charpentier 2024 Scribd Download
com
https://textbookfull.com/product/functional-and-
concurrent-programming-core-concepts-and-
features-1st-edition-charpentier/
https://textbookfull.com/product/learning-concurrent-programming-in-
scala-prokopec/
textbookfull.com
https://textbookfull.com/product/millers-review-of-orthopaedics-7th-
edition-mark-miller/
textbookfull.com
Ayahuasca Journals A Journey Through Impossible Realms
James O’Reilly
https://textbookfull.com/product/ayahuasca-journals-a-journey-through-
impossible-realms-james-oreilly/
textbookfull.com
https://textbookfull.com/product/publication-manual-of-the-american-
psychological-association-7th-edition-coll/
textbookfull.com
https://textbookfull.com/product/is-entrepreneurship-dead-the-truth-
about-startups-in-america-scott-a-shane/
textbookfull.com
https://textbookfull.com/product/intellectual-property-and-financing-
strategies-for-technology-startups-1st-edition-gerald-b-halt-jr/
textbookfull.com
If I'm Being Honest 1st Edition Emily Wibberley & Austin
Siegemund-Broka [Wibberley
https://textbookfull.com/product/if-im-being-honest-1st-edition-emily-
wibberley-austin-siegemund-broka-wibberley/
textbookfull.com
Functional and Concurrent
Programming
This page intentionally left blank
Functional and Concurrent
Programming
Michel Charpentier
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and the publisher was aware of a trademark
claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed
or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is
assumed for incidental or consequential damages in connection with or arising out of the use of the
information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which
may include electronic versions; custom cover designs; and content particular to your business, training
goals, marketing focus, or branding interests), please contact our corporate sales department at corpsales
@pearsoned.com or (800) 382-3419.
For questions about sales outside the United States, please contact [email protected].
All rights reserved. This publication is protected by copyright, and permission must be obtained from
the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any
form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information re-
garding permissions, request forms and the appropriate contacts within the Pearson Education Global
Rights & Permissions Department, please visit www.pearson.com.
ISBN-13: 978-0-13-746654-2
ISBN-10: 0-13-746654-4
ScoutAutomatedPrintCode
To Karen and Andre
This page intentionally left blank
Contents
Preface xxv
Acknowledgments xxxv
Chapter 3 Immutability 21
3.1 Pure and Impure Functions 21
3.2 Actions 23
3.3 Expressions Versus Statements 25
3.4 Functional Variables 26
vii
viii Contents
Glossary 463
Index 469
This page intentionally left blank
List of Listings
2.1 Example of a method defined with a symbolic name. 13
2.2 Example of a method defined for infix invocation. 13
2.3 Example of a method added to a type by extension. 14
2.4 Example of a local function defined within another function. 14
2.5 Example of a function with variable-length arguments. 15
2.6 Example of a function with default values for some of its arguments. 16
2.7 Example of a function parameterized by a type; contrast with Lis. 2.8. 18
2.8 Example of parameterization by multiple types; contrast with Lis. 2.7. 19
xv
xvi List of Listings
7.1 Simple list lookup, using tail recursion; see also Lis. 7.2. 80
7.2 Simple list lookup, using tail recursion and pattern matching. 81
7.3 List length, not tail recursive; contrast with Lis. 7.4. 81
7.4 List length, using tail recursion; contrast with Lis. 7.3. 82
7.5 Recursive implementation of drop on lists; see also Lis. 7.6. 82
7.6 Recursive implementation of drop on lists; see also Lis. 7.5. 83
7.7 Implementation of getAt on lists, using drop. 84
7.8 Recursive implementation of take; contrast with Listings 7.21 to 7.23. 84
7.9 Recursive implementation of list concatenation. 86
7.10 Appending at the end of a list using concatenation. 86
7.11 Recursive implementation of list flattening. 87
7.12 Recursive implementation of list zipping. 88
7.13 Recursive implementation of list splitting. 89
7.14 Recursive implementation of list grouping. 89
7.15 Linear, tail recursive implementation of list reversal. 90
7.16 List building by prepending and reversing instead of appending. 91
7.17 Insertion sort. 92
7.18 Merge-sort; fixed from Lis. 6.5; see also Lis. 7.19. 93
7.19 Alternative implementation of merge-sort; see also Lis. 7.18. 93
7.20 Quick-sort with user-defined splitting; see also Lis. 10.1. 94
7.21 Tail recursive implementation of take; contrast with Lis. 7.8. 94
7.22 Buffer-based implementation of take; contrast with Lis. 7.21. 95
7.23 Loop-based implementation of take; contrast with Lis. 7.22. 95
8.6 Set conversion to balanced binary search trees, improved in Lis. 8.12. 107
8.7 Left and right rotations on binary search trees. 109
8.8 Rebalancing of binary search trees by rotations. 110
8.9 Key insertion in self-balancing binary search trees. 111
8.10 Key deletion in self-balancing binary search trees; see also Lis. 8.11. 111
8.11 Key deletion in self-balancing binary search trees; see also Lis. 8.10. 112
8.12 Conversion from set to binary search trees, improved from Lis. 8.6. 112
12.11 All the solutions to subset-sum; contrast with Lis. 12.10 and 12.12. 192
12.12 The solutions to subset-sum derived lazily; contrast with Lis. 12.11. 192
13.1 Pipeline example with no handling of failures; contrast with Lis. 13.2. 201
13.2 Pipeline example with failure handling; contrast with Lis. 13.1. 202
20.1 Thread-safe queue (single public lock); see also Lis. 20.5 and 20.7. 298
20.2 Batch insertions by client-side locking on a concurrent queue. 299
20.3 Batch extraction by client-side locking on a concurrent queue. 300
20.4 Batch extraction without synchronization; contrast with Lis. 20.3. 300
20.5 Thread-safe queue (single private lock); see also Lis. 20.1 and 20.7. 301
20.6 Batch processing from queue with a private lock. 302
20.7 Thread-safe queue (two private locks); see also Lis. 20.1 and 20.5. 303
20.8 Batch processing from a queue with a split lock. 304
21.1 Example concurrency from a thread pool; contrast with Lis. 17.1. 307
21.2 Using a thread pool to create threads and wait for their termination. 309
21.3 A server that processes requests sequentially; contrast with Lis. 21.4. 310
21.4 A server that processes requests concurrently; contrast with Lis. 21.3. 310
21.5 Example of scheduled execution using a timer pool. 313
21.6 Example of parallel evaluation of higher-order function map. 316
22.1 Possible deadlock of the incorrect box implementation in Lis. 22.3. 327
22.2 Single-threaded executions are sequentially consistent. 330
22.3 Multithreaded executions are not sequentially consistent. 331
xxi
xxii List of Figures
27.1 Possible loss of value from concurrent calls to push on a stack. 403
27.2 Compare-and-set to push/pop a lock-free stack. 404
27.3 Message flow of actors in Lis. 27.7. 409
Foreword by Cay Horstmann
In my book Scala for the Impatient, I provide a rapid-fire introduction into the many
features of the Scala language and API. If you need to know how a particular feature
works, you will find a concise explanation and a minimal code example (with real code,
not fruits or animals). I assume that the reader is familiar with Java or a similar object-
oriented programming language and organize the material to maximize the experience
and intuition of such readers. In fact, I wrote the book because I was put off by the
learning materials at the time, which were disdainful of object-oriented programming
and biased toward functional programming as the superior paradigm.
That was more than a decade ago. Nowadays, functional techniques have become
much more mainstream, and it is widely recognized that the object-oriented and func-
tional paradigms complement each other. In this book, Michel Charpentier provides
an accessible introduction to functional and concurrent programming. Unlike my Scala
book, the material here is organized around concepts and techniques rather than lan-
guage features. Those concepts are developed quite a bit more deeply than they would
be in a book that is focused on a programming language. You will learn about nontrivial
and elegant techniques such as zippers and trampolines.
This book uses Scala 3 for most of its examples, which is a great choice. The concise
and elegant Scala syntax makes the concepts stand out without being obscured by a
thicket of notation. You will particularly notice that when the same concept is expressed
in Scala and in Java. You don’t need to know any Scala to get started, and only a modest
part of Scala is used in the code examples. Again, the focus of the book is concepts,
not programming language minutiae. Mastering these concepts will make you a better
programmer in any language, even if you never end up using Scala in your career.
I encourage you to actively work with the sample programs. Execute them, observe
their behavior, and experiment by making changes. I suggest that you use a program-
ming environment that supports Scala worksheets, such as Visual Studio Code, IntelliJ,
or the online Scastie service. With a worksheet, turnaround is quick and exploratory
programming is enjoyable.
Seven out of the 28 chapters are complete case studies that illustrate the material
that preceded them. They are chosen to be interesting without being overwhelming. I
am sure you will profit from working through them in detail.
The book is divided into two parts. The first part covers functional programming
with immutable data, algebraic data types, recursion, higher-order functions, and lazy
evaluation. Even if you are at first unexcited about reimplementing lists and trees, give
it a chance. Observe the contrast with traditional mutable data structures, and you
will find the journey rewarding. The book is blessedly free of complex category theory
that in my opinion—evidently shared by the author—requires a large amount of jargon
before yielding paltry gains.
xxiii
xxiv Foreword by Cay Horstmann
The focus of the second part is concurrent programming. Here too the organization
along concepts rather than language and API features is refreshing. Concurrent pro-
gramming is a complex subject with many distinct use cases and no obvious way of
teaching it well. Michel has broken down the material into an interesting and thought-
provoking sequence of chapters that is quite different from what you may have seen
before. As with the first part, the ultimate aim is not to teach you a specific set of skills
and techniques, but to make you think at a higher level about program design.
I enjoyed reading and working through this unique book and very much hope that
you will too.
Cay Horstmann
Berlin, 2022
Random documents with unrelated
content Scribd suggests to you:
»Mitä nyt?» kummasteli hän. »Eivätkö kuninkaan käskyläiset
kunnioita hurskautta eivätkä ikää, koska tunkeutuvat pyhän miehen
yksinäiseen asuntoon edes lupaa kysymättä?»
Siten alkoi ystävyys, jota kesti niin kauan kuin kelpo pappi eli.
Milloin Norman of Tornin vain sopi, kävi hän katsomassa ystäväänsä
isä Claudea. Juuri tämä se opetti pojan lukemaan ja kirjoittamaan
ranskan-, englannin- ja latinankieltä sellaisena aikana, jolloin
ainoastaan harvat ylimykset osasivat kirjoittaa oman nimensä.
»Jos hän on Musta Susi», supatti isä Claude pojalle, »ei meille olisi
voinut pahemmin käydä, sillä hän ryöstää aina kirkonmiehiä ja
humalassa ollessaan, kuten hän nyt on, murhaa uhrinsa. Minä
heittäydyn heidän eteensä, ja te kiiruhdatte sillä aikaa, ratsunne
luokse ja pakenette.» Hän käytti ranskankieltä ja piti käsiään
rukousasennossa, joten hän johti täydelleen harhaan rosvot, jotka
eivät lainkaan aavistaneet hänen puhuvan pojalle.
Kun poika näki tämän joukon roistoja, jotka olivat liittyneet yhteen
ryöstääkseen papistolta, heräsi pojan mielessä ajatus, joka oli
utuisen hämäränä pyörinyt hänen piilotajuntansa sisimmissä
sopukoissa aina siitä alkaen, kun hän voittamalla kolme ritaria oli niin
helposti saanut niin runsaan saaliin, hevosia, aseita, varustuksia ja
kultaa. Kuten aina hänen myöhemmän elämänsä aikana merkitsi
nytkin ajatus toimimista.
*****
»En tiedä, isä», vastasi Norman of Torn, »jollei syynä ole se, että
kysyn itseltäni: 'Mitä varten kaikki tämä?' Minkä tähden isäni opetti
minut aina ryöstämään lähimmäisiltäni? Pidän taistelusta, mutta
taistella saa yllin kyllin laillisestikin, ja mitä hyötyä minulla on kaikista
ryöstetyistä varoistani, jollen saa mennä ihmisten asumasijoille niitä
kuluttamaan? Jos pistäisin pääni Lontoon kaupunkiin, jäisi se
epäilemättä sinne hamppuisen kaulanauhan pidättämällä.
»Voin vain arvata, Norman of Torn, koska olen nähnyt erään toisen
henkilön, jota sinä muistutat.»
Vasta puhuessaan hän tarkasti nuorta naista. Että hän oli ylhäinen
nainen, sitä eivät osoittaneet ainoastaan hänen ratsastusasunsa ja
hevosensa hihnoituksen upeus, vaan myöskin hänen ylimyksellinen
ja kopea esiintymisensä ja kauniiden kasvojensa ylpeä ilme.