Functional Data Structures in R: Advanced Statistical Programming in R Mailund instant download
Functional Data Structures in R: Advanced Statistical Programming in R Mailund instant download
https://textbookfull.com/product/functional-data-structures-in-r-
advanced-statistical-programming-in-r-mailund/
https://textbookfull.com/product/functional-data-structures-in-r-
advanced-statistical-programming-in-r-thomas-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/advanced-r-statistical-
programming-and-data-models-analysis-machine-learning-and-
visualization-1st-edition-matt-wiley/
https://textbookfull.com/product/advanced-linear-modeling-
statistical-learning-and-dependent-data-3rd-edition-
christensen-r/
https://textbookfull.com/product/advanced-r-data-programming-and-
the-cloud-1st-edition-matt-wiley/
Functional Data
Structures in R
Thomas Mailund
Functional Data
Structures in R
Advanced Statistical
Programming in R
Thomas Mailund
Programming in R
Thomas Mailund
Aarhus N, Denmark
https://doi.org/10.1007/978-1-4842-3144-9
While the advice and information in this book are believed to be true
and accurate at the date of publication, neither the authors nor the
editors nor the publisher can accept any legal responsibility for any
errors or omissions that may be made. The publisher makes no
warranty, express or implied, with respect to the material contained
herein.
rights-permissions.
Table of Contents
Introduction xi
Chapter 1: Introduction1
Structure on Data
�������������������������������
�������������������������������
�������������������������������
�����4
List Functions
�������������������������������
�������������������������������
�������������������������������
���������28
Trees
�������������������������������
�������������������������������
�������������������������������
����������������������37
Bags
�������������������������������
�������������������������������
�������������������������������
�����������������������68
Stacks
�������������������������������
�������������������������������
�������������������������������
��������������������73
Queues
�������������������������������
�������������������������������
�������������������������������
�������������������74
iii
Table of Con
en s
Time Comparisons
�������������������������������
�������������������������������
���������������������������84
Double-Ended Queues
�������������������������������
�������������������������������
���������������������87
Lazy Queues
�������������������������������
�������������������������������
�������������������������������
�����������95
Implementing Lazy Evaluation
�������������������������������
�������������������������������
���������96
Lazy Lists
�������������������������������
�������������������������������
�������������������������������
����������98
Leftist Heaps
�������������������������������
�������������������������������
�������������������������������
��������140
Binomial Heaps
�������������������������������
�������������������������������
�������������������������������
����144
Splay Heaps
�������������������������������
�������������������������������
�������������������������������
���������157
Plotting Heaps
�������������������������������
�������������������������������
�������������������������������
������178
Search Trees
�������������������������������
�������������������������������
�������������������������������
��������190
Insertion
�������������������������������
�������������������������������
�������������������������������
���������195
Deletion
�������������������������������
�������������������������������
�������������������������������
����������203
Splay Trees
�������������������������������
�������������������������������
�������������������������������
�����������231
iv
Table of Con
Conclusions 247
Acknowledgements�����������������������
�������������������������������
�������������������������������
������248
Bibliography 249
Index 251
v
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
ix
Introduction
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
InT
In roduCTIon
CTI
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.
xii
CHAPTER 1
Introduction
Chapter 1 IntroduCtIon
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.
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.
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
Let’s say we want a data structure that represents sets, and we need
two operations on it: we want to be able to insert elements into the
set, and we want to be able to check if an element is found in the
set. The generic interface for such a data structure could look like
this:
insert <- function(set, elem) UseMethod("insert") member <-
function(set, elem) UseMethod("member") Using generic
functions, we can replace one implementation with another with little
hassle. We just need one place to specify which concrete
implementation we will use for an object we will otherwise only
access through the abstract interface. Each implementation we write
will have one function for constructing an empty data structure. This
empty structure sets the class for the concrete implementation, and
from here on we can access the data structure through generic
functions. We can write a simple list-based implementation of the set
data structure like this: empty_list_set <- function() {
1 If 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.
member(s, 1)
## [1] FALSE
s <- insert(s, 1)
member(s, 1)
## [1] TRUE
Another rule of thumb for interfaces that I will stick to in this book,
with one exception, is that I will always have my functions take the
data structure as the first argument. This isn’t something absolutely
necessary, but it fits the convention for generic functions, so it makes
it easier to work with abstract interfaces, and even when a function is
not abstract—when I need some helper functions—remembering that
the first argument is always the data structure is easier. The one
exception to this rule is the construction of linked lists, where
tradition is to have a construction function, cons, that takes an
element as its first argument and a list as its second argument and
construct a new list where the element is put at the head of the list.
This construction is too much of a tradition for me to mess with, and
I won’t write a generic function of it, so it doesn’t come into conflict
with how we handle polymorphism.
8
Chapter 2 abstraCt Data struCtures
We can construct linked lists in R using R’s built-in list data structure.
class = "linked_list_set")
We just construct a list with two elements, head and tail. These will
be references to other objects—head to the element we store in the
list, and tail to the rest of the list—so we are in effect using them as
pointers. We then add a class to the list to make linked lists work as
an implementation of an abstract data structure.
identical(x, linked_list_nil)
The is_empty function is a generic function that we will use for all
data structures.
The identical test isn’t perfect. It will consider any list element
containing NA as the last item in a list as the sentinel. Because we
don’t expect anyone to store NA in a linked list—it makes sense to
have missing data in a lot of analysis, but rarely does it make sense
to store it in data structures—it will have to do.
11
But this depends on many factors that cannot necessarily know about
when we design our algorithms. The computer the code will run on
might not be available to us when we develop our software, and both
its memory and CPU capabilities are likely to affect the running time
significantly. The running time is also likely to depend intimately on
the data we will run the algorithm on. If we want to know exactly
how long it will take to analyze a particular set of data, we have to
run the algorithm on this data. Once we have done this, we know
exactly how long it took to analyze the data, but by then it is too late
to explore different solutions to do the analysis faster.
First, we reduce our data to its size. We might have a set with n
elements, or a string of length n. Although our data structures and
algorithms might use very different actual wall time to work on
different data of the same size, we care only about the number n and
not the details of the data. Of course, data of the same size is not all
equal, so when we reduce all our information about it to a single size,
we have to be a little careful about what we mean when we talk
about the algorithmic complexity of a problem. Here, we usually use
one of two approaches: we speak of the worst-case or the
average/expected complexity. The worst-case 12
Second, we do not consider the actual running time for data of size n
—where we would need to know exactly how many operations of
different kinds would be executed by an algorithm, and how long
each kind of operation takes to execute. We just count the number of
operations and consider them equal. This gives us some function of n
that tells us how many operations an algorithm or operation will
execute, but not how long each operation takes. We don’t care about
the details when comparing most algorithms because we only care
about asymptotic behavior when doing most of our algorithmic
analysis.
runs in any big-O complexity that is an upper bound for the actual
runtime complexity. We want to get this upper bound as exact as we
can, to properly evaluate different choices of algorithms, but if we
have upper and lower bounds for various algorithms, we can still
compare them.
Another Random Scribd Document
with Unrelated Content
Human eyes can, however, neither see them, nor can human hands
touch them, for they are far removed from them, existing as they do
on another plane of existence. Yet, he who keeps his virtue, and
who knows the key to the chain of existences, can bring them out
from their own realm and into his own and cause them to act.
The third group of the Sephiroth stands in relation to Matter in the
same way as the other two stand to the Mind and the Heart, and
may be called Elementals par excellence. They are called Firmness,
Splendor, primary Foundation and Kingdom.—
I now wish to engage your attention by describing to you
Leibnitz’s Monads. His monads have all the characteristics of
Elementals, at the same time, that they seem to be purely physical
molecules. But this very duplicity is an argument for my theory, that
Leibnitz’s monad is a faithful definition of an Elemental. If it should
be proved that they are not Elementals, and I doubt that that can be
proved, they will at least serve as illustrations as to what an
Elemental is.
The paper on “The Higher Life,”159 and the remarks which it has
called forth, have led me to further reflections upon the subject.
That subject is in fact, Individual Evolution, and the warning
expressed by Murdhna Joti, in that article about “impetuously
rushing into the circle of ascetics,” opens up an important phase of
the topic most vital to humanity. For this sentence is not a mere
advisory caution; it points out the only available procedure, the one
course conducive to successful evolution, or final perfection. This
course may be briefly summed up in one word,—Rotation. Upon
examination we shall find this fact proved by the laws governing
Universal Brotherhood.
To begin with, when we take into consideration the personalities
of the real Founders of the Theosophical Society, we find ourselves
safely concluding that the institution of this principle of Brotherhood
as the basis of that Society, did not occur from any arbitrary
selection, nor yet from ethical or even humanitarian considerations
merely. We may say that it was not chosen; it presented itself as a
central fact, one which correlates with all things, and is itself one of
the aspects of the Great, the Mysterious Law. It must be moreover
that level of the Law most nearly related to the human being, and by
which alone he can raise himself from this “Slough of Despond”
called matter. Upon no lesser precedent than this would the Masters,
those supreme exponents of the Law, proceed. The outcome and
teaching of that Law is Unity; the power of Unity is its exoteric
expression. (Its hidden expression, Great Spirits alone can declare.)
This power is conferred by the economic tendency of Nature, which
uniformly moves along the line of least resistance and of larger
currents of energy, which draw in turn all minor streams of being
into their resistless tide. In order to bring home to all mankind the
primary fact that only as a united body, only by living in and working
with and for all, can unbroken advance to the Perfect Goal be
achieved, this unitarian necessity had to be conveyed by a term
which would appeal to the untrained, as well as to the cultured
mind. No man or woman so grossly ignorant but can sense the
advantages of “Universal Brotherhood,” while the more profound the
thinker, the more he warms to the sublime comprehensiveness of
this idea.
Many readers will doubtless recall an italicised sentence in the
“Diary of a Hindu,” also published in The Path. It ran as follows:—“No
Yogee will do a thing unless he sees the desire in another Yogee’s
mind.” These were the words of a teacher, and those who may
require it have here an authoritative recognition of the need of
humanitarian unity. For man’s strength lies in his perfect equilibrium,
and by man I now mean the whole, triune man. That this fact is also
true on the physical plane alone, is evidenced by medical testimony
to the effect that while perfect health is perfect balance, the more
complete this balance, the more readily is it disturbed. Thus trained
athletes are compelled to take dietetic and other precautions, which
men of minor strength disregard with apparent impunity. I say
“apparent,” because the result is of course visible in their inferior
physical powers. Only when the triune man has attained equilibrium
is he a moral force; then alone is he in complete harmony. Harmony
with what? With the Law that works for perfection or reunion, faith
in which and accordance with which, is the sum of the highest
consciousness of the human being. Now remember that there is at
all times a body, (be it numerically large or small,) of individuals
cognizing and waiting upon this Law. They perceive its tendency,
they only act with and through it, and the cumulative energy of this
compact body, plus certain impersonal forces, is in itself a
tremendous power, so vast in fact, that plus the energizing spirit
again, it may be said to form the exoteric expression of the Law
itself. Imagine some one member of this body attempting to act
from his separate impulse, and not from the general instinct. By
disengaging his unit of force from the sum total, he at once
neutralizes its effect and limits its expansive ratio; hence it is that
action from self, however disinterested, is enfeebling in its tendency.
This man may join himself to the powers of evil and act in opposition
to the Law: he has then the accruing benefit of that energetic total,
but this must fail in the long run, because it is minus the creative
spirit, which works for eventual harmony. So true is it that a given
cause produces similar results on all the planes alike, that in the
spiritual as in the physical world, there must be united action to
produce large results. The inutility of weak, single effort was
acknowledged by St. Paul when he said—“Because thou art neither
cold nor hot I will spew thee out of my mouth.” Unless the Yogee
therefore, perceives an idea in other related minds, as the reflection
of the Universal mind, he does not act. When the individual mind
has freed itself from all desire for personal action and resting in the
Universal Mind, acts passively with it alone, saying: “I rise with thy
rising, with thee subside,”—then the individual has attained Nirvana.
So that our present unit of power depends upon our greater or
lesser assimilation with the highest aggregate of mind, and its
continuance, upon our adherence to that manifested body of the
Universal Mind which works for Good, with faith into the Perfect Law.
This body in turn depends upon the individual efforts of its
members, for the continuous elevation and expansion of its highest
Ideal. Being thus interdependent, I think we may easily recognize
that Universal Brotherhood is the starting point towards final
success, and that its complete realization is the goal itself. Each may
attain Omniscience, but only as one of a body, not as a separate
part. “You shall enter the light, but you shall never touch the
flame.”160 So we may be part of the universal spirit, yet never that
spirit itself.
This Brotherhood then, in its harmonious equilibrium, implies
subservience to the Law of Evolution. The course marked out by this
Law is one of gradual progression through a series of interlinked
processes, not one of which can be intermitted or dropped, any
more than we can omit a link from a chain without break of
continuity, which would in this case imply a break of individuality,
either as applied to a member or to the whole body. We find this
course substantiated by Nature, who is our great initiator. Murdhna
Joti’s phrase about not rushing “into the circle of ascetics,” refers to
the rotation prevalent in Nature, and may be used in a large general
sense, and not merely applied to any especial circle, such as the
Hindu, Mahomedan, Christian or other group of ascetics. He refers to
the disadvantages consequent upon any violation of this rotatory
course; these apply quite as much to the farmer who fails to rotate
his crops, as to any thing or person rushing into any plane, before
being in all respects fitted to go there. Each plane in itself
constitutes a “circle of ascetics,” and must be entered in the proper
manner. In every department of Life we meet with an acceptance of
this fact. No man is admitted to the privileges of naturalization until
he has resided in a country, and has had time to accustom himself to
its manners and laws. It is ever held necessary to serve a certain
apprenticeship before entering any profession or trade. The social
usages even make “circles of ascetics” in this sense. A boor, a
ploughman, or even unsuitably attired persons, are not desired or
admitted in a parlor full of people in splendid array, and a natural
instinct makes them shrink from entering there. When exceptions
occur, there is an undercurrent of discord perceptible; all are alike ill
at ease. So in Nature, minerals, plants and animals are limited to
their proper sphere. Birds cannot swim nor fishes fly. I would say, as
birds or as fish per se they cannot do so, nor can the boor, as a boor,
be at ease with elevated minds. But advancement is the common lot
of all, provided it be made step by step in the natural series of
succession.
What then is this process in practical Life? It is, firstly, the
identification of yourself with the highest consciousness accessible
on your present plane, the engrafting upon your entire life of the
best ideal attainable, so that you may act upon it in every thought
and word. If you can do no more, select in your own mind the most
unselfish and pure-hearted person in your horizon, and study the
workings of such gracious aspirations and deeds. Noble ideals will
soon spring up within you, and by this lodestone similar minds will
swiftly be attracted, until you shall collectively form a nucleus of
persons identical in aim and influence. If one receives a ray of Truth,
he will speedily reflect it to all, and thus our attainment is largely
regulated by that of our compeers. Largely, but not entirely. There
are exceptional souls who progress with amazing velocity, far
outstripping the comrades of their starting-level. But even these
hearts of power reach up to the more perfect spirits above them,
and to feel this attraction they must have prepared themselves for it,
in the uniform, if rapid, rotation of previous existences. Each must
trace out the prescribed circuit, but he may travel fast or slow. Let
him not rashly conceive himself to be endowed with unusual spiritual
momentum: time is better spent in caution than in failure.
Murdhna Joti gives valid warning not to rush in until all is ready.
The circle is prepared, but you may not be so. Again, your fitness
may be assured and the circle for the moment closed. The course of
physical nature will exemplify my meaning. The blood leaves the
heart by the arteries and goes on to the capillary interchange with
the venous system, even as man descends from Spirit into matter,
and at the point of choice, turns, and reascends towards Spirit. The
veins take up the function of returning the blood to the heart; in
these are valves; they receive, hold and transmit the impulse from
the central heart. All the blood between any two valves has to stay
there until the next impulse comes from the heart; when this arrives,
it passes on. The valves close behind each quantum of blood thus
ejected through: it is not possible for the blood to recede;
retrogression is impeded by the closed valve. Nor can it remain;
progress is imperative when the next impulse drives it forward, and
so it goes on to the heart In the same manner each person should
stay in his appropriate place, not only until he is ready, but also until
the great Heart of all is ready to give the next impulse. Then he will
inevitably go on to the next place.
Masters have said that for “chelas and adepts alike there is an
abyss behind each step; a door closed. To stop or to go back is
impossible.” That which is true for the Adept is true for the humblest
disciple, each in his own manner and degree. It behooves us then to
concentrate our attention upon the natural and fitting method of
progression, and to assist those about us in maintaining a high
average of ideality, that the entire body may progress evenly,
steadily, and that nowhere may ignorance or undue haste clot or
clog the way. In the end, the reward of patience is holy. In every
effort you make to lighten the mind of another and open it to Truth,
you help yourself. “Those pearls you find for another and give to
him, you really retain for yourself in the act of benevolence. Never
lose, then, that altitude of mind. Never, never desire to get
knowledge or power for any other purpose than to give it on the
altar, for thus alone can it be saved to you. When you open any
door, beyond it you find others standing there who had passed you
long ago, but now, unable to proceed, they are there waiting; others
are there waiting for you! Then you come, and opening a door, those
waiting disciples perhaps may pass on; thus on and on. What a
privilege this, to reflect that we may perhaps be able to help those
who seemed greater than ourselves.”161
The consent of the Spirit has hallowed those thoughts. Another
Messenger of Truth once said:—“The first shall be last and the last
first; contain yourselves, therefore, in Peace.”
Jasper Niemand, F. T. S.
Thoughts in Solitude.
I.
Within the symbols and doctrines of the Christian Church may
indeed lie hidden all the truths of the Occult Philosophy, and another
and abler pen has already traced the correspondences, but it is
necessary to realize differences as well as likenesses, and while
Christianity, as a definite system, has embodied for the world many
noble ideas, it seems to the writer to have been able to display only
one fact of the divine jewel of Truth—to have been able to trace only
a short line of the celestial circle of Wisdom.
Putting aside all such unphilosophical dogmas, as a personal
anthropomorphic God—atonement by the vicarious sacrifice of
another—eternal damnation and such like, which may be regarded
as the outworks of the Creed, and which indeed many of its own
professors deny or minimize, and coming to the essential kernel of
the system—the inner stronghold of the faith—that which would be
regarded as such by all its truest sons throughout these nearly
nineteen centuries of its existence, it would yet seem to be but a
one-sided statement—a partial view—compared with the all-
embracing Catholicity of the Occult Wisdom.
Unfortunately the outworks and excrescences above referred to,
have, during these many centuries, so warped the thoughts and
feelings of the populations professing this religion that it is no longer
the pure and exalted doctrine as preached by its founder, but
something very different. There are, no doubt, here and there good
and noble souls, who practice the higher virtues of Christianity, but
they are in such a minority that they are quite unable to affect the
popular standard.
When one begins to analyse the stupendous outgrowth called
Western Civilization, of which steam and electricity, in their practical
uses, may be regarded as the types, and to ask how and by what
means this vast fabric has arisen, we are informed by those who are
able to see below the mere surface of things that the setting of
men’s minds in a certain direction must have been the factor, and it
is only logical that if a man’s highest religious duty is put before him
as the saving of his own soul from perdition, a tendency of mind
which may be characterized as the supremely selfish must naturally
be set in motion. When the converging lines of heredity through
many generations have so strengthened this tendency that it has
become a potent factor, the development “in excelsis” of the purely
intellectual faculties as dissociated from the moral will be seen to be
the inevitable result, and from this has naturally evolved the Western
Civilization which is spoken of with so much pride. But are not
nations like trees to be known by their fruits? “Do men gather
grapes of thorns, or figs of thistles?”
What sins are dwelt on with more emphatic reprobation
throughout the whole teachings of Christ than those of hypocrisy
and cupidity? And where is hypocrisy deeper than within the
Christian fold? So deep indeed, that it has become an integral part of
the nature, and is no more recognized as a vice than it was by the
Pharisees of old. And where is the worship of mammon more
rampant than throughout the length and breadth of Christendom?
The preachers of the Churches may utter faint-hearted protests, but
the nations nevertheless remain prostrate before their idol, and as
steam and electricity extend their sway, and new countries are laid
open to modern progress, the more primitive races, to avoid
extinction, join in the mad competition for wealth. But whether
conspicuously shown in the acts of States lustful to conquer fresh
territory, or hidden in the individual character, where it displays itself
in the haste to grow rich by fair means or foul, it remains none the
less a gnawing canker at the heart of Christendom.
What a gulf there lies between the practice of modern Europe and
the divine teachings of the Master.
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