Functional Programming in JavaScript How to improve your JavaScript programs using functional techniques 1st Edition Luis Atenciopdf download
Functional Programming in JavaScript How to improve your JavaScript programs using functional techniques 1st Edition Luis Atenciopdf download
https://ebookmeta.com/product/functional-programming-in-
javascript-how-to-improve-your-javascript-programs-using-
functional-techniques-1st-edition-luis-atencio/
https://ebookmeta.com/product/mastering-javascript-functional-
programming-1st-edition-federico-kereki/
https://ebookmeta.com/product/functional-javascript-1st-edition-
michael-fogus/
https://ebookmeta.com/product/neuroscience-for-learning-and-
development-how-to-apply-neuroscience-and-psychology-for-
improved-learning-and-training-3rd-edition-stella-collins/
ALU 101 TEXTBOOK BASIC LIFE INSURANCE UNDERWRITING
Academy Of Life Underwriting
https://ebookmeta.com/product/alu-101-textbook-basic-life-
insurance-underwriting-academy-of-life-underwriting/
https://ebookmeta.com/product/the-oxford-handbook-of-nigerian-
politics-a-carl-levan/
https://ebookmeta.com/product/bedside-manner-a-small-town-older-
man-younger-woman-instalove-medical-romance-may-december-romance-
series-1st-edition-violet-rae/
https://ebookmeta.com/product/these-vile-secrets-enclave-1-1st-
edition-ce-ricci-ricci-ce/
https://ebookmeta.com/product/the-princeton-field-guide-to-
dinosaurs-second-edition-gregory-s-paul/
Mr. Park Lane (Mister #4) 1st Edition Louise Bay
https://ebookmeta.com/product/mr-park-lane-mister-4-1st-edition-
louise-bay/
www.ebook3000.com
Functional Programming in JavaScript
www.ebook3000.com
www.ebook3000.com
Functional
Programming
in JavaScript
LUIS ATENCIO
MANNING
SHELTER ISLAND
www.ebook3000.com
For online information and ordering of this and other Manning books,
please visit
20 Baldwin Road
PO Box 761
Email: [email protected]
©2016 by Manning Publications Co. All rights reserved.
20 Baldwin Road
PO Box 761
ISBN: 9781617292828
1 2 3 4 5 6 7 8 9 10 – EBM – 21 20 19 18 17 16
www.ebook3000.com
www.ebook3000.com
www.ebook3000.com
brief contents
PART 1
Becoming functional
3
Higher-order JavaScript
23
PART 2
GET FUNCTIONAL........................................................55
57
84
117
PART 3
ENHANCING YOUR FUNCTIONAL SKILLS...................... 151
153
Functional optimizations
180
205
vii
www.ebook3000.com
www.ebook3000.com
contents
preface
xv
acknowledgments
xvii
xix
1 Becoming functional 3
1.1
1.2
substitutability
15
1.3
16
18 ■ Reacting to the
19
1.4
Summary
22
ix
www.ebook3000.com
CONTENTS
2 Higher-order JavaScript 23
2.1
Why JavaScript?
24
2.2
31 ■ Treating objects
as values
34 ■ Navigating
37
2.3
Functions
38
39 ■ Higher-order functions
40
43 ■ Function methods
44
2.4
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
3.1
58
3.2
Method chaining
59
3.3
Function chaining
60
61 ■ Transforming data
with _.map
65
68
3.4
70
71 ■ SQL-like data:
functions as data
75
3.5
77
What is recursion?
79
3.6
Summary
83
4.1
85
86 ■ Arranging functions
in a pipeline
87
CONTENTS
xi
4.2
88
Type-compatible functions
88 ■ Functions and arity:
89
4.3
92
95 ■ Implementing reusable
function templates
97
4.4
98
delayed functions
101
4.5
102
Understanding composition with HTML widgets
102
evaluation
107
programming
111
4.6
112
Identity (I-combinator)
113
Alternation (OR-combinator)
114
A SIGNIFICANT DISCOVERY
“No, Beauty, I haven’t gone back on my word. How can you harbor
such suspicions against a fine old Irish gentleman like myself? Such
a regard as I have for you, yet you will doubt me.” Leila Harper
rolled reproachfully sentimental eyes at Marjorie. “Since it is a
Beauty contest you demand, your Celtic friend will rise to the
occasion.”
“I wish you’d rise soon then.” Marjorie met Leila’s effusive promise
with a coaxing smile.
“Name the day and the hour.” Leila gave vent to a resigned groan,
quite at variance with her fulsome mood of the moment before.
“There you go. One minute you blow hot; the next cold.” Marjorie
shook an arraigning finger before Leila’s face. “I’m going to take you
at your word and name the day and hour. The day will be next
Friday. The hour, eight P.M. The place, the gym, the promoters of
the contest—” Marjorie paused with a dubious, questioning look
toward Leila.
“Aye, Beauty; there’s the rub!” Leila exclaimed. “The contest ought
to be pulled off by either the sophs or freshies. We P. G.’s are
beyond such trifling vanities. So some would be pleased to say we
should be. Now we come to the reason why of things. I’m wisely in
favor of letting the sophs perpetrate the beauty walk.”
“My own opinion,” Marjorie concurred. “How would you turn it over
to them and still manage it, Leila. I mean the details. Only you know
how to manage a Beauty contest like the one you got up long ago.”
“I’m going to be the power behind the throne and manage the
contest through the Bertram girls,” Leila made shrewd declaration.
“They are popular sophs. Besides they will do as I tell them. They’ll
not spoil my fine arrangements.” Leila favored Marjorie with a
whimsical grin. “Let me warn you, beforehand, Beauty. It will be
dangerous for you to attend the contest.”
“Your warning is wasted. I shall sit in the gallery and watch the
Beauty parade. Not because I imagine for a minute that I—that I—”
Marjorie stammered, growing suddenly rosy with confusion.
“That you would certainly win it if you appeared on the gym floor,”
Leila finished with mischievous affability. “No fair decorating the
gallery, Beauty. It’s a most important part you must play on the
floor.”
“No, designing villain. You dragged me into one Beauty contest; but
never again.” She wagged a decisive head at Leila who merely
continued to beam on her.
“This time I have a fine plan for you,” Leila continued, unabashed.
“You are to be one of the judges. I’ll paint lines of age on your lovely
face; give you a snow-white frizzy wig and a shapeless brown bag of
a gown to wear. Even your captain could not pick you out as a Dean.
Now tell me, am I not your devoted Irish friend?” she demanded
ingratiatingly.
“You’re a jewel, Leila Greatheart.” Marjorie’s face grew radiant. “The
very thing I’ll like best. I’d forgotten all about the judges. Their were
three of them at the other contest. It seems ages since that night,
doesn’t it?”
Leila nodded. “Happy ages,” she said, a soft light shining from her
bright blue eyes. “And you were not pleased with me that night,
Beauty, for putting you in your rightful place on the campus.”
“No, I wasn’t,” Marjorie replied with smiling candor. “I recall that I
was almost angry with you. I thought you did it merely to nettle the
Sans. I thought you were very clever, but I wasn’t sure whether or
not I truly liked you.”
“Ah, but I have won dozens of golden opinions from you, Beauty,
since then. I will tell you something quite remarkable about myself. I
am never disliked by a person who likes me.” Leila made the
statement with due impressiveness.
“I’ll tell you something else. You’re an affable old fake, and I’ve been
here just one-half hour longer than I intended to be.” Marjorie rose
from the chair she had been occupying in Leila’s and Vera’s room. “I
needed that half hour for a bout with a terrific bit of old French
poetry. Now it’s gone—the hour, I mean. I wish the poetry was nil,
too! And I’ve not opened my book! It’s almost dinner time, and after
dinner we’re due at Silverton Hall to help Robin rehearse that house
play. You hadn’t forgotten about it, had you?”
“I never forget anything I happen to remember,” was the re-assuring
response.
“Then keep on remembering the Beauty contest,” begged Marjorie
laughing. “This is Monday. I wish you could arrange it for Friday
night. I’m so anxious for Miss Monroe to win it. It will strengthen her
position on the campus.” Her lovely face grew suddenly serious. “You
know so well the way I feel about her, Leila. I’d love to have her free
herself from Leslie Cairns’ influence; to help her raise up a pride in
herself that will place her above doing the contemptible things the
Sans used to do.”
As she talked Marjorie’s voice took on a wistful earnestness which
Leila found irresistible. She did not share Marjorie’s views concerning
Doris Monroe. Nevertheless, Marjorie’s appeal to Leila for help in the
difficult conquest of the more difficult sophomore was in itself
sufficient cause for co-operation on Leila’s part.
“Watch the bulletin board tomorrow, and have no fears,” was Leila’s
parting advice as Marjorie reached the door. “We shall meet again,”
she added portentously.
“In about ten minutes; at dinner. And in my room, after dinner; and
after that, on the campus; and still after that, at Silverton Hall,” flung
back Marjorie over a shoulder as she went out the door. She ran
lightly down the hall to her room, inspirited by Leila’s promise. She
swung open the door with a gay little fling and entered to find Jerry
deep in the perusal of a letter.
“I’m going to be one of the judges at the Beauty contest,” she
breezily informed Jerry. “I forgot to ask Leila who she’d picked for
the other two judges.”
“It’s a good thing for the Ice Queen that you are going to wear a
disguise; efface your face from the college map for the time being,”
Jerry commented, eyes still on her letter. “No judge rig-out for
Jeremiah, I shall appear in all my fatal beauty. But I don’t expect to
get a fair deal,” Jerry sighed loudly. “When is the momentous Beauty
gathering to grace the gym?”
“Friday evening at eight.” Marjorie went on to recount hers and
Leila’s recent conversation.
“You old politician. You’ve everything fixed for your candidate,” Jerry
humorously accused. “What has become of the traditions of
Hamilton? Shocking!”
“They’re right in the foreground, AS ALWAYS,” retorted Marjorie. “I’m
neither old, nor a politician. Nothing has been fixed for my
candidate. Yes; I’ll admit I have one,” she declared in answer to
Jerry’s comically questioning glance. “Just the same, she can only
succeed on her own merits. Giving her a chance to do that isn’t
pulling strings for her.”
“I get you, Bean. I humbly apologize for any dark suspicions I may
have entertained against you. You are a Bean of rare pulchritude,
enterprise and integrity. You are not the only enterprising person on
the campus, though. I hate to speak of myself, but—er-her-r, ahem!”
Jerry loudly cleared her throat. “I’m a credit to the noble profession
of the sleuth.” Her tone of raillery held an undernote of triumph. Her
round face wore a victorious expression which Marjorie did not miss.
“What is it, Jeremiah? You’re brim full of something interesting. I
know you’re aching to tell me. Do go ahead.”
“It’s about those two letters,” Jerry began abruptly. “I mean the two
that were sent to you in the fall when the sophs were warring
among themselves, and Gentleman Gus drew the class presidency.”
“I haven’t forgotten them,” Marjorie said dryly. “You said you’d find
out all about them. Have you?” She gazed interestedly at Jerry.
“Now I begin to understand why you were praising yourself,” she
tacked on, with a teasing smile. “You’ll have just time to tell me
before the dinner gong sounds. Go to it.” She dropped easily down
upon her couch bed, eyes still intent on Jerry.
“You know, and so do I, that the sports committee letter was a fake.
We decided that first thing. Well, I’ve not discovered who wrote it.
I’m still suspicious of three different sets of girls on the campus. But
I haven’t a shred of proof against any of them. Being an honorable
sleuth I don’t prowl ignobly about the campus after my quarry. I set
legitimate traps for ’em. I deduce in a scientific and marvelous
manner. My methods are above reproach, but they take time.”
“So do your remarks,” Marjorie impolitely reminded. “The gong’s
going to ring very, very soon.”
“Oh, is it? So glad you told me. My, but you are rude at times. This is
one of ’em. Back to my subject. I never believed that Miss Walker
wrote the letter to you signed with her name. I made up my mind to
find out whether the handwriting was hers, but I failed to capture a
specimen of her penmanship. I tried a half a dozen nice, lady-like
little schemes. Not one worked. One day luck was with Jeremiah. I
picked up a fine and fussy handkerchief, monogrammed, L.M.W.”
With one eye on the clock Jerry hurriedly recounted the writing of
the note to Louise Walker and the subsequent mailing of it and the
handkerchief to the sophomore.
“Here’s the answer. Found it in the bulletin board this P. M. Look at
it. Next cast your eyes over this piece of bunk.” Jerry laid two
unfolded letters on the study table for Marjorie to examine.
Marjorie obediently left the couch where she had cosily disposed her
slim length. She reached Jerry’s side with one lithe bounce. Hastily
she picked up the letter Jerry indicated. Then she read:
“Dear Miss Macy:
“How fortunate for me that you should have found my pet
handkerchief! I bought it in Europe last summer of one of
those wonderful Belgian lace makers. I prize it highly on
account of the beauty of the embroidery. Consequently I
rarely carry it. Broke my rule for once and lost it. I had no
idea where. It is my good luck, and quite remarkable, I
think, that you should have guessed the initials on it to be
mine. Thank you for your courtesy. Assuring you of my
appreciation,
“Yours very sincerely,
“Louise May Walker.”
The warning, brazen voice of the dinner gong, which Miss Remson
rang but once before each meal, broke in upon Jerry’s pertinent
surmise. It was a signal which called for postponing further
conjecture in the matter.
“I’ve thought of Leslie Cairns more than once, Jerry, in connection
with both those letters,” Marjorie confessed as Jerry took the letters
Marjorie had carefully examined, folded them and tucked them into
a small leather portfolio. “Perhaps it’s been unfair in me to judge her
by past performances.”
“How could one help it? Come along, self-accusing Bean. I’m hungry
enough to eat all the dinner on our table, and give the rest of you
not a scrap. We’ll continue our amazing careers as private
investigators tonight after the ten-thirty bell is heard in the land and
a grateful hush has settled down on Room 15.”
During the busy, merry evening spent with Robin, Phil and the cast
of Silverton Hall payers, Marjorie had neither inclination nor
opportunity to consider the guilt or non-guilt of Leslie Cairns. As
stage manager Leila Harper combined more than usual efficiency
with a drollness of speech and manner which kept the amateur
thespians in a constant gale of giggles.
“Remember your cues and lines, or you’ll be walking into the middle
scenes where you’re neither expected nor wanted,” she warned her
flock.
The play, a two-act comedy entitled “The House Party,” was a bright,
snappy little production written by Eileen Potter, a promising
Silverton Hall sophomore. Phil had advocated the first production of
it as a house play. The sophomore class would be the guests of the
Silverton Hall sophs on the eventful evening. The living room was to
be turned into a theatre. Phil had enlisted Robin’s, Marjorie’s and
Leila’s services in rehearsing it.
Her plan, into which Robin, Marjorie and Leila gladly entered, had a
triple motive. She was anxious that Eileen’s talent should be
recognized on the campus. She was determined that the
unharmonious sophomore class should be brought into harmony.
She intended to hammer away at this plan until she accomplished
that harmony. Last of all, she liked giving house plays. Phil had a
soul even more bent on democracy than was that of Marjorie, if such
a condition could be. Robin often said to her: “Truly, Phil, if you had
lived in the days of ’76 you would have managed somehow to annex
your name to the Declaration of Independence.”
After the rehearsal the hard-working actors, managers and
prompters were treated to frozen custard and sponge cake by
Barbara Severn. She declared Leila to be a slave-driver and that the
custard and cake were needed by the cast as nourishment.
“If I am a slave-driver, why is it you are offering me custard and
cake?” Leila demanded, as Barbara presented her with a plate of the
frozen sweet.
“Merely because you have worked harder than your slaves. You are
what I should call a unique slave-driver,” Barbara sweetly explained.
“And you have far more good sense than you sometimes appear to
have,” Leila complimented. Whereupon the two beamed at each
other and shook hands.
“Don’t fail to be here for another rehearsal Thursday night and the
dress rehearsal on Saturday night,” were Leila’s parting words to the
cast, delivered in the middle of the front walk to the actor group
who had followed her out on the veranda.
She started across the campus in the pale winter moonlight with
Marjorie and Jerry, grumbling in pretended displeasure at the
amount of things she had to do during the next few days.
“Don’t say a word!” Marjorie exclaimed. “Two more rehearsals this
week, the Beauty contest on Friday night, Muriel’s birthday’s next
Monday. Saturday afternoon we have to go into town to buy
presents. Monday afternoon we’ll have to go over to Baretti’s to trim
the birthday table. Sunday I have to write letters, study and do a
dozen and one small things. I can say now I have nothing special on
hand after Monday, but long before then I’ll have a new lot of stunts
planned for the rest of next week.” Her tone grew more despairing
with each enumeration.
“You have so much trouble, Beauty, I’ll say nothing of my own,” was
Leila’s commiserating return, delivered with an unsympathetic grin.
“I am like an Irish fish out of water without Midget. That much I will
say.” Vera had gone to New York for a few days’ visit with her father
before he sailed on an all-winter cruise on the Mediterranean.
“I never saw an Irish fish. How does an Irish fish look?” Jerry
critically demanded.
“Like me. Did you not just hear me say it?” Leila retorted.
“I must go to the Arms to see Miss Susanna this week,” Marjorie
observed irrelevantly. No one appeared to be interested in her
announcement. Jerry and Leila were conducting a laughing
argument which had to do with Irish and non-Irish fishes.
“I love to talk to myself,” she made plaintive complaint when Jerry
and Leila finally paused for breath.
“And I had far rather talk to you, Beauty, than to some P. G.’s I
know,” Leila assured with deep meaning.
“You may talk to me, Bean,” Jerry graciously permitted. “I am
appreciative.”
During the remainder of the short hike across the campus Marjorie
became the laughing, but unimpressed, recipient of flattering
attention.
“Jerry,” she burst out abruptly, soon after the two girls were in their
own room, “it isn’t enough for us to say to each other that we are
glad Miss Walker didn’t write that letter. It is not fair to her not to tell
her the whole thing. Do you think it is?”
Jerry cocked her head to one side and considered. “Nope,” she
answered after due deliberation. “I suppose she ought to be
informed that she is not the villain we took her to be. It may take
marvelous managing by Marvelous Manager to tell her the awful
truth without rousing her ire. According to Gentleman Gus she is
anything but a lamb-like person when she isn’t pleased.”
“Would you be willing to go with me to see her?” Marjorie asked, her
brown eyes meditatively fixed on Jerry. “You are as——”
“Deep in the mud as you are in the mire,” supplied Jerry humorously.
“Something like that,” Marjorie agreed with a smile. “The letter was
sent to me in the first place, but the credit of the discovery that Miss
Walker didn’t write it belongs to you.”
“I’m not likely to pick any bouquets in such a briar patch,” shrugged
Jerry. “Don’t want em. More likely she’ll get wrathful at us when she
finds, we have kept the forged letter so long without going to her
and having matters out. But Jeremiah is not afraid. Let us hope she
behaves like the letter she really wrote.”
In the act of removing one of her slippers, Jerry took it by the strap.
Waving it jauntily she launched into a Bean jingle.
“Upon the haughty soph we’ll call
To clear her tarnished name;
For we have seen, O, noble Bean,
That she was not to blame.”