Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learning C++ Functional Programming
  • Table Of Contents Toc
Learning C++ Functional Programming

Learning C++ Functional Programming

By : Wisnu Anggoro
3 (5)
close
close
Learning C++ Functional Programming

Learning C++ Functional Programming

3 (5)
By: Wisnu Anggoro

Overview of this book

Functional programming allows developers to divide programs into smaller, reusable components that ease the creation, testing, and maintenance of software as a whole. Combined with the power of C++, you can develop robust and scalable applications that fulfill modern day software requirements. This book will help you discover all the C++ 17 features that can be applied to build software in a functional way. The book is divided into three modules—the first introduces the fundamentals of functional programming and how it is supported by modern C++. The second module explains how to efficiently implement C++ features such as pure functions and immutable states to build robust applications. The last module describes how to achieve concurrency and apply design patterns to enhance your application’s performance. Here, you will also learn to optimize code using metaprogramming in a functional way. By the end of the book, you will be familiar with the functional approach of programming and will be able to use these techniques on a daily basis.
Table of Contents (9 chapters)
close
close

Storing many different data types using tuples

We will get acquainted with tuples, an object that is able to hold a collection of elements, and each element can be of a different type. It is a new feature in C++11 and gives power to functional programming. The tuples will be most useful when creating a function that returns the value. Moreover, since functions don't change the global state in functional programming, we can return the tuples for all the values we need to change instead. Now, let's examine the following piece of code:

    /* tuples_1.cpp */
#include <tuple>
#include <iostream>

using namespace std;

auto main() -> int
{
cout << "[tuples_1.cpp]" << endl;

// Initializing two Tuples
tuple<int, string, bool> t1(1, "Robert", true);
auto t2 = make_tuple(2, "Anna", false);

// Displaying t1 Tuple elements
cout << "t1 elements:" << endl;
cout << get<0>(t1) << endl;
cout << get<1>(t1) << endl;
cout << (get<2>(t1) == true ? "Male" : "Female") << endl;
cout << endl;

// Displaying t2 Tuple elements
cout << "t2 elements:" << endl;
cout << get<0>(t2) << endl;
cout << get<1>(t2) << endl;
cout << (get<2>(t2) == true ? "Male" : "Female") << endl;
cout << endl;

return 0;
}

In the preceding code, we created two tuples, t1 and t2, with different constructing techniques using tuple<int, string, bool> and make_tuple. However, these two different techniques will give the same result. Obviously, in the code, we access each element in tuples using get<x>(y), where x is the index and y is the tuple object. And, with confidence, we will get the following result on the console:

Unpacking tuples values

Another useful member that functions in the tuples classes is tie(), which is used to unpack a tuple into individual objects or create a tuple of lvalue references. Also, we have the ignore helper class in tuples, a placeholder to skip an element when unpacking a tuple is using tie(). Let's see the use of tie() and ignore in the following block of code:

    /* tuples_2.cpp */
#include <tuple>
#include <iostream>

using namespace std;

auto main() -> int
{
cout << "[tuples_2.cpp]" << endl;

// Initializing two Tuples
tuple<int, string, bool> t1(1, "Robert", true);
auto t2 = make_tuple(2, "Anna", false);

int i;
string s;
bool b;

// Unpacking t1 Tuples
tie(i, s, b) = t1;
cout << "tie(i, s, b) = t1" << endl;
cout << "i = " << i << endl;
cout << "s = " << s << endl;
cout << "b = " << boolalpha << b << endl;
cout << endl;

// Unpacking t2 Tuples
tie(ignore, s, ignore) = t2;
cout << "tie(ignore, s, ignore) = t2" << endl;
cout << "new i = " << i << endl;
cout << "new s = " << s << endl;
cout << "new b = " << boolalpha << b << endl;
cout << endl;

return 0;
}

In the preceding code, we have the same two tuples that tuples_1.cpp has. We want to unpack t1 into variables i, s, and b respectively, using the tie() method. Then, we unpack t2 to the s variable only, ignoring the int and bool data in t2. If we run the code, the output should be as follows:

Returning a tuple value type

As we discussed earlier, we can maximize the use of tuples in functional programming when we want to write a function that returns multiple data. Let's take a look at the following block of code to know how to return the tuple and access the return value:

    /* tuples_3.cpp */
#include <tuple>
#include <iostream>

using namespace std;

tuple<int, string, bool> GetData(int DataId)
{
if (DataId == 1)
return std::make_tuple(0, "Chloe", false);
else if (DataId == 2)
return std::make_tuple(1, "Bryan", true);
else
return std::make_tuple(2, "Zoey", false);
}

auto main() -> int
{
cout << "[tuples_3.cpp]" << endl;

auto name = GetData(1);
cout << "Details of Id 1" << endl;
cout << "ID = " << get<0>(name) << endl;
cout << "Name = " << get<1>(name) << endl;
cout << "Gender = " << (get<2>(name) == true ?
"Male" : "Female");
cout << endl << endl;

int i;
string s;
bool b;
tie(i, s, b) = GetData(2);
cout << "Details of Id 2" << endl;
cout << "ID = " << i << endl;
cout << "Name = " << s << endl;
cout << "Gender = " << (b == true ? "Male" : "Female");
cout << endl;

return 0;
}

As we can see in the preceding code, we have a new function named GetData() returning a Tuple value. From that function, we will consume the data returning from it. We begin with creating the name variable and get the value from the GetData() function. We can also use the tie() method to unpack the tuple coming from the GetData() function, as we can see in the code when we access the data when ID = 2. The output on the console should be like the following screenshot when we run the code:

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Learning C++ Functional Programming
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon