In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of functional programming example analysis of assembly language, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that you will gain something after reading this functional programming example analysis article of assembly language. Let's take a look.
It's all about eliminating side effects.
To understand functional programming, we need to first understand functions. This may sound boring, but all in all, it is very insightful.
Simply put, a function is something that converts input into output. It's just that it's not that simple. Consider the meaning of the following function in Python:
Def square (x): return Xerox
This function is very simple. It requires a variable x, which could be an int, or a float or double, and then outputs the square of the variable.
Think again about the following function:
Global_list = [] def append_to_list (x): global_list.append (x)
At first glance, this function accepts a variable x, no matter what type, and since there is no return statement, it returns nothing. Is this really the case?
If global_list is not defined in advance, this function will not work, and its output will be the same list, albeit modified. Although global_list does not declare input, when we use this function, it changes:
Append_to_list (1) append_to_list (2) global_list
It returns [1J2] instead of an empty list. This may be the problem, and the list is indeed an input to the function, although we don't specify it.
1. Unfaithful to function
These implied inputs, or outputs in other cases, have an official name: side effects. Although we have only given a simple example, in more complex programs, these may make us face real difficulties.
You can think about how to test append_to_list: not only do we need to read the first line and test the function with any x, but we also need to read the entire definition, understand what it does, define global_list and test it in this way. This example tells us that when you are dealing with programs with thousands of lines of code, simple things can quickly become boring.
The good news is that there is a simple solution: be honest with the function as input. It's better this way:
Newlist = [] def append_to_list2 (x, some_list): some_list.append (x) append_to_list2 (1meme Newlist) append_to_list2 (2m Newlist) newlist
We haven't changed much, the output is still [1], and everything else remains the same.
However, we have changed one thing: the code now has no side effects.
Now, when we look at the function declaration, we know exactly what happened. If the program doesn't work properly, we can easily test each function individually and find out which one is wrong.
two。 Functional programming is writing pure functions
Functions with explicitly declared inputs and outputs are functions with no side effects, while functions with no side effects are pure functions.
A very simple definition of functional programming is to write programs only with pure functions. A pure function never modifies a variable, only creates a new variable as output.
In addition, we can get a specific output for a pure function with a given input. In contrast, impure functions may depend on some global variables. Therefore, if the global variables are different, the same input variables may result in different outputs. The latter makes debugging and code maintenance more difficult.
Here is a simple rule that is easy to find side effects: because each function must have some kind of input and output, the function declaration without any input or output must be impure. If you use functional programming, this is the first declaration you may want to change.
Functional programming is not only map and reduce
Loops are not something in functional programming. First, let's consider the following Python loop:
Integers = [1 odd_ints.append 2 3 for i in squared_odds 4 5 5 5] odd_ints = [] total = [] total = 0for i in integers: if I% 2 = 1 odd_ints.append (I) for i in odd_ints: squared_odds.append (iMagi) for i in squared_odds: total + = I
The above code is obviously too long compared to the simple operation we are going to perform. And it doesn't work because we're modifying global variables.
Instead, we can use the following code instead:
From functools import reduceintegers = [1 lambda n: n% 2 = = 1, integers) squared_odds = map (lambda n: n * n, odd_ints) total = reduce (lambda acc, n: acc + n, squared_odds)
This is a complete function. It's shorter and faster because we don't need to iterate through too many array elements. If you understand how filter, map, and reduce work, the code is not difficult to understand.
This does not mean that all function code uses map, reduce, and so on. This does not mean that you need functional programming to understand map and reduce. It's just that when you abstract a loop, these functions pop up a lot.
1.Lambda function
When it comes to the history of functional programming, many people start with the invention of the lambda function. Although lambda is undoubtedly the cornerstone of functional programming, they are not the root cause.
The Lambda function is a tool that can be used to make a program work. However, we can also use lambda in object-oriented programming.
two。 Static type
Although the above example is not statically typed, it is still functional.
Even if static typing adds an extra layer of security to our code, it is not essential that its function is normal. However, this may be a good supplement.
Functional programming in some programming languages is becoming more and more powerful 1.Perl
Perl handles side effects in a very different way than most programming languages. It contains a magic parameter $\. Perl does have its advantages, but I won't use it for functional programming.
2.Java
If you are using Java for functional programming, I can only wish you luck. Because half of your program is made up of static keywords, and other Java developers will regard your program as a disgrace.
This is not to say that Java is so bad, but because it is not designed for problems solved by functional programming, such as database management or machine learning applications.
3.Scala
What's interesting: the goal of Scala is to unify object-oriented and functional programming. If you think this is a little strange, you are not alone, because everyone thinks so: the goal of functional programming is to completely eliminate side effects, while object-oriented programming is to keep side effects inside the object.
Still, many developers think of Scala as a language that helps them transition from object-oriented programming to functional programming. Perhaps in the next few years, it will be easier for them to play a full role.
4.Python
Python encourages functional programming. You can see this by the fact that each function has at least one input self by default. It's a lot like Python's Zen: explicit is better than implicit!
5.Clojure
According to its creators, about 80% of Clojure is functional programming. By default, all values are immutable, just as they are needed in functional programming. However, we can solve this problem by using variable value wrappers around these immutable values. When you open such a package, what you get is the same.
6.Haskell
This is one of the few purely functional and statically typed languages. Although this looks like a time consumer in the development process, Haskell pays a heavy price when debugging the program. It's not as easy to learn as other languages, but it's definitely worth the investment!
This is the end of the article on "functional programming example Analysis of Assembly language". Thank you for reading! I believe you all have a certain understanding of the knowledge of "functional programming example analysis of assembly language". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.