前回に引き続きJuliaについてです。
ドキュメントに沿って試していきます。
Variables
julia> x=10
10
julia> x+1
11
julia> x=1+1
2
julia> x="Hello"
"Hello"
julia> x=10
10
julia> 2x
20
julia> x="こんにちは"
"こんにちは"
- 普通ですね。日本語も問題無いようです。
Allowed Variable Names
- 予約語はふつーにだめです。
julia> if = "もし"
ERROR: syntax: unexpected "="
julia> else = false
ERROR: syntax: unexpected "else"
julia> try = "No"
ERROR: syntax: unexpected "="
julia> 日本語の変数名 = "でもOKです"
"でもOKです"
julia> 日本語の変数名
"でもOKです"
- 日本語の変数名でもOKですけど、まぁ使いませんよね。
Stylistic Conventions
juliaでの変数のお約束です。ええと...
- Names of variables are in lower case.(変数は小文字で)
- Word separation can be indicated by underscores ('_'), but use of underscores is discouraged unless the name would be hard to read otherwise.(アンダースコアは使ってもいいけど読みにくい場合のみ)
- Names of
Types
andModules
begin with a capital letter and word separation is shown with upper camel case instead of underscores.(TypeとModulesは大文字開始のキャメルケースで) - Names of
functions
andmacros
are in lower case, without underscores.(関数とマクロは小文字でアンダースコアなし) - Functions that write to their arguments have names that end in !. These are sometimes called "mutating" or "in-place" functions because they are intended to produce changes in their arguments after the function is called, not just return a value.(引数を変更して値も返すような関数には!マークをつける。インプレイス関数と呼ばれる。)
大体普通ですねJavaに近いかな?、あと最後のは引数が破壊的な場合には関数名に!をつけろということなんでしょうか??
こういうことですね、説明がありました。
Append !
to names of functions that modify their arguments
Instead of:
function double(a::AbstractArray{<:Number})
for i = firstindex(a):lastindex(a)
a[i] *= 2
end
return a
end
use:
function double!(a::AbstractArray{<:Number})
for i = firstindex(a):lastindex(a)
a[i] *= 2
end
return a
end
実行してみます。
julia> a=[1,2,3]
3-element Array{Int64,1}:
1
2
3
julia> double!(a)
3-element Array{Int64,1}:
2
4
6
julia> a
3-element Array{Int64,1}:
2
4
6
aの値が破壊されていることがわかります。
今日はこの辺で。