加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

函数是一等公民,这究竟在说什么?

发布时间:2021-05-26 21:23:50 所属栏目:编程 来源:互联网
导读:对付有些人来嗣魅这基础不是题目,但有些人却想不大白。我提到,在 Go 说话中,函数是一等国民,但对方不清晰这到底在说什么。看来有须要表明下什么是一等国民。

对付有些人来嗣魅这基础不是题目,但有些人却想不大白。我提到,在 Go 说话中,函数是一等国民,但对方不清晰这到底在说什么。看来有须要表明下什么是一等国民。

再往下看之前,你能说出什么是一等国民吗?

关于一等国民[1](First-class citizen)看看维基百科的界说:

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.

大意是说,在编程说话中,所谓一等国民,是指支持全部操纵的实体, 这些操纵凡是包罗作为参数转达,从函数返回,修改并分派给变量等。

好比 int 范例,它支持作为参数转达,可以从函数返回,也可以赋值给变量,因此它是一等国民。

相同的,函数是一等国民,意味着可以把函数赋值给变量或存储在数据布局中,也可以把函数作为其余函数的参数可能返回值。关于函数是一等国民,在维基百科也有界说[2]。

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.

函数作为一等国民的观念是 1960 年由英国计较机学家 Christopher Strachey[3] 提出来的。然而,并非全部说话都将函数作为一等国民,出格是早期,好比 C 说话中函数就不是一等国民,一些成果通过函数指针来实现的;再好比 C++、Java 等,都是其后的版本才加上的。

一样平常来说,函数式编程说话、动态说话和当代的编程说话,函数城市作为一等国民,好比:Scala、Julia 等函数式说话,JavaScript、Python 等动态说话,Go、Rust、Swift 等当代的编译型说话。

为了让各人对函数是一等国民有更深的领略,针对上文提到的一等国民的一等成果,我们看看 Go 说话是怎样支持的。

匿名函数

函数一样平常是著名字的,但偶然辰没著名字的函数更简捷、好用。没著名字的函数叫匿名函数。

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读