[foo, bar] = [“foo”, “bar”] 功能的名称是什么?

发布于 2024-09-27 23:41:06 字数 117 浏览 9 评论 0原文

我需要知道某些语言提供的这个很酷的功能的正确名称。

仅供参考:在某些语言中,可以通过将值结构分配给“变量”结构来进行多重分配。在问题标题的示例中,它将“foo”分配给 foo,将“bar”分配给 bar。

I need to know a correct name for this cool feature that some languages provide.

FYI: In some languages it is possible to do a multiple assignments by assigning a structure of values to a structure of "variables". In the example in the question title it assigns "foo" to foo and "bar" to bar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

木緿 2024-10-04 23:41:06

它通常在函数式语言(没有赋值)中称为“解构绑定”,在命令式语言中称为“解构赋值”。

有些语言提供该功能的子集,然后将其称为不同的名称。例如,在 Python 中,它适用于元组、列表或序列,称为“元组解包”、“列表解包”或“序列解包”,在 Ruby 中,它称为“元组解包”、“列表解包”或“序列解包”。与数组(或可转换为数组的对象)一起使用,称为并行赋值。

解构绑定可以变得任意复杂。例如,此(虚构的)绑定

[Integer(a), b, 2, c] = some_array

会将 some_array 的第一个元素分配给 a,将第二个元素分配给 b,将第四个元素分配给 c,但如果第一个元素是Integer,第三个元素等于2且长度为4。因此,这甚至包含了一些条件逻辑。

解构绑定是更通用的模式匹配的子集,它是 Haskell、ML、OCaml、F#、Erlang 和 Scala 等函数式语言的标准功能。不同之处在于,解构绑定仅允许您分解结构并将其组件绑定到变量,而模式匹配还匹配这些结构内的值并允许您做出决策,特别是允许您在绑定的上下文中运行任意代码。 (您可以将上面的假想绑定视为解构绑定和模式匹配之间的中间位置。)

以下是假想语言中的 reverse 函数的经典示例,使用模式匹配编写:

def reverse(l: List): List {
  match l {
    when []              { return [] }
    when [first :: rest] { return (reverse(rest) :: first) }
  }
}

It's generally called destructuring bind in functional languages (which don't have assignments) and destructuring assignment in imperative languages.

Some languages provide subsets of that feature and then call it something different. For example, in Python it works with Tuples, Lists or Sequences and is called Tuple unpacking, List unpacking or Sequence unpacking, in Ruby, it works with Arrays (or objects that are convertible to an array) and is called parallel assignment.

Destructuring bind can get arbitrarily complex. E.g. this (imaginary) bind

[Integer(a), b, 2, c] = some_array

would assign the first element of some_array to a, the second element to b and the fourth element to c, but only if the first element is an Integer, the third element is equal to 2 and the length is 4. So, this even incorporates some conditional logic.

Destructuring bind is a subset of more general pattern matching, which is a standard feature of functional languages like Haskell, ML, OCaml, F#, Erlang and Scala. The difference is that destructuring bind only lets you take apart a structure and bind its components to variables, whereas pattern matching also matches on values inside those structures and lets you make decisions and in particular lets you run arbitrary code in the context of the bindings. (You can see the above imaginary bind as half-way in between destructuring bind and pattern matching.)

Here's the classical example of a reverse function in an imaginary language, written using pattern matching:

def reverse(l: List): List {
  match l {
    when []              { return [] }
    when [first :: rest] { return (reverse(rest) :: first) }
  }
}
孤千羽 2024-10-04 23:41:06

在 Python 中,它被称为列表或序列解包:http://docs. python.org/tutorial/datastructs.html#tuples-and-sequences

my_list = ["foo", "bar"]
foo, bar = my_list

In Python it is known as list or sequence unpacking: http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences

my_list = ["foo", "bar"]
foo, bar = my_list
屋檐 2024-10-04 23:41:06

在 Ruby 和其他语言中这称为并行赋值。

It's called parallel assignment in Ruby and other languages.

