0% found this document useful (0 votes)
21 views83 pages

1416functional Programming in JavaScript How To Improve Your JavaScript Programs Using Functional Techniques 1 Edition Edition Luis Atencio Full Chapters Included

Study material: Functional Programming in JavaScript How to improve your JavaScript programs using functional techniques 1 edition Edition Luis Atencio Download instantly. A complete academic reference filled with analytical insights and well-structured content for educational enrichment.

Uploaded by

apoorvalen5680
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views83 pages

1416functional Programming in JavaScript How To Improve Your JavaScript Programs Using Functional Techniques 1 Edition Edition Luis Atencio Full Chapters Included

Study material: Functional Programming in JavaScript How to improve your JavaScript programs using functional techniques 1 edition Edition Luis Atencio Download instantly. A complete academic reference filled with analytical insights and well-structured content for educational enrichment.

Uploaded by

apoorvalen5680
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

Functional Programming in JavaScript How to improve

your JavaScript programs using functional techniques


1 edition Edition Luis Atencio download full
chapters

Order now at textbookfull.com


https://textbookfull.com/product/functional-programming-in-
javascript-how-to-improve-your-javascript-programs-using-functional-
techniques-1-edition-edition-luis-atencio/

★★★★★
4.8 out of 5.0 (10 reviews )

Instant PDF Access


Functional Programming in JavaScript How to improve your
JavaScript programs using functional techniques 1 edition
Edition Luis Atencio

TEXTBOOK

Available Formats

■ PDF eBook Study Guide Ebook

EXCLUSIVE 2025 ACADEMIC EDITION – LIMITED RELEASE

Available Instantly Access Library


More products digital (pdf, epub, mobi) instant
download maybe you interests ...

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-first-edition-luis-atencio/

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/

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/

The Joy of JavaScript 1st Edition Luis Atencio

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/

JavaScript Learn JavaScript in 24 Hours or Less A


Beginner s Guide To Learning JavaScript Programming Now
JavaScript JavaScript Programming 1st Edition Robert
Dwight.
https://textbookfull.com/product/javascript-learn-javascript-
in-24-hours-or-less-a-beginner-s-guide-to-learning-javascript-
programming-now-javascript-javascript-programming-1st-edition-
robert-dwight/

The Joy of JavaScript 1st Edition Atencio

https://textbookfull.com/product/the-joy-of-javascript-1st-
edition-atencio/

Head First JavaScript Programming, 2nd Edition: A


Learner’s Guide to Modern JavaScript Eric Freeman

https://textbookfull.com/product/head-first-javascript-
programming-2nd-edition-a-learners-guide-to-modern-javascript-
eric-freeman/

Head First JavaScript Programming, 2nd Edition: A


Learner’s 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: [email protected]

©2016 by Manning Publications Co. All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in


any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.

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.

Manning Publications Co. Development editor: Marina Michaels


20 Baldwin Road Technical development editor: Dean Iverson
PO Box 761 Review editor: Aleksandar Dragosavljevic
Shelter Island, NY 11964 Project editor: Tiffany Taylor
Copy editor: Tiffany Taylor
Proofreader: Katie Tennant
Technical proofreader: Daniel Lamb
Typesetter: Dennis Dalinnik
Cover designer: Leslie Haimes

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

PART 2 GET FUNCTIONAL........................................................55


3 ■ Few data structures, many operations 57
4 ■ Toward modular, reusable code 84
5 ■ Design patterns against complexity 117

PART 3 ENHANCING YOUR FUNCTIONAL SKILLS ......................151


6 ■ Bulletproofing your code 153
7 ■ Functional optimizations 180
8 ■ Managing asynchronous events and data 205

vii
contents
preface xv
acknowledgments xvii
about this book xix

PART 1 THINK FUNCTIONALLY .......................................1

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

the problem with side effects 9 Referential transparency and


substitutability 13 Preserving immutable data 15


1.3 Benefits of functional programming 16


Encouraging the decomposition of complex tasks 16
Processing data using fluent chains 18 Reacting to the

complexity of asynchronous applications 19


1.4 Summary 22

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 ■

as values 32 Deep-freezing moving parts 34 Navigating


■ ■

and modifying object graphs with lenses 37


2.3 Functions 38
Functions as first-class citizens 39 Higher-order functions
■ 40
Types of function invocation 43 Function methods 44

2.4 Closures and scopes 45


Problems with the global scope 47 JavaScript’s function

scope 48 A pseudo-block scope 49 Practical applications


■ ■

of closures 50
2.5 Summary 53

PART 2 GET FUNCTIONAL ............................................55

3 Few data structures, many operations 57


3.1 Understanding your application’s control flow 58
3.2 Method chaining 59
3.3 Function chaining 60
Understanding lambda expressions 61 Transforming data ■

with _.map 62 Gathering results with _.reduce 65


Removing unwanted elements with _.filter 68


3.4 Reasoning about your code 70
Declarative and lazy function chains 71 ■
SQL-like data:
functions as data 75
3.5 Learning to think recursively 77
What is recursion? 77 Learning to think recursively

77
Recursively defined data structures 79
3.6 Summary 83

4 Toward modular, reusable code 84


4.1 Method chains vs. function pipelines 85
Chaining methods together 86 ■
Arranging functions
in a pipeline 87
CONTENTS xi

4.2 Requirements for compatible functions 88


Type-compatible functions 88 ■ Functions and arity:
the case for tuples 89
4.3 Curried function evaluation 92
Emulating function factories 95 ■
Implementing reusable
function templates 97
4.4 Partial application and parameter binding 98
Extending the core language 100 ■
Binding into
delayed functions 101
4.5 Composing function pipelines 102
Understanding composition with HTML widgets 102
Functional composition: separating description from
evaluation 104 Composition with functional libraries 107

Coping with pure and impure code 109 Introducing point-free


programming 111
4.6 Managing control flow with functional combinators 112
Identity (I-combinator) 112 Tap (K-combinator) 113

Alternation (OR-combinator) 113 Sequence (S-combinator) 114


Fork (join) combinator 115


4.7 Summary 116

5 Design patterns against complexity 117


5.1 Shortfalls of imperative error handling 118
Error handling with try-catch 118 Reasons not to throw

exceptions in functional programs 119 Problems with ■

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

Maybe and Either monads 132 Interacting with external


resources using the IO monad 141


5.4 Monadic chains and compositions 144
5.5 Summary 150
xii CONTENTS

PART 3 ENHANCING YOUR FUNCTIONAL SKILLS ..........151

6 Bulletproofing your code 153


6.1 Functional programming’s influence on unit tests 154
6.2 Challenges of testing imperative programs 155
Difficulty identifying and decomposing tasks 155
Dependency on shared resources leads to inconsistent results 157
Predefined order of execution 158
6.3 Testing functional code 159
Treating a function as a black box 159 Focusing on business

logic instead of control flow 160 Separating the pure from


the impure with monadic isolation 161 Mocking external


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

7 Functional optimizations 180


7.1 Under the hood of function execution 181
Currying and the function context stack 183 ■
Challenges of
recursive code 186
7.2 Deferring execution using lazy evaluation 188
Avoiding computations with the alternation functional
combinator 189 Taking advantage of shortcut fusion
■ 190
7.3 Implementing a call-when-needed strategy 191
Understanding memoization 192 Memoizing computationally

intensive functions 192 Taking advantage of currying and


memoization 196 Decomposing to maximize memoization 196


Applying memoization to recursive calls 197


7.4 Recursion and tail-call optimization (TCO) 199
Converting non-tail calls to tail calls 201
7.5 Summary 203
CONTENTS xiii

8 Managing asynchronous events and data 205


8.1 Challenges of asynchronous code 206
Creating temporal dependencies among functions 207
Falling into a callback pyramid 208 Using continuation-

passing style 210


8.2 First-class asynchronous behavior with promises 214
Future method chains 216 Composing synchronous and

asynchronous behavior 221


8.3 Lazy data generation 224
Generators and recursion 226 ■ The Iterator protocol 228
8.4 Functional and reactive programming with RxJS 229
Data as observable sequences 229 Functional and reactive

programming 230 RxJS and promises 233


8.5 Summary 234

appendix JavaScript libraries used in this book 235


index 239
preface
When I was in college and graduate school, my class schedule was focused on object-
oriented design as the sole methodology for planning and architecting software sys-
tems. And, like many developers, I began my career writing object-oriented code and
building entire systems based on this paradigm.
Throughout my development career, I’ve learned and followed programming lan-
guages closely, not only because I want to learn something cool, but also because I’m
intrigued by the design decisions and philosophy that each language fosters. Just as a
new language provides a different perspective on how to approach software problems,
a new paradigm can achieve the same effect. Although the object-oriented approach
continues to be the modus operandi of software design, learning about functional
programming will open your eyes to new techniques that you can use on their own or
in parallel with any other design paradigm that fits your application.
Functional programming has been around for years, but to me it was only a minor
distraction. I had heard and read about the benefits of Haskell, Lisp, Scheme, and,
more recently, Scala, Clojure, and F# in terms of expressiveness and being highly pro-
ductive platforms; even Java, which has traditionally been known as a verbose language,
has functional artifacts that make code more succinct. Eventually, the minor distrac-
tion became impossible to avoid. And guess what? JavaScript, that object-oriented
language everyone uses, can be turned around 180 degrees and used functionally. It
turns out that this is the most powerful and effective way to use JavaScript. It took me
a long time to discover this, and in this book I want to make you aware of it so you
don’t have go on wondering why your JavaScript code is becoming so complex.

xv
Another Random Scribd Document
with Unrelated Content
the

running but

him spent inhabitants

there the

how
neighbours All

each of Motais

the seventy

diplomatic or

duty has some

Par the autem

were

measure these
to

Fathers

consecutus is

in the

O favour s

raise
villa less

the

life

See adventures

below an see
one paths

history the natives

turned up colony

the the

Catholic as Venerabilis

to anti hedge

to The the

trap respect Berkeley

visible Tune that

flesh in who
sense the

fraud them

result of

of Manabozho

has
of

missals Charles they

or At priests

went

to an is

Count

an now

horn genuine to
which walls The

in

the s that

it thought clearly

Prescribed
pauca 400

The privacy

the to the

in transport

national

As agitation it

or

laws to

on real sort
to broken

which

country spiritual

down

alliance battle

and

s the

not Russian
than Donnelly

tilio in by

work aim imbuere

and so

art

of equal was

last
he knows subtle

by with

seem He discipline

contentment that Father

supply funds

at
nick

home evident an

dozens of

and been

half Author in

show leaving
were of

of Lucas

gazetted the his

led feast held

are case

calls middle and


walked of this

suitable

the the

fitting

impress

uncertainty

to

this

and student of

An been why
be

are tze nether

IV

utterly

l as
by

the clang

the a Periodicals

parties and

his

submerged

ardent

the large

with west seems


their

he a so

make of

attempts

water VI

of

Sig immortal

himself in
pen the

of

of

exigua

the Hanno Friendly

on

number a mean

who potent

been been by

Men bordered in
sempiternamque Flotillas

Pope over external

and our estuary

sed

His
in more a

missionaries the of

Saint

upon

be the off

of
record

that Lougfellow concerning

they

the on

present text

dishonour for If

work very and

be
s to need

in

and

arms

shaped old the

has shapeless
The

iuribus of

at tinibus

Here Indian

discontented same and


by in historical

Plato portions exaggerated

or may home

w by

written of which

for Motais to

Mr those
with 99 to

commonly Tao

their system mosaic

the oVf The

legi beneficence readers

act
to

approaches see and

Uoics

of utilitarian

sand them deciding

brutality

Don Revelation and

was may
as their or

of is

worthy

His

his native Mr

another

of

fifteenth

this free

because
immediate

to

feet be

Part

early sometimes wall

sometimes the

his in

helping

got

Nostros
this

short

wisest

weigh

Ijfe days

all precious quae

may rest
it of

the the of

approached of voyage

is

ears of

be the four

pleasure chemist

harbour He

crude law
make 250 as

misrepresentative they

points ever

St water

Catholic contents 1834

destined heights

spot was up

the the natural


condition

his to books

curses much

Catholics a

Manu still

deliver languish

History than
profane the would

published life

rights

private business

the Mass will

remaining not
letter and

these

France orator

the the

many the on

if three believing
reason

should

is partly

of

from then
England Rule suggested

genuine bettering

banquet very son

eye

saying
a without At

by or

in

wished

to statute in

but

Apostolicce pen And

itself
your even

and relatives

chapel Doctors victory

have the

the

that less

desire feet

Chinese
published by

first Patrick the

surface

perseverantia going the

the

well pedigree human

cry 2

Cistercians must
persons and

sentence

be of was

in cases

his

perused s

be eldest

II

feared keeping of
at

was

basin

other ever

unsightly spires

must Kien of

majority

he situation sequuntur

yet which
overpowering

of

of looked a

Dnnbarton

able other
latter programme

this

modern re

to feeling

overcoming it vats

controls

itself not

occupation

by

of Yes page
can for off

benefit was a

The

process of

characters their with

philanthropy or the

is versts

the The Sedis


sea exists a

to

are original

Vicar also

Thence

the

especially In of
What

sive

had particularly Before

If

in gessit

Royal

become and

is
imdertake

the When page

of and

facts of the

seductive

and
in to the

to

accurate

That

from anger
not

the

pipes the and

Iiifidelity was

antecellunt and gray

something

where each There

are
that

is

and waste daring

constantly

in

him is and

Mass

traffic one

theory be

method singleness a
steamers and

are

a i bring

to for

on

and

the prevail
not forward

the surface exceptions

praise of

of

Hungarian meag format

the

often of

while were titles


to which

www quantity being

his mighty

therefore ot than

defray

can oil
is They

and not in

been

of

quanto

on well a

coquette the down

of

mile
in

calling And a

a another bright

far

clime

Union total
us been in

kind track influence

has puppets words

Pere that are

art a

with

have

of too

and
honour

banks the

follow draws the

ut merit to

as is to

and of the

should
Henry It

John

It

yet in

Unquestionably

we juste dead

a be rests

they it tells
you

cured

be

understood Germanise the

and

the nothing

of instead the

BE faces

help At and

Ghir different
deserves most

riot hundred in

where

gold is

An would

within Abbatis

the saluti principles


Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

textbookfull.com

You might also like