In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "why a function is a first-class citizen". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn why functions are first-class citizens.
For first-class citizens [1] (First-class citizen), take a look at Wikipedia's definition:
In programming language design, a first-class citizen (also type, object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, modified, and assigned to a variable.
In programming languages, a first-class citizen is an entity that supports all operations, which usually include passing as parameters, returning from functions, modifying and assigning variables, and so on.
For example, the int type, which can be passed as an argument, can be returned from a function, or assigned to a variable, so it is a first-class citizen.
Similarly, a function is a first-class citizen, which means that a function can be assigned to a variable or stored in a data structure, or it can be used as an argument to other functions or a return value. There is also a definition of a function as a first-class citizen in Wikipedia.
In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. Some programming language theorists require support for anonymous functions (function literals) as well.In languages with first-class functions, the names of functions do not have any special status; they are treated like ordinary variables with a function type. The term was coined by Christopher Strachey in the context of "functions as first-class citizens" in the mid-1960s.
The concept of function as a first-class citizen was put forward by the British computer scientist Christopher Strachey [3] in 1960. However, not all languages regard functions as first-class citizens, especially in the early days, for example, functions in C language are not first-class citizens, and some functions are achieved through function pointers; for example, C++, Java and so on are added in later versions.
Generally speaking, functional programming languages, dynamic languages and modern programming languages all serve as first-class citizens, such as functional languages such as Scala and Julia, dynamic languages such as JavaScript and Python, and modern compiled languages such as Go, Rust and Swift.
In order to give you a better understanding of the function as a first-class citizen, let's take a look at how the Go language supports the first-class function mentioned above.
Anonymous function
Functions generally have names, but sometimes unnamed functions are more concise and easy to use. A function without a name is called an anonymous function.
Here is an example of an anonymous function in the Go language:
Package main import ("fmt") func main () {fn: = func () {fmt.Println ("This is anonymous function!")} fn () fmt.Printf ("The type of fn:% T\ n", fn)} / output: / / This is anonymous function! / / The type of fn: func ()
Run online: https://play.studygolang.com/p/IcInzZsAr0a.
In Go, the most common scenario used by anonymous functions is to open a goroutine, and you will often see code like this:
Go func () {/ / xxxx} ()
Here, the anonymous function is called immediately after it is defined. In addition, defer statements are also common.
Define function types
Defining function types is similar to other types, while the second half is similar to anonymous functions, except that there is no function implementation. For example, the HandlerFunc function type in the net/http package:
Type HandlerFunc func (ResponseWriter, * Request)
How to use this type? To be able to understand such code means that you understand:
Var h http.HandlerFunc = func (w ResponseWriter, req * Request) {fmt.Fprintln (w, "Hello World!")}
Function as a parameter
This means that one function as an argument to another function, that is, a callback, is common in JS. It also appears frequently in the Go language. The problem at the beginning of the article is that the function is used as a parameter. According to the API definition of Gin, the signature of the router.GET method is as follows:
Func (group * RouterGroup) GET (relativePath string, handlers... HandlerFunc) IRoutes
Where HandlerFunc is a function type, which is defined as follows:
Type HandlerFunc func (* Context)
Therefore, in router.GET ("/ users", Users), Users is only a parameter of the GET function, and the parameter type is HandlerFunc, while the definition of Users as long as it conforms to HandlerFunc:
Func Users (ctx * gin.Context) {}
Because the function Users is taken as an argument here, there is no need to pass parameters to Users. The call to Uers is handled internally by GET, which is called callback.
Function as a return value
Function as a return value, in Go, such a function must be anonymous. In Web development, the middleware will use the above function as the return value, or take Gin as an example to define a Logger middleware:
Func Logger () gin.HandlerFunc {return func (c * gin.Context) {t: = time.Now () / / Set example variable c.Set ("example", "12345") / / before request c.Next () / / after request latency: = time.Since (t) log.Print (latency) / / access the status we are sending status: = c.Writer.Status () log.Println (status)}}
As we know above, gin.HandlerFunc is a function type, so an instance of this type needs to be returned. As long as the anonymous function (function number face value) is consistent with the underlying type of gin.HandlerFunc type, it will be implicitly converted, so you can directly return the anonymous type func (c * gin.Context) {}.
It is often heard that higher-order functions support higher-order functions if they are first-class citizens. A function only takes one or more function type parameters, or returns a function, which is called a higher-order function.
Closure
Closure is a special case of anonymous functions. When the variable accessed by an anonymous function is defined outside the function body, such an anonymous function is called a closure.
A simple example:
Package main import ("fmt") func main () {a: = 5 func () {fmt.Println ("a =", a)} ()}
In the above program, the anonymous function accesses the variable an on line 10, and an exists outside the function body. So this anonymous function is a closure.
Thank you for your reading. the above is the content of "Why a function is a first-class citizen". After the study of this article, I believe you have a deeper understanding of why a function is a first-class citizen. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.