Functional Data Structures in R: Advanced Statistical Programming in R Thomas Mailund download
Functional Data Structures in R: Advanced Statistical Programming in R Thomas Mailund download
https://textbookfull.com/product/functional-data-structures-in-r-
advanced-statistical-programming-in-r-thomas-mailund/
https://textbookfull.com/product/functional-data-structures-in-r-
advanced-statistical-programming-in-r-mailund/
https://textbookfull.com/product/functional-programming-in-r-
advanced-statistical-programming-for-data-science-analysis-and-
finance-1st-edition-thomas-mailund/
https://textbookfull.com/product/domain-specific-languages-in-r-
advanced-statistical-programming-1st-edition-thomas-mailund/
https://textbookfull.com/product/domain-specific-languages-in-r-
advanced-statistical-programming-1st-edition-thomas-mailund-2/
Metaprogramming in R: Advanced Statistical Programming
for Data Science, Analysis and Finance 1st Edition
Thomas Mailund
https://textbookfull.com/product/metaprogramming-in-r-advanced-
statistical-programming-for-data-science-analysis-and-
finance-1st-edition-thomas-mailund/
https://textbookfull.com/product/advanced-object-oriented-
programming-in-r-statistical-programming-for-data-science-
analysis-and-finance-1st-edition-thomas-mailund/
https://textbookfull.com/product/beginning-data-science-in-r-
data-analysis-visualization-and-modelling-for-the-data-
scientist-1st-edition-thomas-mailund/
https://textbookfull.com/product/pointers-in-c-programming-a-
modern-approach-to-memory-management-recursive-data-structures-
strings-and-arrays-thomas-mailund/
https://textbookfull.com/product/advanced-r-statistical-
programming-and-data-models-analysis-machine-learning-and-
visualization-1st-edition-matt-wiley/
Functional Data
Structures in R
Advanced Statistical Programming in R
—
Thomas Mailund
Functional Data
Structures in R
Advanced Statistical
Programming in R
Thomas Mailund
Functional Data Structures in R: Advanced Statistical
Programming in R
Thomas Mailund
Aarhus N, Denmark
Chapter 1: Introduction������������������������������������������������������������������������1
iii
Table of Contents
Chapter 5: Heaps������������������������������������������������������������������������������135
Leftist Heaps�����������������������������������������������������������������������������������������������������140
Binomial Heaps�������������������������������������������������������������������������������������������������144
Splay Heaps������������������������������������������������������������������������������������������������������157
Plotting Heaps���������������������������������������������������������������������������������������������������178
Heaps and Sorting���������������������������������������������������������������������������������������������183
iv
Table of Contents
Conclusions��������������������������������������������������������������������������������������247
A
cknowledgements������������������������������������������������������������������������������������������248
Bibliography�������������������������������������������������������������������������������������249
Index�������������������������������������������������������������������������������������������������251
v
About the Author
Thomas Mailund is an associate professor in bioinformatics at Aarhus
University, Denmark. He has a background in math and computer science.
For the last decade, his main focus has been on genetics and evolutionary
studies, particularly comparative genomics, speciation, and gene flow
between emerging species. He has published Beginning Data Science in R,
Functional Programming in R, and Metaprogramming in R with Apress, as
well as other books.
vii
About the Technical Reviewer
Karthik Ramasubramanian works for one
of the largest and fastest-growing technology
unicorns in India, Hike Messenger, where
he brings the best of business analytics
and data science experience to his role. In
his seven years of research and industry
experience, he has worked on cross-industry
data science problems in retail, e-commerce,
and technology, developing and prototyping
data-driven solutions. In his previous role at Snapdeal, one of the largest
e-commerce retailers in India, he was leading core statistical modeling
initiatives for customer growth and pricing analytics. Prior to Snapdeal,
he was part of the central database team, managing the data warehouses
for global business applications of Reckitt Benckiser (RB). He has vast
experience working with scalable machine learning solutions for industry,
including sophisticated graph network and self-learning neural networks.
He has a master’s degree in theoretical computer science from PSG College
of Technology, Anna University, and is a certified big data professional. He
is passionate about teaching and mentoring future data scientists through
different online and public forums. He enjoys writing poems in his leisure
time and is an avid traveler.
ix
Introduction
This book gives an introduction to functional data structures. Many
traditional data structures rely on the structures being mutable. We can
update search trees, change links in linked lists, and rearrange values in a
vector. In functional languages, and as a general rule in the R programming
language, data is not mutable. You cannot alter existing data. The
techniques used to modify data structures to give us efficient building
blocks for algorithmic programming cannot be used.
There are workarounds for this. R is not a pure functional language,
and we can change variable-value bindings by modifying environments.
We can exploit this to emulate pointers and implement traditional
data structures this way; or we can abandon pure R programming and
implement data structures in C/C++ with some wrapper code so we can
use them in our R programs. Both solutions allow us to use traditional data
structures, but the former gives us very untraditional R code, and the latter
has no use for those not familiar with other languages than R.
The good news, though, is that we don’t have to reject R when
implementing data structures if we are willing to abandon the traditional
data structures instead. There are data structures that we can manipulate
by building new versions of them rather than modifying them. These data
structures, so-called functional data structures, are different from the
traditional data structures you might know, but they are worth knowing if
you plan to do serious algorithmic programming in a functional language
such as R.
There are not necessarily drop-in replacements for all the data
structures you are used to, at least not with the same runtime performance
for their operations, but there are likely to be implementations for most
xi
Introduction
abstract data structures you regularly use. In cases where you might have
to lose a bit of efficiency by using a functional data structures instead of a
traditional one, however, you have to consider whether the extra speed is
worth the extra time you have to spend implementing a data structure in
exotic R or in an entirely different language.
There is always a trade-off when it comes to speed. How much
programming time is a speed-up worth? If you are programming in R,
chances are you value programmer-time over computer-time. R is a high-
level language and relatively slow compared to most other languages.
There is a price to providing higher levels of expressiveness. You accept
this when you choose to work with R. You might have to make the same
choice when it comes to selecting a functional data structure over a
traditional one, or you might conclude that you really do need the extra
speed and choose to spend more time programming to save time when
doing an analysis. Only you can make the right choice based on your
situation. You need to find out the available choices to enable you to work
data structures when you cannot modify them.
xii
CHAPTER 1
Introduction
This book gives an introduction to functional data structures. Many
traditional data structures rely on the structures being mutable. We
can update search trees, change links in linked lists, and rearrange
values in a vector. In functional languages, and as a general rule in the R
programming language, data is not mutable. You cannot alter existing data.
The techniques used to modify data structures to give us efficient building
blocks for algorithmic programming cannot be used.
There are workarounds for this. R is not a pure functional language,
and we can change variable-value bindings by modifying environments.
We can exploit this to emulate pointers and implement traditional
data structures this way; or we can abandon pure R programming and
implement data structures in C/C++ with some wrapper code so we can
use them in our R programs. Both solutions allow us to use traditional data
structures, but the former gives us very untraditional R code, and the latter
has no use for those not familiar with other languages than R.
The good news, however, is that we don’t have to reject R when
implementing data structures if we are willing to abandon the traditional
data structures instead. There are data structures we can manipulate by
building new versions of them rather than modifying them. These data
structures, so-called functional data structures, are different from the
traditional data structures you might know, but they are worth knowing if
you plan to do serious algorithmic programming in a functional language
such as R.
There are not necessarily drop-in replacements for all the data
structures you are used to, at least not with the same runtime performance
for their operations—but there are likely to be implementations for most
abstract data structures you regularly use. In cases where you might have
to lose a bit of efficiency by using a functional data structure instead of a
traditional one, you have to consider whether the extra speed is worth the
extra time you have to spend implementing a data structure in exotic R or
in an entirely different language.
There is always a trade-off when it comes to speed. How much
programming time is a speed-up worth? If you are programming in R,
the chances are that you value programmer time over computer time. R
is a high-level language that is relatively slow compared to most other
languages. There is a price to providing higher levels of expressiveness.
You accept this when you choose to work with R. You might have to make
the same choice when it comes to selecting a functional data structure
over a traditional one, or you might conclude that you really do need the
extra speed and choose to spend more time programming to save time
when doing an analysis. Only you can make the right choice based on your
situation. You need to find out the available choices to enable you to work
data structures when you cannot modify them.
2
CHAPTER 2
Abstract Data
Structures
Before we get started with the actual data structures, we need to get
some terminologies and notations in place. We need to agree on what an
abstract data structure is—in contrast to a concrete one—and we need to
agree on how to reason with runtime complexity in an abstract way.
If you are at all familiar with algorithms and data structures, you can
skim quickly through this chapter. There won’t be any theory you are not
already familiar with. Do at least skim through it, though, just to make sure
we agree on the notation I will use in the remainder of the book.
If you are not familiar with the material in this chapter, I urge you to
find a text book on algorithms and read it. The material I cover in this
chapter should suffice for the theory we will need in this book, but there
is a lot more to data structures and complexity than I can possibly cover
in a single chapter. Most good textbooks on algorithms will teach you a lot
more, so if this book is of interest, you should not find any difficulties in
continuing your studies.
Structure on Data
As the name implies, data structures have something to do with structured
data. By data, we can just think of elements from some arbitrary set. There
might be some more structure to the data than the individual data points,
and when there is we keep that in mind and will probably want to exploit
that somehow. However, in the most general terms, we just have some
large set of data points.
So, a simple example of working with data would be imagining we
have this set of possible values—say, all possible names of students at a
university—and I am interested in a subset—for example, the students
that are taking one of my classes. A class would be a subset of students,
and I could represent it as the subset of student names. When I get an
email from a student, I might be interested in figuring out if it is from one
of my students, and in that case, in which class. So, already we have some
structure on the data. Different classes are different subsets of student
names. We also have an operation we would like to be able to perform on
these classes: checking membership.
There might be some inherent structure to the data we work with, which
could be properties such as lexicographical orders on names—it enables us to
sort student names, for example. Other structure we add on top of this. We add
structure by defining classes as subsets of student names. There is even a third
level of structure: how we represent the classes on our computer.
The first level of structure—inherent in the data we work with—is not
something we have much control over. We might be able to exploit it in
various ways, but otherwise, it is just there. When it comes to designing
algorithms and data structures, this structure is often simple information;
if there is order in our data, we can sort it, for example. Different
algorithms and different data structures make various assumptions about
the underlying data, but most general algorithms and data structures make
few assumptions. When I make assumptions in this book, I will make those
assumptions explicit.
4
Chapter 2 Abstract Data Structures
5
Chapter 2 Abstract Data Structures
1
I f you are unfamiliar with generic functions and the S3 system, you can check out
my book Advanced Object-Oriented Programming in R book (Apress, 2017), where
I explain all this.
6
Another Random Scribd Document
with Unrelated Content
As for Alf, he returned at once to his battalion, where he gave
unsatisfactory answers to all questions. He was a man of little
imagination, but it seemed that he was now in his own case
beginning to link up cause with effect. At all events he refrained for
as long as possible from cleaning his second tunic-button, and might
have been seen now and again regarding it with awe not unmixed
with alarm.
CHAPTER III
THE MIRACLE OF THE PLANES
The word "rest" as used at the front has been described as being
purely a technical term, bearing no relation whatever to the other
word of the same name. Certainly during the last fortnight of this
particular period Alf Higgins and Bill Grant found cause to realize the
truth of this description.
A new brigadier had just been appointed to command the Middlesex
Fusiliers Brigade. He was an upstanding young giant of thirty, and
the main tenets of his creed were fitness and efficiency. In pursuit of
the latter he organized strenuous sham fights over miles of country,
and he urged upon his colonels that only by encouraging athletic
contests on a hitherto unheard of scale could they hope to attain the
former.
Alf and Bill were no athletes, but they continued to play football with
more vigor than skill until their platoon was knocked out of the
battalion competition. They bore their defeat with stoicism, hoping
that they would now be allowed to assume the much more
accustomed and congenial rôle of spectators. Instead of this they
found themselves (to their inexpressible indignation) called upon to
sustain the battalion's honor in cross-country runs under the eye of
that speechless but efficient officer Lieutenant Donaldson.
In the evenings, however, they were free to extract what
amusement they could out of life. The pierrot troupe, without which
no division at the front considered itself complete, played to packed
houses every other night in the Y.M.C.A.; while a cinematograph
show had been rigged up in a barn. Each day, also, a limited number
of passes to Amiens entitled such as were favored of Fortune to a
blissful day's taste of civilization.
To the officers, however, it seemed sometimes incredible that any of
the men could patronize these delights at all.
"I believe," said Richards to Allen one evening, "that every man in
this company must write to every relation, friend, acquaintance or
business connection he has in Blighty seven times in the week, just
to spite us!"
The company letters had just come in to be censored. Donaldson
had gone to a Sports Committee meeting, and Shaw, as mess
president, was in Amiens restocking the larder.
"Lord, what a pile!" said Allen, sitting down at the table and
beginning his task. "It's lucky I've no letters of my own to write—or
only a note."
He gave a sigh; the man at the front who has nobody in England to
write to is not to be envied.
"I have, though," said Captain Richards. "My wife'll be thinking I'm
dead if I don't write her a proper letter soon."
He also took a handful of letters and set to work.
"May I come in?" said a voice at the door. "Or are you too busy?"
"Come in, of course, major."
The second-in-command entered, glanced round and took in the
situation.
"Don't let me interrupt you," he said politely. "I haven't come to see
you at all, so don't flatter yourselves. I wanted to see Denis's Sketch
and Tatler, that's all."
"On my bed, sir," said Allen.
"Thanks."
There was unbroken silence for some minutes. Then the major cast
The Tatler from him with an exclamation of disgust.
"I wish," he said, "that that grinning little idiot would stop
advertising herself for a bit. You can't pick up a picture-paper
without seeing her selling things or dressing up or generally pushing
herself into the limelight. She wants smacking."
Both men at the table looked up.
"Who's the grinning idiot in question, major?"
"Isobel FitzPeter. Here you are—a whole page of her and her bally
bulldog, labeled 'A famous Beauty—and Friend.' Same photograph in
The Sketch, called 'Beauty and the Beast'! It makes me sick!"
Allen suddenly got up and went out of the room without a word,
very red in the face. Richards and Major Parker stared after him, the
former very embarrassed, the latter simply surprised.
"What's the matter?" asked the major blankly.
"I expect poor old Denis felt he might have used language
unbefitting your rank if he'd stayed. You see—don't let on to a soul,
mind—he's most frightfully gone on the FitzPeter girl."
"Good God, Dickie, what have I said? D'you mean they're engaged
or anything?"
"Oh, no. I don't believe she knows him at all. He used to play cricket
at her father's place, and they were rather pals then, I believe. But
since she's grown up, they've never met. But you know how it is out
here. If I hadn't had my wife to think about, I'd have gone mad long
ago. Denis doesn't seem to have many feminine belongings of his
own, so he's simply installed this girl as a kind of goddess. He seems
to live for the illustrated papers—simply devours them, and cuts out
her picture. This is all rather confidential, major."
"Of course. Poor old chap. You know, Dickie, I do happen to know
the lady. In peace time she was as nice a kid as you could want to
meet. If Denis hasn't met her since then, I don't wonder at him,
because she's really frightfully pretty. But her head has been utterly
turned. She acts as parlor-maid once a fortnight in a hospital my
sister runs in Kensington, and she's more hindrance than help,
because she never arrives in time, and she's always got some
footling reason for wanting to go early. But her photograph in V.A.D.
uniform gets published about once a fortnight, usually headed
'Nursing the Wounded,' or, 'An Indefatigable War Worker'! The worst
of it is she's got brains if she'd use them; only she won't. A spoilt,
thoughtless little idiot, and as pretty as they make 'em. Poor old
Denis."
At this point Allen returned and resumed his work without a word.
The major fell silent. Richards cast about for some subject to cover
the awkward break in the conversation.
"D'you know when we go back to the line, sir?" he asked at last.
"Not settled. End of the week, I think. Look here, I've interrupted
you fellows quite enough. Give me some of those letters."
"Thanks awfully, sir. You're a sportsman."
By dinner time the pile was finished, and Allen had time to write his
note.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com