Introducing Functional Programming Using C Leveraging a New Perspective for OOP Developers 1st Edition Vaskaran Sarcar instant download
Introducing Functional Programming Using C Leveraging a New Perspective for OOP Developers 1st Edition Vaskaran Sarcar instant download
https://ebookname.com/product/introducing-functional-programming-
using-c-leveraging-a-new-perspective-for-oop-developers-1st-
edition-vaskaran-sarcar/
https://ebookname.com/product/functional-javascript-introducing-
functional-programming-with-underscore-js-1st-edition-michael-
fogus/
https://ebookname.com/product/introducing-windows-7-for-
developers-1st-edition-yochay-kiriaty/
https://ebookname.com/product/introducing-microsoft-visual-
basic-2005-for-developers-sean-campbell/
https://ebookname.com/product/paradise-lost-state-failure-in-
nepal-1st-edition-subho-basu/
Understanding Art 10th Edition Lois Fichner-Rathus
https://ebookname.com/product/understanding-art-10th-edition-
lois-fichner-rathus/
https://ebookname.com/product/high-throughput-screening-in-
chemical-catalysis-technologies-strategies-and-applications-1st-
edition-alfred-hagemeyer/
https://ebookname.com/product/vascular-medicine-a-companion-to-
braunwald-s-heart-disease-expert-consult-online-and-print-1e-1st-
edition-mark-a-creager/
https://ebookname.com/product/meningiomas-expert-consult-online-
and-print-expert-consult-title-online-print-1-har-psc-edition-m-
necmettin-pamir/
https://ebookname.com/product/this-bright-light-of-ours-stories-
from-the-voting-rights-fight-1st-edition-maria-gitin/
Mechanics of Materials SI Edition James M. Gere
https://ebookname.com/product/mechanics-of-materials-si-edition-
james-m-gere/
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
Introduction
are discussed in this part. The first one is used in Chapter 5 when I
discuss currying. The second one is used in Chapter 8 and Chapter 9
when I discuss functional error handling and the Monad pattern.
The best way to learn something is by analyzing case studies, asking questions
about any doubts you have, and doing exercises. So, throughout this book, you will see
interesting code snippets, “Q&A Sessions,” and exercises. Each question in the “Q&A
Sessions” sections is marked with <chapter_no>.<Question_no>. For example, 5.3 means
question 3 from Chapter 5. You can use the simple exercises to evaluate your progress.
Each question in these exercises is marked with E<chapter_no>.<Question_no>. For
example, E6.2 means exercise 2 from Chapter 6.
The code examples and questions and answers (Q&As) are straightforward. I believe
that by analyzing these Q&As and doing the exercises, you can verify your progress.
They are presented to make your future learning easier and more enjoyable, but most
importantly, they will help you become confident as a developer.
You can download all the source code of the book from the publisher’s website,
where you can also find an errata list for the book. I suggest that you visit that website to
receive any important corrections or updates.
Prerequisite Knowledge
The target readers of this book are those who want to make the most of C# by harnessing
the power of functional programming. I expect you to be familiar with .NET, C#, and
OOP concepts. In fact, knowing about some advanced concepts such as delegates
and lambda expressions can accelerate your learning. I assume that you know how to
compile or run a C# application in Visual Studio. This book does not invest time in easily
available topics, such as how to install Visual Studio on your system, how to write a
“Hello World” program in C#, and so forth. Though I have used C# as the programming
language, if you are familiar with a similar language like Java, you can apply that
understanding to this book.
xix
Introduction
• Are you familiar with .NET, C#, and basic object-oriented concepts
such as polymorphism, inheritance, abstraction, and encapsulation?
• Are you interested in knowing how the core constructs of C# can help
you in FP?
You probably shouldn’t pick this book if the answer is “yes” to any of the following
questions:
Useful Software
These are the important tools that I use in this book:
• While writing this book, I had the latest edition of Visual Studio
Community 2022 (64-bit, version 17.5.4). All the programs were
tested with C# 11 and .NET 7.
xx
Introduction
• The community edition is free of cost. If you do not use the Windows
operating system, you can still use the free Visual Studio Code, which
is a source-code editor developed by Microsoft to support Windows,
Linux, or Mac operating systems. At the time of this writing, Visual
Studio 2022 for Mac is also available, but I did not test my code on it.
• This book suits you best if you are familiar with some advanced
features in C# such as delegates and lambda expressions. If not,
please read about these topics before you start reading this book.
• The code in this book should give you the expected output in future
versions of C#/Visual Studio as well. Though I believe that the results
should not vary in other environments, you know the nature of
software: it is naughty. So, I recommend that if you want to see the
exact same output as in the book, you mimic the same environment.
• You can download and install the Visual Studio IDE from https://
visualstudio.microsoft.com/downloads/ (see Figure I-1).
xxi
Introduction
Figure I-1. Download link for Visual Studio 2022, Visual Studio for Mac, and
Visual Studio Code
Note At the time of this writing, this link works fine and the information is
correct. But the link and policies may change in the future. The same comment
applies to all the links mentioned in this book.
Source Code
All the source code used in this book can be found at https://github.com/apress/
introduction-functional-programming-cs.
xxii
Introduction
I’ve used top-level statements heavily in this book. Consequently, there is no need
for me to explicitly write the Main method for console applications. You understand that
using this technique, I minimized the code lengths. When you use top-level statements,
the C# compiler does the necessary job on your behalf in the background. Top-level
statements have been supported since C# 9.0.
I also like to add that I enabled implicit usings for my C# projects. The implicit
usings feature automatically adds common global using directives for the type of
project you are building. Starting from C#10.0, this feature is also supported. Otherwise,
I had to add the necessary directives to my programs manually.
Finally, all the output/code in the book uses the same font and structure. To draw
your attention in some places, I have used bold fonts. For example, consider the
following output fragment (taken from Chapter 3 where I discuss external immutability):
Final Words
Congratulations, you have chosen a programming language to experiment with a
paradigm that will assist you throughout your career. As you learn and review these
concepts, I suggest you write your code instead of copying and pasting it; there is no
better way to learn.
Upon completing this book, you’ll be confident about FP and the value it
provides you.
xxiii
PART I
In brief, these are the building blocks for FP and the foundations you’ll need to
understand before reading Part II of this book.
CHAPTER 1
Functional Programming
Overview
You can reach a destination using different vehicles. If the destination is well connected
to transportation routes, you can use a car, a bus, a train, or an airplane. If the destination
is nearby, you may opt for a bicycle. If you are health conscious, you may prefer to walk.
This is simple to understand. Now think about some special scenarios: you need to
reach your destination as soon as possible, but it is not a nearby location. Are you going
to walk? The answer is no. In a situation like this, you will want to use a vehicle to reach
your destination faster. But when you are not in a hurry or want to avoid a crowded bus,
you may prefer to walk. Depending on the context, one approach might be a better fit
compared to others.
The programming world is no different. You can use different programming styles
to create the same application. Each approach has its pros and cons. Based on the given
constraints in an application, you might prefer one approach over another. If you are
aware of multiple routes, you can choose the approach that suits your needs best.
3
© Vaskaran Sarcar 2023
V. Sarcar, Introducing Functional Programming Using C#, https://doi.org/10.1007/978-1-4842-9697-4_1
Chapter 1 Functional Programming Overview
So, using C#, you can also combine different styles of coding in an application.
Most importantly, to implement the key ideas in functional programming (FP), you can
use C# instead of learning a new programming language. Throughout the book, we’ll
look into these possibilities.
Now it is clear that conceptually a method and a function are the same. You see this
terminology difference because these two different paradigms treat functions differently.
In this book, we are exploring functional programming. So, I’ll be using the term function
instead of method in the corresponding places.
In Chapter 2, you’ll see that in addition to the traditional methods, there are other
ways to represent functions.
4
Chapter 1 Functional Programming Overview
Q&A Session
1.1 C# primarily follows OOP, but you are using it in the functional style. This helps
us avoid learning a new programming language, but are you saying that learning an
FP-based computer language is a bad idea?
Not at all. Developing your knowledge and learning a new programming language
are always good ideas. If you know functional languages like F# or Haskell, no one is
restricting you from using them. But in a real-world project, you may not have that
freedom. For example, if you are working on a project that uses only C#, you have
only one option: coding with C#. But C# is a multiparadigm language, and it supports
functional programming. This means that by using some of its features, you can bring
the power of functional programming to an existing project.
Second, you may not have the time or motivation to learn a new programming
language to fulfill some specific needs in a project. In such cases, it is always helpful to
implement the concept using known features.
Finally, many existing features in C# have been developed following the functional
style of coding. Instead of blindly using them, if you understand the context, you can
enjoy your learning.
Important Characteristics of FP
Though FP is a programming paradigm, it does not specify how the concepts should
be implemented in a programming language. As a result, a programming language that
follows FP can support many different features to implement these concepts. At a high
level, FP has the following characteristics:
• It prefers immutability.
Let’s take a quick look at these bullet points. You can surely guess that we’ll cover all
these points in more detail in the upcoming chapters.
5
Chapter 1 Functional Programming Overview
In C#, delegate types can represent functions. If you are familiar with Func (or
Action) delegates or if you are a lambda lover, probably you know this answer, and you
are already familiar with the usage.
To clarify, let’s consider the Func<int,int> type. You know that this is a delegate type
and it encapsulates a function that accepts one int parameter and returns a value of type
int. Let’s consider a sample line of code that is as follows:
Since the function is assigned to the variable doubleMaker, a developer can write
something like int result = doubleMaker(5);.
You can also pass this function to another function. Demonstration 1 shows such
a usage.
Demonstration 1
In the following program, doubleMaker is an instance of Func<int, int>, and it is used
as a variable. I pass this variable to another function, called GetResult.
6
Chapter 1 Functional Programming Overview
Note I remind you that I have heavily used top-level statements and enabled
implicit usings for the C# projects in this book. These features came in C#
9.0 and C#10.0 respectively.
int temp = 5;
Func<int, int> doubleMaker = x => x * 2;
int result = Container.GetResult(doubleMaker,temp);
WriteLine(result);
Output
The output is easy to predict. Since I have passed the integer 5 as an input (along with
the function variable doubleMaker), the program outputs 10.
Analysis
Let’s analyze the key steps in this program. First I declare a function using a delegate
and name the function variable doubleMaker. I then pass this variable to invoke the
GetResult function. So, the following points are clear:
7
Random documents with unrelated
content Scribd suggests to you:
The Project Gutenberg eBook of Gorillas &
Chimpanzees
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Author: R. L. Garner
Language: English
By
R. L. Garner
Illustrated
London
Osgood, McIlvaine & Co.
45 Albemarle Street, W.
1896
To
MY FAITHFUL AND GENEROUS FRIEND
MR. ADOLPH STROHM
WHO HAS GIVEN ME
LIBERAL AID AND UNSWERVING ENCOURAGEMENT
AND TO MY KIND AND STEADFAST FRIEND
MR. JAMES A. DEEMIN
WITH WHOM I SHARED SOME OF THE HARDSHIPS OF TRAVEL
AND A FEW OF THE JOYS OF THE HUNT
THIS VOLUME IS
GRATEFULLY DEDICATED BY
ITS AUTHOR
PREFACE
The present work is the natural product of some years devoted
to a study of the speech and habits of monkeys. It has led up to the
special study of the great apes. The matter contained herein is
chiefly a record of the facts tabulated during recent years in that
field of research.
The aim in view is to convey to the casual reader a more correct
idea than now prevails concerning the physical, mental, and social
habits of these apes.
The favourable conditions under which the writer has been
placed, in the study of these animals in the freedom of their native
jungle, have not hitherto been enjoyed by any other student of
Nature.
A careful aim to avoid all technical terms and scientific
phraseology has been adhered to, and the subject treated in a
simple style. Tedious details are relieved by an ample supply of
anecdotes taken from the writer's own observations, and most of
them are the acts of his own pets or of apes in a wild state. The
author has refrained from rash deductions and abstruse theories, but
has sought to place the animals here treated in their true light,
believing that to dignify the apes is not to degrade man, but to exalt
him even more.
It is hoped that a more perfect knowledge of these animals may
bring man into closer fellowship and deeper sympathy with Nature,
and cause him to realise that all creatures think and feel in some
degree, however small.
THE AUTHOR.
CONTENTS
CHAP. PAGE
PREFACE vii
I MAN AND APE COMPARED 1
II CAGED IN AN AFRICAN JUNGLE 14
III DAILY LIFE AND SCENES IN THE JUNGLE 22
IV THE CHIMPANZEE 36
V PHYSICAL, SOCIAL, AND MENTAL
QUALITIES 46
VI THE SPEECH OF CHIMPANZEES 66
VII THE CAPTURE AND CHARACTER OF
MOSES 76
VIII THE LIFE AND DEATH OF MOSES 92
IX AARON 102
X AARON AND ELISHEBA 116
XI THE DEATH OF AARON AND ELISHEBA 136
XII OTHER CHIMPANZEES 144
XIII OTHER KULU-KAMBAS 176
XIV GORILLAS 188
XV HABITS OF THE GORILLA 213
XVI OTHELLO AND OTHER GORILLAS 234
XVII OTHER APES 252
XVIII THE TREATMENT OF APES IN CAPTIVITY 262
LIST OF ILLUSTRATIONS
Page
Portrait of R. L. Garner Frontispiece
Waiting and Watching in
To face
the Cage 16
Starting for a Stroll " 22
Preparing for the Night " 30
In the Jungle " 42
A Stroll in the Jungle " 54
The Edge of the Jungle " 62
Trading Station in the
"
Interior 102
Plain and Edge of the
"
Forest 108
A Native Canoe " 118
Aaron and Elisheba " 132
Native Village at Moile—
"
Interior of Nyanza 146
Consul II. Riding a Tricycle " 164
Consul II. In Full Dress " 170
Native Village at Glass
"
Gaboon 180
Natives Skinning a Gorilla " 191
Skulls of Gorillas—Front
and Side Views 199–202
Young Gorilla Walking To face 208
Native Carrier Boy " 222
Native Women of the
"
Interior 230
GORILLAS AND CHIMPANZEES
CHAPTER I
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.
ebookname.com