Functional Programming in Python 1st Edition David Mertz PDF Download
Functional Programming in Python 1st Edition David Mertz PDF Download
https://ebookultra.com/download/functional-programming-in-python-1st-
edition-david-mertz/
https://ebookultra.com/download/text-processing-in-python-2-print-
edition-mertz/
https://ebookultra.com/download/functional-programming-in-c-first-
edition-ivan-cukic/
https://ebookultra.com/download/maya-programming-with-python-
cookbook-1st-edition-herbez/
https://ebookultra.com/download/parallel-programming-with-python-
first-edition-jan-palach/
Real World Functional Programming With Examples in F and C
1st Edition Tomas Petricek
https://ebookultra.com/download/real-world-functional-programming-
with-examples-in-f-and-c-1st-edition-tomas-petricek/
https://ebookultra.com/download/principles-of-soft-computing-using-
python-programming-1st-edition-gypsy-nandi/
https://ebookultra.com/download/python-cookbook-3rd-edition-david-
beazley/
https://ebookultra.com/download/introduction-to-computing-and-
programming-in-python-a-multimedia-approach-mark-j-guzdial/
https://ebookultra.com/download/elements-of-programming-interviews-in-
python-1st-edition-adnan-aziz-amit-prakash-tsung-hsien-lee/
Functional programming in Python 1st Edition David
Mertz Digital Instant Download
Author(s): David Mertz
ISBN(s): 9781491928561, 1491928565
Edition: 1
File Details: PDF, 1.56 MB
Year: 2015
Language: english
Functional
Programming
in Python
David Mertz
Additional
Resources
4 Easy Ways to Learn More and Stay Current
Programming Newsletter
Get programming r elated news and content delivered weekly to your inbox.
oreilly.com/programming/newsletter
O’Reilly Radar
Read more insight and analysis about emerging technologies.
radar.oreilly.com
Conferences
Immerse yourself in learning at an upcoming O’Reilly conference.
conferences.oreilly.com
©2015 O’Reilly Media, Inc. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. #15305
Functional Programming
in Python
David Mertz
Functional Programming in Python
by David Mertz
Copyright © 2015 O’Reilly Media, Inc. All rights reserved.
Attribution-ShareAlike 4.0 International (CC BY-SA 4.0).
See: http://creativecommons.org/licenses/by-sa/4.0/
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA
95472.
O’Reilly books may be purchased for educational, business, or sales promotional use.
Online editions are also available for most titles (http://safaribooksonline.com). For
more information, contact our corporate/institutional sales department:
800-998-9938 or [email protected].
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Functional Pro‐
gramming in Python, the cover image, and related trade dress are trademarks of
O’Reilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the
information and instructions contained in this work are accurate, the publisher and
the author disclaim all responsibility for errors or omissions, including without limi‐
tation responsibility for damages resulting from the use of or reliance on this work.
Use of the information and instructions contained in this work is at your own risk. If
any code samples or other technology this work contains or describes is subject to
open source licenses or the intellectual property rights of others, it is your responsi‐
bility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-92856-1
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
Callables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Named Functions and Lambdas 12
Closures and Callable Instances 13
Methods of Classes 15
Multiple Dispatch 19
Lazy Evaluation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
The Iterator Protocol 27
Module: itertools 29
Higher-Order Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Utility Higher-Order Functions 35
The operator Module 36
The functools Module 36
Decorators 37
iii
Preface
• Functions are first class (objects). That is, everything you can do
with “data” can be done with functions themselves (such as
passing a function to another function).
• Recursion is used as a primary control structure. In some lan‐
guages, no other “loop” construct exists.
v
• There is a focus on list processing (for example, it is the source
of the name Lisp). Lists are often used with recursion on sublists
as a substitute for loops.
• “Pure” functional languages eschew side effects. This excludes
the almost ubiquitous pattern in imperative languages of assign‐
ing first one, then another value to the same variable to track
the program state.
• Functional programming either discourages or outright disal‐
lows statements, and instead works with the evaluation of
expressions (in other words, functions plus arguments). In the
pure case, one program is one expression (plus supporting defi‐
nitions).
• Functional programming worries about what is to be computed
rather than how it is to be computed.
• Much functional programming utilizes “higher order” functions
(in other words, functions that operate on functions that oper‐
ate on functions).
vi | Preface
functional programming are available. The one exception here is
that I will discuss Matthew Rocklin’s multipledispatch as the best
current implementation of the concept it implements.
Most third-party libraries around functional programming are col‐
lections of higher-order functions, and sometimes enhancements to
the tools for working lazily with iterators contained in itertools.
Some notable examples include the following, but this list should
not be taken as exhaustive:
Resources
There are a large number of other papers, articles, and books written
about functional programming, in Python and otherwise. The
Python standard documentation itself contains an excellent intro‐
duction called “Functional Programming HOWTO,” by Andrew
Kuchling, that discusses some of the motivation for functional pro‐
gramming styles, as well as particular capabilities in Python.
Preface | vii
Random documents with unrelated
content Scribd suggests to you:
Having brought the place at which the hour is given to the
meridian, set the hour index to the given hour; then turn the globe
about until the place where the hour is required comes to the
meridian, and the index will point out the hour at the said place.
Thus when it is Noon at London, it is
H. M.
Rome 0 52 P. M.
Constantinople 2 07 P. M.
At
Vera-Cruz 5 30 A. M.
Pekin in China 7 50 P. M.
Having found the Sun’s place in the ecliptic, bring the same to
the meridian, and note the degree over it; then turning the globe
round, all places that pass under that degree will have the Sun
vertical that day.
Bring the given place to the meridian, and mark what degree of
latitude is exactly over it; then turning the globe about its axis, those
two points of the ecliptic, which pass exactly under the said mark,
are the Sun’s place; against which, upon the wooden horizon, you’ll
have the days required.
Having found the Sun’s declination, and brought the first place
(London) to the meridian, set the index to the given hour, then turn
the globe about until the index points to XII at noon; which being
done, that place upon the globe which stands under the point of the
Sun’s declination upon the meridian, has the Sun that moment in the
Zenith.
Having found the place where the Sun is vertical at the given
hour, rectify the globe for that latitude, and bring the said place to
the meridian.
Then all those places that are in the Western semicircle of the
horizon, have the Sun rising at that time.
Those in the Eastern semicircle have it setting.
To those who live under the upper semicircle of the meridian, it is
12 o’clock at noon. And,
Those who live under the lower semicircle of the meridian, have
it at midnight.
All those places that are above the horizon, have the Sun above
them, just so much as the places themselves are distant from the
horizon; which height may be known by fixing the quadrant of
altitude in the zenith, and laying it over any particular place.
In all those places that are 18 degrees below the Western side of
the horizon, the twilight is just beginning in the morning, or the day
breaks. And in all those places that are 18 degrees below the
Eastern side of the horizon, the twilight is ending, and the total
darkness beginning.
The twilight is in all those places whose depression below the
horizon does not exceed 18 degrees. And,
All those places that are lower than 18 degrees, have dark night.
The depression of any place below the horizon is equal to the
altitude of its Antipodes, which may be easily found by the quadrant
of altitude.
It has been shewed in the last problem, how to place the globe
in such a position as to exhibit the length of the diurnal and
nocturnal arches in all places of the Earth, at a particular time: If the
globe be continually rectified, according as the Sun alters his
declination, (which may be known by bringing each degree of the
ecliptic successively to the meridian) you’ll see the gradual increase
or decrease made in the days, in all places of the World, according
as a greater or lesser portion of the parallels of latitude, stands
above the horizon. We shall illustrate this problem by examples
taken at different times of the year.
1. Let the Sun be in the first point of ♋ (which happens on the
21st of June) that point being brought to the meridian, will shew the
Sun’s declination to be 23½ degrees North; then the globe must be
rectified to the latitude of 23½ degrees; and for the better
illustration of the problem, let the first meridian upon the globe be
brought under the brass meridian. The globe being in this position,
you’ll see at one view the length of the days in all latitudes, by
counting the number of hours contained between the two extreme
meridians, cutting any particular parallel you pitch upon, in the
Eastern and Western part of the horizon. And you may observe that
the lower part of the arctic circle just touches the horizon, and
consequently all the people who live in that latitude have the Sun
above their horizon for the space of 24 hours, without setting; only
when he is in the lower part of the meridian (which they would call
12 at night) he just touches the horizon.
To all those who live between the arctic circle and the Pole, the
Sun does not set, and its height above the horizon, when he is in the
lower part of the meridian, is equal to their distance from the arctic
circle: For example, Those who live in the 83d parallel have the Sun
when he is lowest at this time 13½ degrees high.
If we cast our eye Southward, towards the equator, we shall find,
that the diurnal arches, or the length of days in the several latitudes,
gradually lessen: The diurnal arch of the parallel of London at this
time is 16½ hours; that of the Equator (is always) 12 hours; and so
continually less, ’till we come to the Antarctic Circle, the upper part
of which just touches the horizon; just those who live in this latitude
have just one sight of the Sun, peeping as it were in the horizon:
And all that space between the antarctic circle and the South Pole,
lies in total darkness.
If from this position we gradually move the meridian of the globe
according to the progressive alterations made in the Sun’s
declination, by his motion in the ecliptic, we shall find the diurnal
arches of all those parallels, that are on the Northern side of the
equator, continually decrease; and those on the Southern side
continually increase, in the same manner as the days in those places
shorten and lengthen. Let us again observe the globe when the Sun
has got within 10 degrees of the equinoctial; now the lower part of
the 80th parallel of North latitude just touches the horizon, and all
the space betwixt this and the pole, falls in the illuminated
hemisphere: but all those parallels that lie betwixt this and the arctic
circle, which before were wholly above the horizon, do now intersect
it, and the Sun appears to them to rise and set. From hence to the
equator, we shall find that the days have gradually shortened; and
from the equator Southward, they have gradually lengthened, until
we come to the 80th parallel of the South latitude; the upper part of
which just touches the horizon; and all places betwixt this and the
South Pole are in total darkness; but those parallels betwixt this and
the antarctic circle, which before were wholly upon the horizon, are
now partly above it; the length of their days being exactly equal to
that of the nights in the same latitude in the contrary hemisphere.
This also holds universally, that the length of one day in one latitude
North, is exactly equal to the length of the night in the same latitude
South; and vice versa.
Let us again follow the motion of the Sun, until he has got into
the equinoctial, and take a view of the globe while it is in this
position. Now all the parallels of latitude are cut into two equal parts
by the horizon, and consequently the days and nights are of equal
lengths, viz. 12 hours each, in all places of the world; the Sun rising
and setting at six o’clock, excepting under the two Poles, which now
lie exactly in the horizon: Here the Sun seems to stand still in the
same point of the heavens for some time, until by degrees, by his
motion in the ecliptic, he ascends higher to one and disappears to
the other, there being properly no days and nights under the Poles;
for there the motion of the Earth round its axis cannot be observed.
If we follow the motion of the Sun towards the Southern tropic,
we shall see the diurnal arches of the Northern parallels continually
decrease, and the Southern ones increase in the same proportion,
according to their respective latitudes; the North Pole continually
descending, and the South Pole ascending, above the horizon, until
the Sun arrives into ♑ , at which time all the space within the
antarctic circle is above the horizon; while the space between the
arctic circle, and its neighbouring Pole, is in total darkness. And we
shall now find all other circumstances quite reverse to what they
were when the Sun was in ♋ ; the nights now all over the world
being of the same length that the days were of before.
We have now got to the extremity of the Sun’s declination; and if
we follow him through the other half of the ecliptic, and rectify the
globe accordingly, we shall find the seasons return in their order,
until at length we bring the globe into its first position.
The two foregoing problems were not, as I know of, published in
any book on this subject before; and I have dwelt the longer upon
them, because they very well illustrate how the vicissitudes of days
and nights are made all over the world, by the motion of the Earth
round her axis; the horizon of the globe being made the circle,
separating light and darkness, and so the Sun to stand still in the
vertex. And if we really could move the meridian, according to the
change of the Sun’s declination, we should see at one view, the
continual change made in the length of days and nights, in all places
on the Earth; but as globes are fitted up, this cannot be done;
neither are they adapted for the common purposes, in places near
the equator, or any where in the Southern hemisphere. But this
inconvenience is now remedied (at a small additional expence) by
the hour circle being made to shift to either Pole; and some globes
are now made with an hour circle fixed to the globe at each Pole
between the globe and meridian, so as to have none without side to
interrupt the meridian from moving quite round the wooden horizon.
Bring the solstitial point to the meridian, and set the index to 12
at noon; then turn the globe Westward, ’till the index points at half
the number of hours given; which being done, keep the globe from
turning round its axis, and slide the meridian up or down in the
notches, ’till the solstitial point comes to the horizon, then that
elevation of the Pole will be the latitude.
If the hours given be 16, the latitude is 49 degrees; if 20 hours,
the latitude is 63¼ degrees.
Find a point in the ecliptic half so many degrees distant from the
solstitial point, as there are days given, and bring that point to the
meridian; then keep the globe from turning round its axis, and move
the meridian up or down until the aforesaid point of the ecliptic
comes to the horizon; that elevation of the Pole will be the latitude
required.
If the days given were 78, the latitude is 71½ degrees.
This method is not accurate, because the degrees in the ecliptic
do not correspond to natural days; and also because the Sun does
not always move in the ecliptic at the same rate; however, such
problems as these may serve for amusements.
Bring the Sun’s place to the meridian, and mark the number of
degrees contained betwixt that point and the equator; then count
the same number of degrees from the nearest Pole (viz. the North
Pole, if the Sun’s declination is Northerly, otherwise the South Pole)
towards the equator, and note that point upon the meridian; then
turn the globe about, and all the places which pass under the said
point, are those where the Sun begins to shine constantly, without
setting on the given day. If you lay the same distance from the
opposite Pole towards the equator, and turn the globe about, all the
places which pass under that point, will be those where the longest
night begins.
The Latitude of the place being given, to find the hour
of the day when the Sun shines.
If it be in the summer, elevate the Pole according to the latitude,
and set the meridian due North and South; then the shadow of the
axis will cut the hour on the Dial plate: For the globe being rectified
in this manner, the hour circle is a true Equinoctial Dial; the axis of
the globe being the Gnomon. This holds true in Theory, but it might
not be very accurate in practice, because of the difficulty in placing
the horizon of the globe truly horizontal, and its meridian due North
and South.
If it be in the winter half year, elevate the South Pole according
to the latitude North, and let the North part of the horizon be in the
South part of the meridian; then the shade of the axis will show the
hour of the day as before: But this cannot be so conveniently
performed, tho’ the reason is the same as in the former case.
To find the Sun’s altitude, when it shines, by the Globe.
Having set the frame of the globe truly horizontal or level, turn
the North Pole towards the Sun, and move the meridian up or down
in the notches, until the axis casts no shadow; then the arch of the
meridian, contained betwixt the Pole and the horizon, is the Sun’s
altitude.
Note, The best way to find the Sun’s altitude, is by
a little quadrant graduated into degrees, and having
sights and a plummet to it: Thus, hold the quadrant in
your hand, so as the rays of the Sun may pass through
both the sights, the plummet then hanging freely by
the side of the instrument, will cut in the limb the
altitude required. These quadrants are to be had at the
instrument-makers, with lines drawn upon them, for
finding the hour of the day, and the azimuth; with
several other pretty conclusions, very entertaining for
beginners.
The Latitude and the Day of the Month being given, to
find the hour of the day when the Sun shines.
Having placed the wooden frame upon a level, and the meridian
due North and South, rectify the globe for the latitude, and fix a
needle perpendicularly over the Sun’s place: The Sun’s place being
brought to the meridian, set the hour index at 12 at noon, then turn
the globe about until the needle points exactly to the Sun, and casts
no shadow, and then the index will shew the hour of the day.
Having rectified the globe for the latitude, the zenith, and the
Sun’s place, turn the globe and the quadrant of altitude, so that the
Sun’s place may cut the given degree of altitude: then the index will
show the hour, and the quadrant will cut the azimuth in the horizon.
Thus, if at London, on the 21st of August, the Suns altitude, be 36
degrees in the forenoon, the hour of the day will be IX, and the
Sun’s azimuth about 58 degrees from the South part of the meridian.
The Sun’s Azimuth being given, to place the Meridian
of the Globe due North and South, or to find a
Meridian Line when the Sun shines.
Let the Sun’s azimuth be 30 degrees South-Easterly, set the
horizon of the globe upon a level, and bring the North Pole into the
zenith; then turn the horizon about until the shade of the axis cuts
as many hours as is equivalent to the azimuth (allowing 15 degrees
to an hour) in the North-West part of the hour circle, viz. X at night,
which being done, the meridian of the globe stands in the true
meridian of the place. The globe standing in this position, if you
hang two plummets at the North and South points of the wooden
horizon, and draw a line betwixt them, you will have a meridian line;
which if it be on a fixed plane (as a floor or window) it will be a
guide for placing the globe due North and South, at any other time.
Rectify the globe for the latitude, the zenith, and the Sun’s place,
then the number of degrees contained betwixt the Sun’s place and
the vertex, is the Sun’s meridional zenith distance; the complement
of which to 90 degrees, is the Sun’s meridian altitude. If you turn
the globe about until the index points to any other given hour, then
bringing the quadrant of altitude to cut the Sun’s place, you will have
the Sun’s altitude at that hour; and where the quadrant cuts the
horizon, is the Sun’s azimuth at the same time. Thus May the 1st at
London, the Sun’s meridian altitude will be 61½ degrees; and at 10
o’clock in the morning, the Sun’s altitude will be 52 degrees, and his
azimuth about 50 degrees from the South part of the meridian.
Having rectified the globe for the latitude, the zenith, and the
Sun’s place, take a point in the ecliptic exactly opposite to the Sun’s
place, and find the Sun’s altitude and azimuth, as by the last
problem, and these will be the depression and the altitude required.
Thus, if the time given be the 1st of December, at 10 o’clock at
night, the depression and azimuth will be the same as was found in
the last problem.
Rectify the globe for the latitude, the zenith, and the Sun’s place,
then put the quadrant of altitude to the Sun’s azimuth in the horizon,
and turn the globe ’till the Sun’s place meet the edge of the
quadrant, then the said edge will shew the altitude, and the index
point to the hour. Thus, May the 21st at London when the Sun is due
East, his altitude will be about 24 degrees, and the hour about VII in
the morning; and when his azimuth is 60 degrees South-Westerly,
the altitude will be about 44½ degrees, and the hour about 2¾ in
the afternoon.
Thus, the latitude and the day being known, and having besides
either the altitude, the azimuth, or the hour; the other two may be
easily found.
Rectify the globe for the latitude and zenith, and set the edge of
the quadrant to the given azimuth; then turning the globe about,
that point of the ecliptic which cuts the altitude, will be the Sun’s
place. Keep the quadrant of the altitude in the same position, and
having brought the Sun’s place to the meridian, and the hour index
to 12 at noon, turn the globe about ’till the Sun’s place cuts the
quadrant of altitude, and then the index will point the hour of the
day.
Bring the Sun’s place in the ecliptic to the meridian; then that
degree of the equator, which is cut by the meridian, will be the Sun’s
Right Ascension; and that degree of the meridian, which is exactly
over the Sun’s place, is the Sun’s Declination.
After the same manner, bring the place of any Fixed Star to the
meridian, and you will find its Right Ascension in the equinoctial, and
Declination of the meridian.
Thus, the right ascension and declination is found, after the same
manner as the longitude and latitude of a place upon the Terrestrial
Globe.
Note, The right ascension and declination of the
Sun vary every day; but the right ascension, &c. of the
Fixed Stars is the same throughout the year[6].
The Sun’s Right Ascension. Declin.
Deg. Deg.
January 31 314 17⅓ S.
April 5 14¼ 6 N.
Thus on
July 21 120¼ 20½ N.
November 26 242¼ 21 S.
R. Asc. Dcl.
Deg. Deg.
Aldebaran 65 16 N.
Spica Virginis 197¾ 9¾ S.
Capella 74 45⅔
Syrius, or the Dog-Star 98¼ 16⅓
Note, The declination of the Sun may be found
after the same manner by the Terrestrial Globe, and
also his right ascension, when the equinoctial is
numbered into 360 degrees, commencing at the
equinoctial point ♈ : But as the equinoctial is not
always numbered so, and this being properly a
Problem in Astronomy, we choose rather to place it
here.
By the converse of this problem, having the right ascension and
declination of any point given, that point itself may be easily found
upon the globe.
Having rectified the globe for the latitude, the zenith, and the
Sun’s place, turn the globe about until the index points to the given
hour, the globe being kept in this position.
All those Stars that are in (Eastern/Western) side of the horizon,
are then (Rising/Setting).
All those Stars that are under the meridian, are then culminating.
And if the quadrant of altitude be laid over the center of any
particular Star, it will show that Star’s altitude at that time; and
where it cuts the horizon, will be the Star’s azimuth from the North
or South part of the meridian.
The globe being kept in the same elevation, and from turning
round its axis, move the wooden frame about until the North and
South points of the horizon lie exactly in the meridian; then right
lines imagined to pass from the center thro’ each Star upon the
surface of the globe, will point out the real Star in the heavens,
which those on the globe are made to represent. And if you are by
the side of some wall whose bearing you know, lay the quadrant of
altitude to that bearing in the horizon, and it will cut all those Stars
which at that very time are to be seen in the same direction, or close
by the side of the said wall. Thus knowing some of the remarkable
Stars in any part of the heavens, the neighbouring Stars may be
distinguished by observing their situations with respect to those that
are already known, and comparing them with the Stars drawn upon
the globe.
Thus, if you turn your face towards the North, you will find the
North Pole of the globe points to the Pole Star; then you may
observe two Stars somewhat less bright than the Pole Star, almost in
a right line with it, and four more which form a sort of quadrangle;
these seven Stars make the constellation called the Little Bear; the
Pole-Star being in the tip of the tail. In this neighbourhood you will
observe seven bright Stars, which are commonly called Charles’s
Wane; these are the bright Stars in the Great Bear, and form much
such another figure with those before-mentioned in the little Bear:
The two foremost of the square lie almost in a right line with the
Pole Star, and are called the Pointers, so that knowing the Pointers,
you may easily find the Pole Star. Thus the rest of the Stars in this
constellation, and all the Stars in the neighbouring constellations
may be easily found, by observing how the unknown Stars lie either
in quadrangles, triangles, or strait lines from those that are already
known upon the globe.
After the same manner the globe being rectified, you may
distinguish those Stars that are to the Southward of you, and be
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
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.
ebookultra.com