Chapter One Introduction to functional programming- functional programming introduction

The first chapter introduces functional programming

Functional Programming is a programming paradigm. In which the function is a first-class citizen, may be paid to the function of other variables, it can be used as another parameter or function return values. Functional programming requirements, use only the expression, do not use statement.

Functional programming is a declarative programming and imperative programming it different. Command means to command the computer to do something, it needs to perform each step clearly is to calculate the results. Statement means you instructions what to do, then the task is to find out how the programming language to do, it is usually defined what is, rather than step by step to tell the computer to do it.

Understand the imperative and declarative programming examples

Imagine, you write a function that receives the file list and calculate the number of lines per file.
Imperative approach might look like this:

  • Open each file
  • The definition of a variable to store the number of rows to count
  • Every time a character by character read files, when faced with a line break, count ++
  • To the value end of the file, the store count

Here is the code:


auto count_lines_in_file(const std::vector<std::string>& files) -> std::vector<int>
{
    std::vector<int> result;
    int c = 0;

    for (const auto& file : files)
    {
        int count_lines = 0;
        std::ifstream in(file);
        while (c = in.get())
        {
            if (c == '\n')
                count_lines++;
        }
        result.push_back(count_lines);
    }
    return result;
}

However, the above code, error-prone, such as uninitialized variables, if, while the conditions and the like.
The following approach uses std :: count algorithm and input streams iterators simplifies the code:


int count_lines(const std::string& file)
{
    std::ifstream in(file);
    // 像对容器一样,也可以对流迭代器使用stl算法
    return std::count(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>(), '\n');
}

auto count_lines_in_file(std::vector<std::string>& files) -> std::vector<int>
{
    std::vector<int> result;

    for (auto& file : files)
    {
        result.push_back(count_lines(file));
    }
}

In the above solution, you do not care how count_lines is achieved, count_lines just pointing out the number of rows of the file. This is the functional programming intentions.

Let us simplify the code again, closer to functional programming, this time using std :: transform:


auto coutn_lines_in_file3(std::vector<std::string>& files)
{
    std::vector<int> result;
    std::transform(files.begin(), files.end(), result.begin(), count_lines);
    return result;
}

To c ++ 20, you can simplify the use of the library again:


auto count_lines_in_file4(std::vector<std::string>& files)
{
    // 使用管道操作符(c++20)
    return files | std::transform(count_lines);
}

Guess you like

Origin www.cnblogs.com/vlyf/p/12624017.html