Julia 1.0.0 : What does the `=>` operator do?

标签 julia

我看到这个 Stackoverflow => 的代码,但是当我在 Julia 1.0.0 在线帮助中搜索“=>”时,我得到零点击。

replace!(x, 0=>4)  # The last expression is the focus of this question.

在 REPL 帮助中,我得到:
help?> =>
search: =>

  Pair(x, y)
  x => y

Construct a Pair object with type Pair{typeof(x), typeof(y)}. The elements are stored in the fields first and second. They can also be accessed via iteration.

See also: Dict

Examples ≡≡≡≡≡≡≡≡≡≡


  julia> p = "foo" => 7
  "foo" => 7

  julia> typeof(p)
  Pair{String,Int64}

  julia> p.first
  "foo"

  julia> for x in p
             println(x)
         end
  foo
  7

什么=>做在 replace!(x, 0=>4) ?它是创建一对,用四个替换所有零,还是什么?为什么我在 Julia 1.0.0 在线文档中似乎没有找到它?

编辑

添加了代码以帮助我理解下面@Bill 的有用答案:
julia> x = [1, 0, 3, 2, 0]
5-element Array{Int64,1}:
 1
 0
 3
 2
 0

julia> replace!(x, 0=>4)
5-element Array{Int64,1}:
 1
 4
 3
 2
 4

编辑 2

除了@Bill 接受的答案之外,我发现@Steven 的答案也很有帮助。抱歉,我无法同时检查它们,但 Bill 是第一个进来的,并且它们都提供了有用的信息。

最佳答案

“=> 在替换中做了什么!(x, 0=>4)?它是否创建了一对,用四个替换所有零,或者什么?”

它创建了一个对。在函数 replace 中,第二个参数位置的 Pair 表示 replace() 的多次分派(dispatch)选择替换函数的一个版本,其中给定一个数字数组或字符串 x,x 中适合 Pair 第一部分的所有项目都被替换与 Pair 的第二部分的实例。

您可以查看 REPL 文档进行替换以了解详细信息。

关于 Julia 1.0.0 : What does the `=>` operator do?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52488743/

相关文章:

csv - 具有不同列数的 Readtable() - Julia

dataframe - 选择分组数据框中每个组的第 n 个元素

optimization - Julia 中最小二乘坐标下降算法不收敛

julia - 如何在 Julia 中将运算符用作函数?

arrays - Julia:大型数组的有限打印

reflection - Julia:通过给定的字符串调用函数

julia - 在没有互联网连接的情况下使用 julia 语言(镜像?)

julia - 以复合类型添加所有数组的高效而简洁的方法?

testing - 如何在 julia 中为自定义结构创建相等性测试用例

performance - Julia 从标准正态分布中绘制