Professional Documents
Culture Documents
Functional Programming in Javascript How To Improve Your Javascript Programs Using Functional Techniques First Edition Luis Atencio
Functional Programming in Javascript How To Improve Your Javascript Programs Using Functional Techniques First Edition Luis Atencio
https://textbookfull.com/product/functional-programming-in-
javascript-how-to-improve-your-javascript-programs-using-
functional-techniques-1-edition-edition-luis-atencio/
https://textbookfull.com/product/beginning-functional-javascript-
functional-programming-with-javascript-using-ecmascript-6-1st-
edition-anto-aravinth/
https://textbookfull.com/product/beginning-functional-javascript-
functional-programming-with-javascript-using-ecmascript-6-1st-
edition-anto-aravinth-2/
https://textbookfull.com/product/the-joy-of-javascript-1st-
edition-luis-atencio/
Beginning Functional JavaScript: Uncover the Concepts
of Functional Programming with EcmaScript 8 2nd Edition
Srikanth Machiraju
https://textbookfull.com/product/beginning-functional-javascript-
uncover-the-concepts-of-functional-programming-with-
ecmascript-8-2nd-edition-srikanth-machiraju/
https://textbookfull.com/product/the-joy-of-javascript-1st-
edition-atencio/
https://textbookfull.com/product/head-first-javascript-
programming-2nd-edition-a-learners-guide-to-modern-javascript-
eric-freeman/
https://textbookfull.com/product/head-first-javascript-
programming-2nd-edition-a-learners-guide-to-modern-javascript-
eric-freeman-2/
Functional Programming in JavaScript
Functional
Programming
in JavaScript
LUIS ATENCIO
MANNING
SHELTER ISLAND
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: orders@manning.com
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine.
ISBN: 9781617292828
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – EBM – 21 20 19 18 17 16
To my wonderful wife, Ana.
Thank you for your unconditional support
and for being the source of passion and inspiration in my life.
brief contents
PART 1 THINK FUNCTIONALLY ..................................................1
1 ■ Becoming functional 3
2 ■ Higher-order JavaScript 23
vii
contents
preface xv
acknowledgments xvii
about this book xix
1 Becoming functional
1.1
3
Can functional programming help? 5
1.2 What is functional programming? 5
Functional programming is declarative 7 Pure functions and
■
ix
x CONTENTS
2 Higher-order JavaScript 23
2.1 Why JavaScript? 24
2.2 Functional vs. object-oriented programming 24
Managing the state of JavaScript objects 31 Treating objects ■
of closures 50
2.5 Summary 53
programming 111
4.6 Managing control flow with functional combinators 112
Identity (I-combinator) 112 Tap (K-combinator) 113
■
null-checking 121
5.2 Building a better solution: functors 121
Wrapping unsafe values 122 ■ Functors explained 124
5.3 Functional error handling using monads 127
Monads: from control flow to data flow 128 Error handling with
■
dependencies 164
6.4 Capturing specifications with property-based testing 166
6.5 Measuring effectiveness through code coverage 172
Measuring the effectiveness of testing functional code 173
Measuring the complexity of functional code 177
6.6 Summary 179
xv
What is functional programming? 5
and use it to solve complex JavaScript tasks. But before we can get into all this, you
need to learn why thinking functionally is important and how it can help you tackle
the complexities of JavaScript programs.
and reduce mutation of state in your application. I know this sounds like a mouthful, but
I’ll visit each of these terms further and build on them throughout the book.
Normally, FP books start with computing Fibonacci numbers, but I’d rather start
with a simple JavaScript program that displays text on an HTML page. What better text
to print than the good ol’ “Hello World”:
This program is simple, but because everything is hardcoded, you can’t use it to dis-
play messages dynamically. Say you wanted to change the formatting, the content, or
perhaps the target element; you’d need to rewrite this entire expression. Maybe you
decide to wrap this code with a function and make the change points parameters, so
you can write it once and use it with any configuration:
function printMessage(elementId, format, message) {
document.querySelector(`#${elementId}`).innerHTML =
`<${format}>${message}</${format}>`;
}
An improvement, indeed, but still not a completely reusable piece of code. Suppose
you want to write to a file instead of an HTML page. You need to take the simple
thought process of creating parameterized functions to a different level, where param-
eters aren’t just scalar values but can also be functions themselves that provide addi-
tional functionality. Functional programming is a bit like using functions on steroids,
because your sole objective is to evaluate and combine lots of functions with others to
achieve greater behavior. I’ll fast-forward a bit and show you a sneak peek at this same
program using a functional approach.
printMessage('Hello World');
Without a doubt, this looks radically different than the original. For starters, h1 isn’t a
scalar anymore; it’s a function just like addToDom and echo. Visually, it feels as though
you’re creating a function from smaller functions.
There’s a reason for this madness. Listing 1.1 captures the process of decomposing
a program into smaller pieces that are more reusable, more reliable, and easier to
Another random document with
no related content on Scribd:
Jänöjussi meni lähempää katsomaan, kurkistipa ikkunastakin.
Ja kun saatiin tietää sekin, että kissat aikoivat ajaa heitä vastaan
komeasti kärryillä, päättivät he ottaa auton, että näyttäisi
komeammalta.
Mentiin paikalle, mutta voi ilkeys! kirkosta ei ollut muuta jälellä kuin
pahainen hyhmätönkkö ja sekin romahti näkymättömiin ja suli
auringon paisteessa vedeksi.
— No karhupa tietenkin.
— Ei se nouse sinultakaan!
— Kontio kuninkaaksi!
Nallen jouluyö.
Ka, mikäs sieltä nyt tuli kelkkansa kanssa? Ihminen taisi olla, mikä
lienee partaniekka. Heh, eiköhän liene ihan vanha joulu-ukko
liikkeellä. Olipas vain!
Siinähän Riitta jo toikin ruuan tähteitä. Kun nyt menisi vain pois.
Kas, eikös ollut leipäkorissa tuoretta leipääkin, ja miten herkullisen
näköistä oli liha paistiastiassa! Ei muuta kuin käsiksi vain!
Mutta voi ilkeys! Riitta oli tullut niin hiljaa ruokakonttoriin, ettei
kukaan heistä huomannut, ennenkuin tämä löi käsiään yhteen ja
hihkasi.: