Beginning Functional JavaScript. Apress

Functional Programming with JavaScript Using EcmaScript 6

Anto Aravint

Chapter 1: Functional Programming in Simple Terms
Chapter 2: Fundamentals of JavaScript Functions
Chapter 3: Higher-Order Functions
Chapter 4: Closures and Higher-Order Functions
Chapter 5: Being Functional on Arrays
Chapter 6: Currying and Partial Application
Chapter 7: Composition and Pipelines
Chapter 8: Fun with Functors
Chapter 9: Monads in Depth
Chapter 10: Pause, Resume with Generators

e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 2.00 USD
 Pages
 172 p
 File Size
 3,232 KB
 File Type
 PDF format
 ISBN-13 (pbk)
 ISBN-13 (electronic) 
 978-1-4842-2655-1
 978-1-4842-2656-8
 Copyright   
 2017 by Anto Aravinth 

About the Author
Anto Aravinth is a Senior Business Intelligence Developer at VisualBI, Chennai. He
has been busy developing web applications using Java; JavaScript; and frameworks like
ReactJs, Angular, etc., for the last five years. He has a solid understanding of the Web and
its standards. Anto is also an open source contributor to popular frameworks such as
ReactJs, Selenium, and Groovy.

Anto loves playing table tennis in his free time. He has a great sense of humor,
too! Anto is also a Technical Development Editor for React Quickly, a book that will be
published by Manning in 2017.

Acknowledgments
Writing a book is not easy as I would have thought. It’s almost very like making a movie.
You need to go to each publication unit with your table of contents. The table of contents
are like your movie script. It needs to have a strong beginning, keep the audience
engaged, and then have a good ending. The screenplay should be very good which is
achieved via your text. The process of a book starts when an editorial team accepts your
table of contents. I want to thank Pramila for that; she was helpful in the initial stages of
the book. Of course, writing a technical book needs to be technically correct as well. And
that’s where I need to give a special thanks to our technical editorial team! They were
very good at catching any technical issues in my writing. I want to give special thanks to
Anila for working through the chapters and finding any grammatical errors – and making
sure the contents are good enough for the readers to be engaged. All these phases are
managed by our manager named Prachi. Thanks, Prachi, for making it happen!
I want to dedicate this book to my Lord Jesus, late father Belgin Rayen, and my
beloved mother named Susila. I also want to give thanks to Kishore, my brother-in-law,
for supporting me throughout my life and career. I never told my sister Ramya (one and
only sibling) that I’m authoring a book. I just couldn’t predict how she would react to this
event. Special thanks to her as well.
Special thanks to all my friends & colleagues who have been very supportive in my
career: Deepak, Vishal, Shiva, Mustafa, Anand, Ram (Juspay), Vimal (Juspay), Lalitha,
Swetha, Vishwapriya. Final thanks to my close cousins: Bianca, Jennifer, Amara, Arun,
Clinton, Shiny, Sanju.
There can be improvements made in my style of writing, content, authoring, etc.
If you want to share your thoughts, please contact me at antoaravinthrayen@gmail.com.
I’m also available on twitter @antoaravinth.
Thanks for purchasing this book! I hope you will enjoy it. Good luck!
Anto Aravinth, India

Table of Contents
About the Author ............................................................................ xiii
Acknowledgments ........................................................................... xv
■■Chapter 1: Functional Programming in Simple Terms .................... 1
What Is Functional Programming? Why It Matters? ................................ 1
Referential Transparency ......................................................................... 4
Imperative, Declarative, Abstraction ........................................................ 5
Functional Programming Benefits ........................................................... 6
Pure Functions ........................................................................................ 6
Pure Functions Lead to Testable Code ......................................................................6
Reasonable Code ......................................................................................................8
Parallel Code ........................................................................................... 9
Cachable ............................................................................................... 10
Pipelines and Composable .................................................................... 11
Pure Function Is a Mathematical Function ............................................ 11
What We Are Going to Build ................................................................... 12
Is JavaScript a Functional Programming Language? ............................ 13
Summary ............................................................................................... 13
■■Chapter 2: Fundamentals of JavaScript Functions ....................... 15
ECMAScript A Bit of History ................................................................... 16
Creating and Executing Functions ......................................................... 16
First Function ..........................................................................................................17
Strict Mode .............................................................................................................18
Return Statement Is Optional .................................................................................19
Multiple Statement Functions .................................................................................20
Function Arguments ...............................................................................................21
ES5 Functions Are Valid in ES6 ...............................................................................21
Setting Up Our Project ........................................................................... 21
Initial Setup ............................................................................................................22
Our First Functional Approach to the Loop Problem ...............................................23
Gist on Exports ........................................................................................................25
Gist on Imports .......................................................................................................25
Running the Code Using Babel-Node ......................................................................26
Creating Script in Npm ...........................................................................................26
Running the Source Code from Git .........................................................................27
Summary ............................................................................................... 28
■■Chapter 3: Higher-Order Functions ............................................... 29
Understanding Data ............................................................................... 30
Understanding JavaScript Data Types ....................................................................30
Storing a Function ..................................................................................................30
Passing a Function .................................................................................................31
Returning a Function ..............................................................................................32
Abstraction and Higher-Order Functions ............................................... 33
Abstraction Definitions ...........................................................................................33
Abstraction via Higher-Order Functions ..................................................................34
Higher-Order Functions in the Real World ............................................. 37
every Function ........................................................................................................37
some Function ........................................................................................................38
sort Function ...........................................................................................................39
Summary ............................................................................................... 43
■■Chapter 4: Closures and Higher-Order Functions ......................... 45
Understanding Closures ........................................................................ 46
What Are Closures? ................................................................................................46
Remembering Where It Is Born ...............................................................................47
Revisiting sortBy Function ......................................................................................49
Higher-Order Functions in the Real World (Continued) .......................... 50
tap Function ............................................................................................................50
unary Function ........................................................................................................51
once Function .........................................................................................................52
Memoize Function ..................................................................................................53
Summary ............................................................................................... 55
■■Chapter 5: Being Functional on Arrays ......................................... 57
Working Functionally on Arrays ............................................................. 58
map ........................................................................................................................58
filter ........................................................................................................................61
Chaining Operations .............................................................................. 62
concatAll .................................................................................................................63
Reducing Function ................................................................................ 66
reduce Function ......................................................................................................67
Zipping Arrays ....................................................................................... 72
zip Function ............................................................................................................73
Summary ............................................................................................... 75
■■Chapter 6: Currying and Partial Application ................................. 77
A Few Terminologies ............................................................................. 77
unary Function ........................................................................................................78
Binary Function ......................................................................................................78
variadic Functions ..................................................................................................78
Currying ................................................................................................. 79
Currying Use Cases ................................................................................................81
A logger Function - Using Currying ........................................................................82
Revisit Curry ...........................................................................................................83
Back to logger Function ..........................................................................................86
Currying in Action .................................................................................. 87
Finding number in Array Contents ..........................................................................87
squaring an Array ...................................................................................................88
Data Flow .............................................................................................. 88
Partial Application ...................................................................................................89
Implementing partial Function ................................................................................89
Currying vs. Partial Application ..............................................................................92
Summary ............................................................................................... 92
■■Chapter 7: Composition and Pipelines .......................................... 93
Composition in General Terms ............................................................... 93
Unix Philosophy ......................................................................................................94
Functional Composition ......................................................................... 96
Revisiting map,filter ...............................................................................................96
compose Function ..................................................................................................97
Playing with compose function ............................................................. 98
curry and partial to the Rescue ..............................................................................99
compose many function .......................................................................................102
Pipelines / Sequence ........................................................................... 103
Implementing pipe ................................................................................................104
Odds on Composition ............................................................................................104
Debugging Using tap Function .............................................................................105
Summary ............................................................................................. 106
■■Chapter 8: Fun with Functors ..................................................... 107
What Is a Functor? .............................................................................. 107
Functor Is a Container ..........................................................................................108
Functor Implements Method Called map ..............................................................109
MayBe ................................................................................................. 111
Implementing MayBe ............................................................................................111
Simple Use Cases .................................................................................................112
Real-World Use Cases ..........................................................................................114
Either Functor ...................................................................................... 118
Implementing Either .............................................................................................118
Reddit Example Either Version ..............................................................................120
Word of Caution - Pointed Functor ...................................................... 123
Summary ............................................................................................. 123
■■Chapter 9: Monads in Depth ....................................................... 125
Getting Reddit Comments for Our Search Query ................................. 125
The Problem ........................................................................................ 126
Implementation of the First Step ..........................................................................128
Problem of So Many maps ....................................................................................134
Solving the Problem via join ................................................................ 135
join Implementation ..............................................................................................135
chain Implementation ...........................................................................................137
Summary ............................................................................................. 140
■■Chapter 10: Pause, Resume with Generators ............................. 141
Async Code and Its Problem ................................................................ 142
Callback Hell .........................................................................................................142
Generators 101 .................................................................................... 144
Creating Generators ..............................................................................................144
Caveats of Generators ..........................................................................................145
yield New Keyword ...............................................................................................146
done Property of Generator ..................................................................................148
Passing Data to Generators ..................................................................................149
Using Generators to Handle Async Calls ............................................. 151
Generators for Async - A Simple Case ..................................................................151
Generators for Async - A Real-World Case ...........................................................156
Summary ............................................................................................. 158
Appendix A .................................................................................... 159
How to Install Node In Your System .................................................... 159
Installing Depedencies ........................................................................ 159
Index .............................................................................................. 161

Bookscreen
e-books shop

Functional Programming in Simple Terms 
The first rule of functions is that they should be small. The second rule of
functions is that they should be smaller than that.
—ROBERT C. MARTIN
Loading...
DMCA.com Protection Status