什么是“pin”操作符为和,并且Elixir变量是否可变? [英] What is the "pin" operator for, and are Elixir variables mutable?

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

问题描述

目前正在试图理解Elixir中的^操作符。
从网站:


当没有兴趣重新绑定
a变量时,可以使用pin运算符^而是在
匹配之前匹配其值:


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



考虑到这一点,您可以附加一个新的值到如下符号:

  iex> x = 1#输出1
iex> x = 2#输出2

我也可以这样做:

  iex> x = x + 1#输出3! 

所以我的第一个问题是Elixir变量是否可变?
肯定看起来像是这样的情况?在功能编程语言中应该不可能吗?



所以现在我们来到^运算符...

  iex> x = 1#输出1
iex> x = 2#输出2
iex> x = 1#输出1
iex> ^ x = 2#MatchError
iex> ^ x = 1#输出1

我认为^的效果是锁定 x绑定到最后一个值。那是所有的吗?
为什么不确保所有的匹配/作业是不可变的,像Erlang本身?



我刚刚习惯了...

解决方案

Elixir中的数据仍然是不可变的,但有几个shorthands,让你输入更少或不用担心找到新的名字。在Erlang中,您可以经常看到这样的代码:

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



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

这是完全一样的,但它看起来好一点。当然最好的办法就是这样写:

  list |>排序|>过滤器|> do_something |>另外_ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ c>变量,你得到新的实例:

  iex(1)> a = 1 
1
iex(2)> b = [a,2]
[1,2]
iex(3)> a = 2
2
iex(4)> b
[1,2]#第一个没有改变,这是不可变的,目前只是指向别的东西

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

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

在Elixir中,您只需编写˚F。它自动为您完成。这意味着,每次在模式匹配的左侧都有变量,那么您需要为其分配新的值。



没有 ^ 运算符,您将无法在模式匹配的左侧有一个变量,因为它将从右侧获取新值。 ^ 意味着不要将新事物分配给此变量 - 将其视为文字值。



这就是为什么在Elixir

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

在Erlang中相当于:

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

和:

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

相当于:

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


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

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:

Source - 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"

I can also do:

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

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"

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?

I was just getting used to that...

解决方案

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)

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

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

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.

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.

That is why in Elixir

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

is equivalent in Erlang to:

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

and:

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

is equivalent to:

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

which in Erlang is:

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

这篇关于什么是“pin”操作符为和,并且Elixir变量是否可变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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