What is the name of a [foo, bar] = [“foo”, “bar”] feature?

怎甘沉沦 提交于 2019-11-28 08:27:52

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) }
  }
}

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

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

Perl and PHP call it list assignment

Perl:

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

PHP:

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

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

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).

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!