<jupyter_start><jupyter_text>`[1, 2, 3, 4, 5]` はリストではなく、配列(もしくはベクトル)である。<jupyter_code>num_arr = [1, 2, 3, 4, 5]
function f(num_arr)
    sum = 0
    n_iters = length(num_arr)
    for i in 1:n_iters
        sum += num_arr[i]
    end
    return sum
end

f(num_arr)<jupyter_output><empty_output><jupyter_text>`f(num_arr)` は配列(Array)以外にも適用可能だが `num_arr` という名前でいいのか？<jupyter_code>f(1:5)
[typeof(1:5); [∘(fill(supertype, k)...)(typeof(1:5)) for k in 1:5]]<jupyter_output><empty_output><jupyter_text>あと、`Base.sum` とローカル変数の名前が被っていても確かに問題ないが、それでもいいのか？<jupyter_code>function f_rev1(num_abst_arr)
    total = 0
    n_iters = length(num_abst_arr)
    for i in 1:n_iters
        total += num_arr[i]
    end
    return total
end

f_rev1(num_arr)<jupyter_output><empty_output><jupyter_text>`f_rev1([k for k in 1:5])` は計算できるが、`f_rev1(k for k in 1:5)` はエラーになる。<jupyter_code>f_rev1([k for k in 1:5])
f_rev1(k for k in 1:5)
G = (k for k in 1:5)
function f_rev2(num_iter)
    total = 0
    for num in num_iter
        total += num
    end
    return total
end

f_rev2(num_arr)
f_rev2(k for k in 1:5)<jupyter_output><empty_output><jupyter_text>引数 `num_iter` の要素が整数でない場合に型不安定！<jupyter_code>@code_warntype f_rev2([1.0, 2.0, 3.0, 4.0, 5.0])
function f_rev3(num_iter)
    total = zero(first(num_iter))
    for num in num_iter
        total += num
    end
    return total
end

f_rev3(num_arr)
f_rev3(k for k in 1:5)
@code_warntype f_rev3([1.0, 2.0, 3.0, 4.0, 5.0])<jupyter_output>MethodInstance for f_rev3(::[0mVector{Float64})
  from f_rev3(num_iter) in Main at In[13]:1
Arguments
  #self#[36m::Core.Const(f_rev3)[39m
  num_iter[36m::Vector{Float64}[39m
Locals
  @_3[33m[1m::Union{Nothing, Tuple{Float64, Int64}}[22m[39m
  total[36m::Float64[39m
  num[36m::Float64[39m
Body[36m::Float64[39m
[90m1 ─[39m %1  = Main.first(num_iter)[36m::Float64[39m
[90m│  [39m       (total = Main.zero(%1))
[90m│  [39m %3  = num_iter[36m::Vector{Float64}[39m
[90m│  [39m       (@_3 = Base.iterate(%3))
[90m│  [39m %5  = (@_3 === nothing)[36m::Bool[39m
[90m│  [39m %6  = Base.not_int(%5)[36m::Bool[39m
[90m└──[39m       goto #4 if not %6
[90m2 ┄[39m %8  = @_3[36m::Tuple{Float64, Int64}[39m
[90m│  [39m       (num = Core.getfield(%8, 1))
[90m│  [39m %10 = Core.getfield(%8, 2)[36m::Int64[39m
[90m│  [39m       (total = total + num)
[90m│  [39m       (@_3 = Base.iterate(%3, %10))
[90m│  [39m %13 = (@_3 === nothing)[36m::Bool[39m
[90m│  [39m[...]<jupyter_text>`f_rev3(num_iter)` は引数 `num_iter` の要素が数でなくても使えるのに、`num_iter` という名前でいいのか？<jupyter_code>f_rev3([[1, 2], [3, 4], [5, 6]])
function f_rev4(iter)
    total = zero(first(iter))
    for val in iter
        total += val
    end
    return total
end

f_rev4(num_arr)
f_rev4(k for k in 1:5)
f_rev4([[1, 2], [3, 4], [5, 6]])<jupyter_output><empty_output><jupyter_text>Juliaでは全ての文が値を持つ式なので函数の終わりでの `return` は省略できる。<jupyter_code>function f_rev5(iter)
    total = zero(first(iter))
    for val in iter
        total += val
    end
    total
end

f_rev5(num_arr)
f_rev5(k for k in 1:5)
f_rev5([[1, 2], [3, 4], [5, 6]])<jupyter_output><empty_output><jupyter_text>`iter` が空の場合はエラーになる。<jupyter_code>zeros(0)
f_rev5(zeros(0))
function f_rev6(iter, init = isempty(iter) ? zero(eltype(iter)) : zero(first(iter)))
    total = init
    for val in iter
        total += val
    end
    total
end

f_rev6(num_arr)
f_rev6(k for k in 1:5)
f_rev6([[1, 2], [3, 4], [5, 6]])
f_rev6(zeros())
f_rev6(Vector{Int64}[])
f_rev6(Vector{Int64}[], [0, 0])<jupyter_output><empty_output><jupyter_text>函数の定義がこれだけ短いなら、「意味のある変数名」ではなく、1文字変数名でもいいんじゃね？<jupyter_code>function f_rev7(A, o = isempty(A) ? zero(eltype(A)) : zero(first(A)))
    s = o
    for v in A
        s += v
    end
    s
end

f_rev7(num_arr)
f_rev7(k for k in 1:5)
f_rev7([[1, 2], [3, 4], [5, 6]])
f_rev7(zeros())
f_rev7(Vector{Int64}[], [0, 0])
A = [
    1 2
    3 4
]
f_rev7(A)<jupyter_output><empty_output><jupyter_text>Juliaの配列のインデックスは任意始まりにできる。<jupyter_code>using OffsetArrays
a = OffsetArray([k^3 for k in -3:3], -3:3)
@show collect(eachindex(a))

for i in eachindex(a)
    println("a[", i, "] = ", a[i])
end
@show pairs(a)
for (i, v) in pairs(a)
    println("a[", i, "] = ", v)
end
pairs(a)
A = OffsetArray(reshape(1:24, 4, 3, 2), -1:2, -1:1, 0:1)
for k in axes(A, 3), j in axes(A, 2), i in axes(A, 1)
    println("A[", i, ", ", j, ", ", k, "] = ", A[i, j, k])
end<jupyter_output>A[-1, -1, 0] = 1
A[0, -1, 0] = 2
A[1, -1, 0] = 3
A[2, -1, 0] = 4
A[-1, 0, 0] = 5
A[0, 0, 0] = 6
A[1, 0, 0] = 7
A[2, 0, 0] = 8
A[-1, 1, 0] = 9
A[0, 1, 0] = 10
A[1, 1, 0] = 11
A[2, 1, 0] = 12
A[-1, -1, 1] = 13
A[0, -1, 1] = 14
A[1, -1, 1] = 15
A[2, -1, 1] = 16
A[-1, 0, 1] = 17
A[0, 0, 1] = 18
A[1, 0, 1] = 19
A[2, 0, 1] = 20
A[-1, 1, 1] = 21
A[0, 1, 1] = 22
A[1, 1, 1] = 23
A[2, 1, 1] = 24
{'fraction_non_alphanumeric': 0.20419287211740042, 'fraction_numerical': 0.0949685534591195, 'mean_word_length': 4.174620390455531, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '327679', 'license': 'MIT', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2502, 'n_words': 493, 'path': '0001/revising a function.ipynb', 'repo_name': 'genkuroki/public', 'size': '4770'}