什么是“销"?运算符 for 和 Elixir 变量是否可变? [英] What is the "pin" operator for, and are Elixir variables mutable?

查看:21
本文介绍了什么是“销"?运算符 for 和 Elixir 变量是否可变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在尝试理解 Elixir 中的^"运算符.来自网站:

Currently trying to understand the "^" operator in Elixir. From the website:

没有兴趣重新绑定时可以使用pin操作符^一个变量,而是匹配它之前的值匹配:

The pin operator ^ can be used when there is no interest in rebinding a variable but rather in matching against its value prior to the match:

来源 - http://elixir-lang.org/getting_started/4.html

考虑到这一点,您可以像这样为符号附加一个新值:

With this in mind, you can attach a new value to a symbol like so:

iex> x = 1  # Outputs "1"
iex> x = 2  # Outputs "2"

我也可以:

iex> x = x + 1  # Outputs "3"!

所以我的第一个问题是;Elixir 变量是否可变?看起来确实是这样......在函数式编程语言中这不应该是可能的吗?

So my first question is; Are Elixir variables mutable? It sure looks like if that's the case... Shouldn't that be possible in a functional programming language?

所以现在我们来到^"操作符...

So now we come to the "^" operator...

iex> x = 1  # Outputs "1"
iex> x = 2  # Outputs "2"
iex> x = 1  # Outputs "1"
iex> ^x = 2 # "MatchError"
iex> ^x = 1  # Outputs "1"

我认为^"的作用是将x"锁定到绑定到它的最后一个值.仅此而已吗?为什么不确保所有匹配"/分配都像 Erlang 本身一样不可变?

I think the effect of "^" is to lock "x" to the last value binded to it. Is that all there is to it? Why not just ensure that all 'matches'/assignments are immutable like Erlang itself?

我只是习惯了...

推荐答案

Elixir 中的数据仍然是不可变的,但是有几个速记,可以让您减少输入或者不用担心寻找新名称.在 Erlang 中,你经常可以看到这样的代码:

The data in Elixir is still immutable, but there are couple of shorthands, that let you type less or don't worry about finding new names. In Erlang, you could often see code like this:

SortedList = sort(List),
FilteredList = filter(SortedList),
List3 = do_something_with(FilteredList),
List4 = another_thing_with(List3)

在 Elixir 中,你可以只写:

In Elixir, you could just write:

list = sort(list)
list = filter(list)
list = do_something_with(list)
list = another_thing_with(list)

这完全一样,但看起来好一点.当然,最好的解决方案是这样写:

This is exactly the same, but it looks a little better. Of course the best solutions would be to write like this:

list |> sort |> filter |> do_something |> another_thing_with

每次你给 list 变量赋值新的东西,你都会得到一个新的实例:

Every time, you assign new thing to list variable, you get new instance:

iex(1)> a = 1
1
iex(2)> b = [a, 2]
[1, 2]
iex(3)> a = 2
2
iex(4)> b
[1, 2] # first a did not change, it is immutable, currently a just points to something else

你只是说,你不再对旧的 a 感兴趣,让它指向别的东西.如果你有 Erlang 背景,你可能知道 shell 中的 f 函数.

You just say, that you are no longer interested in the old a and let it point to something else. If you are coming from Erlang background, you probably know the f function from shell.

A = 1.
f(A).
A = 2.

在 Elixir 中,您不必编写 f.它会自动为您完成.这意味着,每次在模式匹配的左侧有变量时,您都在为其分配新值.

In Elixir you just don't have to write the f. It is done automatically for you. This means, that every time, you have variable on the left side of the pattern match, you are assigning new value to it.

如果没有 ^ 运算符,您将无法在模式匹配的左侧拥有变量,因为它将从右侧获得新值.^ 表示不要给这个变量分配新的东西——把它当作一个字面值.

Without the ^ operator, you wouldn't be able to have a variable on the left side of pattern match, because it would get new value from the right side. ^ means do not assign new things to this variable - treat it as a literal value.

这就是为什么在 Elixir 中

That is why in Elixir

x = 1
[1, x, 3] = [1, 2, 3]

在 Erlang 中相当于:

is equivalent in Erlang to:

X = 1,
[1, CompletelyNewVariableName, 3] = [1, 2, 3]

和:

x = 1
[1, ^x, 3] = [1, 2, 3]

相当于:

x = 1
[1, 1, 3] = [1, 2, 3]

在 Erlang 中是:

which in Erlang is:

X = 1,
[1, X, 3] = [1, 2, 3]

这篇关于什么是“销"?运算符 for 和 Elixir 变量是否可变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