七颜 2024-10-04 23:41:06

Perl 和 PHP 称之为列表赋值

Perl:

my ($foo, $bar, $baz) = (1, 2, 3);

PHP:

list($foo, $bar, $baz) = array(1, 2, 3);

Perl and PHP call it list assignment

Perl:

my ($foo, $bar, $baz) = (1, 2, 3);

PHP:

list($foo, $bar, $baz) = array(1, 2, 3);
恬淡成诗 2024-10-04 23:41:06

如果您将右侧视为元组,则可以将分配视为一种 元组拆包

If you view the right hand side as a tuple, one could view the assignment as a kind of Tuple Unpacking.

套路撩心 2024-10-04 23:41:06

在 Erlang 中,它......好吧,这不是赋值,而是模式匹配(因为在 Erlang 中没有赋值)。

$ erl
Erlang R14B (erts-5.8.1) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:true]

Eshell V5.8.1  (abort with ^G)
1> [H1, H2, H3| Rest] = [1,2,3,4,5].
[1,2,3,4,5]
2> H1.
1
3> H2.
2
4> H3.
3
5> Rest.
[4,5]

为什么叫“模式匹配”?因为它实际上是匹配模式。看:

6> [1,2,3,4,A] = [1,2,3,4,5].
[1,2,3,4,5]
7> A.
5
8> [1,2,3,4,A] = [1,2,3,4,6].
** exception error: no match of right hand side value [1,2,3,4,6]

在第一个中,我们所做的实际上相当于一个断言,即列表将以 [1,2,3,4] 开头,并且第五个值可以是任何值,但请绑定它进入未绑定变量A。在第二个中,我们做了同样的事情,只是 A 现在已绑定,因此我们显式查找列表 [1,2,3,4,5] (因为 A 现在是 5)。

In Erlang it's ... well, it's not assignment, it's pattern matching (seeing as there is no assignment, as such, in Erlang).

$ erl
Erlang R14B (erts-5.8.1) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:true]

Eshell V5.8.1  (abort with ^G)
1> [H1, H2, H3| Rest] = [1,2,3,4,5].
[1,2,3,4,5]
2> H1.
1
3> H2.
2
4> H3.
3
5> Rest.
[4,5]

Why is it called "pattern matching"? Because it actually is matching patterns. Look:

6> [1,2,3,4,A] = [1,2,3,4,5].
[1,2,3,4,5]
7> A.
5
8> [1,2,3,4,A] = [1,2,3,4,6].
** exception error: no match of right hand side value [1,2,3,4,6]

In the first one we did what effectively amounts to an assertion that the list would start with [1,2,3,4] and that the fifth value could be anything at all, but please bind it into the unbound variable A. In the second one we did the same thing except that A is now bound so we're looking explicitly for the list [1,2,3,4,5] (because A is now 5).

蓝天白云 2024-10-04 23:41:06

Mozilla 将其称为解构赋值。在Python中,它是序列解包;元组拆包是一种常见的特殊情况。

Mozilla calls it destructuring assignment. In Python, it's sequence unpacking; tuple unpacking is a common special case.

宫墨修音 2024-10-04 23:41:06

在 Clojure 中,它被称为“解构”。简单的例子:

(let [[foo bar] ["foo" "bar"]]
  (println "I haz" foo "and" bar))

它也经常用于函数定义中,例如以下将单点参数解构为 x 和 y 分量:

(defn distance-from-origin [[x y]]
  (sqrt (+ (* x x) (* y y))))

您还可以使用相同的技术来解构嵌套数据结构或键/值关联映射。

In Clojure it would be called destructuring. Simple example:

(let [[foo bar] ["foo" "bar"]]
  (println "I haz" foo "and" bar))

It's also often used in function definitions, e.g. the following destructures a single point argument into x and y components:

(defn distance-from-origin [[x y]]
  (sqrt (+ (* x x) (* y y))))

You can also use the same technique to destructure nested data structures or key/value associative maps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文