Programming paradigm-functional programming

Speaking from a topic

 

Topic: Read in a text file, determine the frequency of use of all words and sort them from high to low, and print out a sorted list of all words and their frequencies.

 

Traditional solution

image

 

Functional solution

image

The difference between imperative and functional

  1. Imperative programming style often forces us to interweave different tasks for performance reasons so that we can complete multiple tasks with one loop. In the object-oriented imperative programming language, the unit of reuse is the message used for communication between classes and classes, such as methods.

     

  2. Functional programming uses map() and filter() to liberate us, allowing us to consider problems at a higher level of abstraction and see the problems more clearly. The idea of ​​reuse in functional programming languages ​​is very different.

     

    Functional languages ​​advocate the use of highly optimized operations for these data structures on a limited number of key data structures (such as list, set, and map) to form a basic operating mechanism. The developer then inserts his own data structure and high-level functions to adjust the operation mode of the mechanism according to the specific purpose.

     

     

One more question

Topic: Find the position of the first character in a character array in a string. For example, "Hello, World", ["a", "e", "i", "o", "u"], then e is the first character in the string, the position is 1, and 1 is returned

Problem-solving code:

        let words = "h e l l o w o r l d"
        let compare = ["a", "e", "i", "o", "u"]
        let wordsList = words.split(separator: " ")
        var index = 0
        zip(wordsList, compare).filter { $0 == $1}.map { (sub, str) in
            index = wordsList.firstIndex(of: sub) ?? 0
        }
        print(index)

Welcome to pay attention to [The Way of Infinite Testing] public account , reply [receive resources],
Python programming learning resources dry goods,
Python+Appium framework APP UI automation,
Python+Selenium framework Web UI automation,
Python+Unittest framework API automation,

Resources and codes are sent for free~
There is a QR code of the official account at the bottom of the article, you can just scan it on WeChat and follow it.

Remarks: My personal public account has been officially opened, dedicated to the sharing of test technology, including: big data testing, functional testing, test development, API interface automation, test operation and maintenance, UI automation testing, etc., WeChat search public account: "Infinite The Way of Testing", or scan the QR code below:

 Add attention and let us grow together!

Guess you like

Origin blog.csdn.net/weixin_41754309/article/details/113093262