Introducing Functional Programming Using C#: Leveraging A New Perspective For OOP Developers 1st Edition Vaskaran Sarcar Download PDF
Introducing Functional Programming Using C#: Leveraging A New Perspective For OOP Developers 1st Edition Vaskaran Sarcar Download PDF
com
https://ebookmeta.com/product/introducing-functional-
programming-using-c-leveraging-a-new-perspective-for-oop-
developers-1st-edition-vaskaran-sarcar/
OR CLICK BUTTON
DOWNLOAD NOW
https://ebookmeta.com/product/simple-and-efficient-programming-with-c-
vaskaran-sarcar/
ebookmeta.com
https://ebookmeta.com/product/test-your-skills-in-c-programming-
review-and-analyze-important-features-of-c-1st-edition-vaskaran-
sarcar/
ebookmeta.com
https://ebookmeta.com/product/java-design-patterns-a-tour-of-23-gang-
of-four-design-patterns-in-java-1st-edition-vaskaran-sarcar/
ebookmeta.com
https://ebookmeta.com/product/interviewing-a-guide-for-journalists-
and-professional-writers-3rd-edition-gail-sedorkin/
ebookmeta.com
A Bit of a Bite Mob Ties Book 4 1st Edition Milly Taiden
https://ebookmeta.com/product/a-bit-of-a-bite-mob-ties-book-4-1st-
edition-milly-taiden/
ebookmeta.com
https://ebookmeta.com/product/teens-guide-to-college-career-
planning-12th-edition-justin-ross-muchnick/
ebookmeta.com
https://ebookmeta.com/product/cardiac-electrosonography-1st-edition-
eyal-herzog-david-leibowitz-yair-elitzur/
ebookmeta.com
Health and Health Care Concerns among Women and Racial and
Ethnic Minorities 1st Edition Jennie Jacobs Kronenfeld
https://ebookmeta.com/product/health-and-health-care-concerns-among-
women-and-racial-and-ethnic-minorities-1st-edition-jennie-jacobs-
kronenfeld/
ebookmeta.com
https://ebookmeta.com/product/jarviss-health-assessment-physical-
examination-carolyn-jarvis-ann-eckhardt/
ebookmeta.com
Hidden History Of American Healthcare 1st Edition Hartmann
https://ebookmeta.com/product/hidden-history-of-american-
healthcare-1st-edition-hartmann/
ebookmeta.com
Introducing
Functional
Programming
Using C#
Leveraging a New Perspective for
OOP Developers
—
Vaskaran Sarcar
Introducing Functional
Programming Using C#
Leveraging a New Perspective
for OOP Developers
Vaskaran Sarcar
Introducing Functional Programming Using C#: Leveraging a New Perspective for
OOP Developers
Vaskaran Sarcar
Kolkata, West Bengal, India
Introduction�����������������������������������������������������������������������������������������������������������xvii
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
viii
Table of Contents
ix
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 297
x
About the Author
Vaskaran Sarcar obtained his master’s degree in software
engineering from Jadavpur University, Kolkata (India),
and his master’s of computer application from Vidyasagar
University, Midnapore (India). He was a National Gate
Scholar (2007–2009) and has more than 12 years of
experience in education and the IT industry. He devoted
his early years (2005–2007) to the teaching profession at
various engineering colleges, and later he joined HP India
PPS R&D Hub in Bangalore. He worked at HP until August
2019. At the time of his retirement from HP, he was a senior software engineer and team
lead. Vaskaran is following his passion and is now a full-time author. You can find him on
LinkedIn at https://www.linkedin.com/in/vaskaransarcar and see all of his books at
https://amazon.com/author/vaskaran_sarcar.
xi
About the Technical Reviewers
Leandro Fernandes Vieira is a senior software engineer
currently working for a leading payment solutions company.
He earned his degree in system analysis and development
from São Paulo State Technological College (FATEC-SP),
Brazil. His realm of expertise includes the .NET stack and
the C# and F# programming languages. He has a passion
for programming and algorithms and likes to contribute
to open-source projects; in fact, he is a creator of the
RecordParser project, one of the fastest CSV parsers for .NET.
He enjoys spending time with his family, walking in the park, hitting the gym, and
listening to heavy-metal music.
You can reach him at https://github.com/leandromoh/.
xiii
Acknowledgments
I thank the Almighty. I sincerely believe that only with His blessings could I complete
this book. I also extend my deepest gratitude and thanks to the following people:
• Leandro Fernandes Vieira and Paul Louth: They allowed me to use
the Curryfy library and language-ext library in this book. Leandro
also joined the technical review team and provided many useful
suggestions and improvements for this book.
• Shon, Kim, Nagarajan, and Vinoth: Thanks to each of you for your
exceptional support to improve my work.
Finally, I thank those people from the functional programming community who have
shared their knowledge through online blogs, articles, courses, and books.
xv
Introduction
Throughout the ages, prophets have suggested that most of us are not reaching our full
potential. If you look at the great achievers in any field in the current world, you will find
that they are hard workers, and they strive to keep improving. They put in extra effort to
improve their skills, and in many cases, they even hire coaches to learn new techniques.
Then, one day, they discover that all their hard work starts to pay off: they become
masters in their chosen field.
The following quote from the Chinese philosopher Confucius perfectly
summarizes this:
The will to win, the desire to succeed, the urge to reach your full
potential…these are the keys that will unlock the door to personal excellence.
Now let’s apply this philosophy to programming. As a developer, are you reaching
your full potential with C#? I may not know your reply, but I certainly know my answer.
Even after working with C# for more than 14 years, there is still more to learn.
One evening I asked myself, how could I improve my C# skills? I could continue
to try to learn new features and practice them, but intuitively, I knew there was an
alternative answer. So, I started searching for tips and eventually discovered that most of
the time I was using C# for object-oriented programming (OOP). Indeed, it is a perfect
fit for OOP, and there is nothing wrong with this tendency. But what about functional
programming (FP) using C#? It’s not that I never used it (in fact, C# developers are very
much familiar with LINQ), but I was not very conscious of it. So, I keep browsing through
various resources, such as books, articles, and online courses. Eventually, I discovered
that during its development, C# started embracing functional features too, and as a
result, it has become a powerful hybrid language.
I became very interested in the topic and tried to learn more about it. From this
time onward, I started facing challenges. There were some good resources, but I could
not stitch them together to serve my needs. This is why I started documenting my notes
when I was experimenting with using C# in a functional way. This book is a result of
those efforts.
xvii
Introduction
So, welcome to your journey through Introducing Functional Programming Using C#:
Leveraging a New Perspective for OOP Developers.
C# is a powerful programming language, is well accepted in the programming world,
and helps you make a wide range of applications. These are the primary reasons it is
continuously growing in popularity and is always in high demand. So, it is not a surprise
that existing and upcoming developers (for example, college students and programming
lovers) are curious to learn C# and want to create their applications using it.
Many developers try to learn it in the shortest possible time frame and then claim
they know C# well. In fact, many resources claim you can unlock the real power of C# in
a day, a week, or a month. But is this true? I think not. Remember, I’m 14 years in and I’m
still learning.
Malcolm Gladwell’s 10,000-hour rule says that the key to achieving world-class
expertise in any skill is, to a large extent, a matter of practicing the correct way, for a total
of around 10,000 hours. So, even though we may claim that we know something very
well, we actually know very little. Learning is a continuous process, with no end to it.
Then should we stop learning? Definitely, the answer is no. There is something called-
effective learning. It teaches you how to learn fast to serve your need. This is the context
where I like to remind you about the Pareto principle or 80-20 rule. This rule simply states
that 80% of outcomes come from 20% of all causes. This is useful in programming too.
When you truly learn the fundamental aspects of FP, you can use it effectively to improve
your code. Most importantly, your confidence level will raise to a level from where you
can learn more easily. This book is for those who acknowledge this fact. It helps you to
understand the core principles of FP with plenty of Q&A sessions and exercises.
• Part I consists of the first three chapters, which start with an overview
of functional programming (FP). Then we’ll discuss functions and
immutability in depth. These are the building blocks for FP and what
you need to understand to move on to Part II of this book.
• C# is a multiparadigm language, and Part II reveals its potential.
This part will cover how to harness the power of FP. In addition,
two well-known external libraries, called Curryfy and language-ext,
xviii
Other documents randomly have
different content
be said when the Service ended there and the Amidah was
considered optional (רשות).
The Sermon has lost much of its original force and influence. The
cause of this fact is probably to be sought chiefly in the
[449]materialism and scepticism of the age, but to some degree also
in the character of the sermon. It cannot be denied that the pulpit,
instead of being made a place from which Love of Torah and Fear of
God receive life, encouragement, and strength, is frequently turned
into a platform for discussing communal or personal quarrels or
theological controversies, or creating a discontent with existing
institutions, without sufficiently considering the result of such
discontent. Themes like these are not outside the province of the
preacher, but they must not be the staple of his discourses, which
must principally seek to foster אהבת תורה ויראת שמיםin the hearts of
the congregants. As to the history and literature of this branch of the
Service, see Zunz, Die Gottesdienstlichen Vorträge der Juden.
Berlin, 1832.
5. On Page 420.
6. On Page 420.
(3.) The Ritual contains many sections which owe their existence to
particular circumstances that have passed away, and to local
conditions which are different from those prevailing in the countries
in which we live. Have these a right to be kept perpetually in the
Ritual? Certainly not. There is no reason why prayers which have
become obsolete and meaningless should not be modified or
discontinued. But as a rule our prayers are free from references to
the particular causes of their composition, and there is no need to
expunge from the Service petitions, thanksgivings, or praises which
were originally intended for a special occasion, if they are expressed
in general terms, and have become in the Synagogue a source of
devotion and edification. But as to the latter condition, it is difficult to
decide whether a liturgical composition has become, and is still, an
aid to devotion. Much depends on the individual character of the
particular congregation in which the question has been raised, and
each case should be decided on its own merits by a competent and
responsible authority.
Many of the Piyyutim 138 and Selichoth belong to this class; also a
few sections in the ordinary Ritual (see note 2), and the repetition
[453]of the Amidah, and the Kiddush in the Synagogue on the eve of
Sabbaths and Festivals.
(5.) We hear frequently the complaint that the Public Service is too
long. This complaint is of a relative character; it is different from the
feeling of joy expressed in the words, “Blessed are those who dwell
in thy house;” it is different from the sentiment of those chasidim
(Mishnah, Berachoth v. 1) who sat still a while before the
commencement of the prescribed prayer, or those who after the
conclusion thereof sit down again, saying, “Surely the righteous shall
give thanks to thy name; the upright shall dwell in thy presence” (Ps.
cxl. 14). As regards the length of the Service, we should bear in mind
the principle of our Sages: It makes no difference whether the
Service is long or short: only be devout. It is provocative of
irreverence to protract the Service unnecessarily until it becomes
wearisome; but it is equally unbecoming to hurry over it as though it
were an unpleasant task. On the whole the Services, especially
when they are well regulated, are not too long, unless too much time
be spent in singing or in unnecessary interruptions.
In all cases in which a modification seems advisable and lawful it
must be borne in mind that the Ritual is a Sanctuary every element
of which is holy, and that hasty reforms may be less effective than is
hoped. It may perhaps be easy to pull down, but it is not so easy to
build up. Devout members of a congregation may easily be
alienated, but not so easily will new members be attracted, or if
attracted, permanently retained.
7. On Page 424.
“Thou shalt not eat any abominable thing” (Deut. xiv. 3); that is,
according to our traditional explanation, everything that the Word of
God declares to be abominable (Sifre, ad locum). One of the
sections of the Dietary Laws concludes thus: “For I am the Lord that
brought you up out of the land of Egypt, to be your God: ye shall
therefore be holy, for I am holy” (Lev. xi. 45).
But what difference can it make to the Almighty whether we eat this
or that? Surely it makes no difference to the Almighty; but we have
faith in His Goodness and Wisdom, and are convinced that He
knows by what means we may best attain to that [457]holiness which
we are so frequently exhorted to seek, and that the Divine Laws
which He revealed to us for this very purpose show the shortest and
the safest road to this aim.
With the following exceptions, the Dietary Laws concern only animal
food:—
(1.) ערלה“Forbidden fruit,” i.e., the fruit of a tree during the first three
years after its planting (Lev. xix. 23).—The fruit of the fourth year
(נטע רבעי) was formerly, in the time of the Temple, brought to
Jerusalem, and consumed there amidst praises and thanksgiving to
Him who is the source of all blessing (ibid. v. 24). Those who lived
far from Jerusalem were allowed to redeem the fruit of the fourth
year with silver, and to spend the latter in the holy city.
(2.) חדש“New corn.”—The Omer of barley offered on the second day
of Passover is called “the first of your harvest” (Lev. xxiii. 10), and it
was enjoined, “Ye shall eat neither bread, nor parched corn, nor
fresh ears, 140 until this selfsame day, until ye have brought the
oblation of your God” (ibid. 14).
These two laws (ערלהand חדש) seem to have their source in the
dictum, “The first of the first-fruits of thy ground thou shalt bring into
the house of the Lord thy God” (Exod. xxiii. 19).
(3.) כלאים.—Mixture of different kinds. “Thou shalt not sow thy field
with two kinds of seed” (Lev. xix. 19). 141 “Thou shalt not sow thy
vineyard with [458]two kinds of seed” (Deut. xxii. 9). In the former
case only the sowing of divers kinds is prohibited, but the produce of
such sowing is not forbidden; in the latter case, if the law is
transgressed, the produce of both the vine and the seed is not to be
used for any purpose whatever (אסור בהנאה), for the law is followed
by the words, “lest the whole fruit be forfeited, the seed which thou
hast sown and the increase of the vineyard.”
Although these and similar 142 precepts are introduced by the words
“Ye shall keep my statutes (חקתי),” and no reason is given for the
enactment of these statutes, it seems, from the position occupied by
these laws in a section of moral precepts, that they serve as
reminders of the important lesson that our conduct should be
regulated by the principles of contentment and simplicity of life,
principles which are the best safeguard against undue desire for
luxury and superfluity. The prohibition of sowing divers kinds of seed
further reminds us of the importance of preserving our heart in a
state of simplicity and purity; that twofold weights, twofold measures,
and especially a twofold heart are an abomination to the Lord.
(a.) To cut off a piece of flesh from a living animal for our food (אבר מן
החי“a limb of a living animal”).
(b.) To kill the parent with its young on the same day (Lev. xxii. 28;
comp. Deut. xxii. 6).
(c.) To give unnecessary pain to the animal in killing it. The various
regulations for the lawful killing of animals, שחיטה, handed down by
Tradition as Mosaic, הלכה למשה מסיני, are not only in harmony with
this principle, but seem in many instances to have been dictated by
it.
(d.) To eat the blood of beasts and birds (Lev. xvii. 12, 14). The blood
contained in the meat is removed as far as possible by having the
meat soaked in water for half-an-hour, and then kept covered with
salt for an hour, the salt being again removed by rinsing. This
process is called kasher; that is, preparing the meat so as to make it
kasher (כשר“fit for food”).
2. The flesh of beasts and birds that have died from any other cause
than having been killed in the manner prescribed is forbidden. The
flesh of animals that have been killed in the prescribed manner, but
are found to have been affected with some dangerous disease, is
also forbidden as t’refah (טרפה). 143
(a.) With regard to cattle and beasts, the rule is given, “Whosoever
parteth the hoof and is cloven-footed and cheweth the cud, that you
may eat” (Lev. xi. 3).—The clean cattle (בהמה טהורה) and the clean
beasts (חיה טהורה) are enumerated in Deut. xiv. 4 and 5 respectively.
(c.) Fish that have scales and fins are permitted; others—e.g., the
eel—are “unclean” (ibid. xi. 9–12).
(d.) “All winged animals that creep (שרץ העוף), going upon all four,
shall be an abomination unto you. Yet these may ye eat of, every
flying creeping thing that goeth upon all four, which have legs above
their feet, to leap withal upon the earth; even these of them ye may
eat; the arbeh with its kind, and the soleam with its kind, the chargol
with its kind, and the chagabh with its kind” (ibid. 20–22). These are
certain kinds of locusts that satisfy the above condition. (Comp.
Maimonides, Mishneh-torah, Hilchoth maachaloth asuroth i. 21–23.)
(e.) “And every creeping thing that creepeth upon the earth shall be
an abomination; it shall not be eaten” (Lev. xi. 41). In this prohibition
are included all kinds of worms such as are found in fruit, and mites,
snails, oysters, lobsters, crabs, &c. [461]
4. The milk of “unclean” cattle or beasts (e.g., asses’ milk), the eggs
of “unclean” birds, and the roe of “unclean” fish (e.g., caviare
prepared of the roe of the sturgeon) are likewise forbidden.
7. “Thou shalt not seethe the kid in its mother’s milk” (Exod. xxiii. 19).
Tradition explains this law as forbidding all mixture of meat and milk
(בשר בחלב). In its literal sense the verse in which this law is
[462]mentioned seems to point to the duty of self-restraint, as if to tell
us that we should not greedily devour the first ripe fruit, or the young
immediately after their birth.
The significance of the law may be learnt from the fact that it is
mentioned three times in the Pentateuch. Hence the strictness with
which this commandment is observed in Jewish homes. In a Jewish
household, established in accordance with Jewish law and tradition,
there are two separate sets of utensils, the one to be used for the
preparation of meat-diet, the other for the preparation of milk and
butter diet.
The flesh of fish is not considered as meat in this respect, nor are
fish subject to the laws of shechitah.
[Contents]
Notes.
1. On Page 459—1a.
This prohibition is one of the שבע מצות בני נח“the seven Noachide
commandments,” i.e., commandments which, according to Tradition,
were already in force in the days of Noah, and are binding on all his
children or all mankind, and not on the Israelites alone; viz., (1) the
prohibition of idolatry (עבודה זרה), (2) of murder (שפיכות דם), (3) of
adultery (גלוי עריות), (4) of blasphemy (ברכת השם), (5) of robbery
(גזל), (6) of eating meat taken from a living animal (אבר מן החי), and
(7) the institution of courts of justice (דינין). See Babyl. Talm.,
Sanhedrin 56a. [463]
2. On Page 459—1c.
Any deviation from these rules in the act of killing the animal renders
the shechitah unlawful (פסולה). The killing of cattle, beasts, or fowl
for food is therefore entrusted only to such persons as possess a
knowledge of the rules of shechitah and are skilled and trustworthy.
The beth-din or the Chief-Rabbi decides whether a person has duly
qualified himself for the office of shochet (שוחט), and whether he may
kill animals for kasher food.
It is the duty of the shochet to examine the animal before killing it,
and to satisfy himself that it is not in a dying condition; after the
shechitah of any cattle or beast (בהמהor חיה) he must examine the
lungs to assure himself that they are in a normal condition. If he finds
them in a diseased state he declares the flesh of the animal as
t’refah (טרפה) and unfit for food. In the case of poultry the
examination is not made by the shochet; but if any deviation from the
normal state is discovered in the lungs or in any other part, the meat
must not be used as food unless the animal has been examined by a
competent person (Rabbi or dayyan), and declared by him to be
kasher.
(a.) The meat is first soaked in water for half-an-hour; this [464]must
be done within three days after the killing of the animal. It is then
taken out of the water, placed on a slanting board or in a wicker-
basket, and after a few minutes, when the water has sufficiently run
off, sprinkled on all sides with salt. After the lapse of an hour the salt
is again removed by soaking and rinsing.
(b.) In case of urgency, when, e.g., the meat is wanted for a patient,
or when on Friday afternoon there would not be left time enough for
cooking or roasting the meat, it need only be kept in water for fifteen
minutes and in salt for half-an-hour.
(c.) In kashering poultry all the inner parts of the animal must be
taken out and salted separately; the rest must be sprinkled with salt
both within and without.
(d.) Liver is salted a little and roasted on fire, not in the oven or in
any vessel. This done, the liver may be cooked or roasted in any
way.
(e.) The heart is cut open before the salting, and a piece is cut off at
the apex, in order that the blood may run off more easily.—For the
same purpose the horny part of the legs is cut off.—The head must
be opened and sprinkled with salt on both sides, after the brain has
been taken out; from the latter the skin is drawn off, and then it is
salted.
(f.) Eggs found in poultry are treated as meat, but must be salted
separately.
(g.) The vessels used for soaking and salting the meat should not be
used for other purposes.
4. On Page 459—1c.
The law of shechitah applies only to cattle, beasts, and birds (בהמה,
חיהand עוף); there is no commandment as regards the killing of fish.
Tradition supports this exception by reference to the distinction made
in Num. xi. 22: “Shall the flocks and the herds be slain (ישחט) for
them, to suffice them? or shall all the fish of the sea be gathered
together (יאסף) for them, to suffice them?” Although the latter term
(יאסף) is also used of quails (ibid. 32), no such inference is made
with regard to birds, because in ver. 32 the term יאסףis not
contrasted by ישחט; besides, the verb שחטis frequently applied in
the Law to birds, but never to fish. Comp. Babyl. Talm., Chullin, p.
27b.
5. On Page 460—3b.
Comp. Mishnah, Treatise Chullin, iii. 6.—The characteristics of the
clean and the unclean cattle and beasts are given in the Pentateuch,
but the characteristics of the clean or the unclean birds are not
mentioned in the Law. Our Sages, however, declared every bird of
prey—or every bird that seizes its food with its claws, and lifts it up
from the ground before eating it (Rashi) to be an unclean bird; while
all birds are clean that have a projecting claw (אצבע יתרהa claw
longer than the rest) and a crop (זפק), and whose stomach has a
membrane that can easily be peeled off (קרקבנו נקלף). Another rule is
this: Birds that dwell and associate with unclean birds are unclean.
“Not without reason does the starling go to the raven: they are of the
same species” (Babyl. Talm., Chullin 65b). The sentence serves also
as a moral lesson (ibid., Baba Kama 92a). At present, however, only
those birds are killed for food which are known traditionally to have
always been considered as “clean birds” (Maimonides, Mishneh-
torah, Hilchoth maachaloth asuroth, i. 14–20; Tur Joredeah, chap.
lxxxii.).
6. On Page 461—7.
7. On Page 461—4.
The honey of bees is an apparent exception from the rule that the
products of the unclean animals are forbidden. The honey mentioned
in the Bible is mostly the juice of fruit, especially of dates; but the
honey of bees is also mentioned (Judges xiv. 8). It is assumed that
honey does not contain any part of the bee itself, but is merely the
juice of the flowers sucked and again discharged by the bees. (See
Babyl. Talm., Bechoroth 7b; Maim., Mishneh-torah, Hil. maachaloth
asuroth iii. 3.)
Note 8.
It has, however, not lapsed in its entirety. It is only the אסור הנאהthat
is no longer in force. The prohibition of using wine prepared by non-
Jews (סתם יין) as a beverage still continues. This and similar
prohibitions were intended as a barrier against the increase of mixed
marriages among the Jews. (Comp. Babyl. Talm., Shabbath 17b.)
[467]