review juliabox 的教程时看到中文这边这样说:
原地修改与非原地修改函数(Mutating vs. non-mutating functions)
约定:函数名以 !
结束的原地修改传入的变量,函数名非以 !
结束的则不会改变传入的变量。
Pollish up chinese translation (intro2jl-short) by inkydragon · Pull Request #63 · JuliaAcademy/JuliaTutorials · GitHub
并没有忠于原文。
Mutating vs. non-mutating functions
By convention, functions followed by !
alter their contents and functions lacking !
do not.
in place 时以前的译者自己加的,不过听起来有点道理,这样做不就是为了快么。
先伸手,睡一觉,说不定已经有人给出反例了。
Functions that modify their inputs have names that end in !. These
functions are sometimes called mutating functions or in-place functions.
这个 cheatsheet 也提到了这一点
搜了一下源码,看很多文档里都直接把带 !
的函数叫做 in-place 的函数。
ldiv!(Y, A, B) -> Y
Compute `A \\ B` in-place and store the result in `Y`, returning the result.
# swap columns i and j of a, in-place
function swapcols!(a::AbstractMatrix, i, j)
# like permute!! applied to each row of a, in-place in a (overwriting p).
function permutecols!!(a::AbstractMatrix, p::AbstractVector{<:Integer})
shuffle!([rng=GLOBAL_RNG,] v::AbstractArray)
In-place version of [`shuffle`](@ref): randomly permute `v` in-place,
rmul!(A::AbstractArray, b::Number)
Scale an array `A` by a scalar `b` overwriting `A` in-place. Use
我觉得!意思是指函数可能会修改以引用方式传递的值,例如说push!(a,1),a是数组
in-place的语义更加狭窄,主要是指返回值和某个输入值占据有相同的内存区域,用返回值覆盖输入值,例如说shuffle!,返回的数组和输入的数组大小形状一致.但是push!就不是in-place的,因为数组变长了就没办法in-place放在同一个地方