Julia タプル

Julia のタプルは配列と似ており、どちらも順序付きの要素の集合です。異なる点は、タプルの要素は変更できないことです。

また、タプルは小括弧を使用し(...)、配列は角括弧を使用します[...]。

タプルの作成は非常に簡単で、括弧内に要素を追加し、カンマで区切るだけです。配列の多くの関数もタプルで使用できます。

以下の例のとおりです:

サンプル

julia> tupl=(5,10,15,20,25,30)   # タプルを作成する
(5, 10, 15, 20, 25, 30)

julia> tupl
(5, 10, 15, 20, 25, 30)

julia> tupl[3:end]          # 3番目から最後までの要素を持つタプルを出力する
(15, 20, 25, 30)

julia> tupl = ((1,2),(3,4))   # 2次元タプルを作成する
((1, 2), (3, 4))

julia> tupl[1]      # 2次元タプルの要素にアクセスし、1次元目のタプルを出力する
(1, 2)

julia> tupl[1][2]    # 2次元タプルの要素にアクセスし、1次元目のタプルの2番目の要素を出力する
2

タプルの要素は変更できません。変更しようとするとエラーが発生します:

サンプル

julia> tupl2=(1,2,3,4)
(1, 2, 3, 4)

julia> tupl2[2]=0
ERROR: MethodError: no method matching setindex!(::NTuple{4, Int64}, ::Int64, ::Int64)
Stacktrace:
 [1] top-level scope
   @ REPL[8]:1

タプルの命名

タプルに名前を付けることができ、それによってより便利にアクセスできます。

以下に、さまざまなタプルの命名方法を示します。

1、タプルのキー(key)と値(value)を別々に命名する

タプルのキー(key)と値(value)は別々に独立して命名できます。例は以下のとおりです:

サンプル

julia> names_shape = (:corner1, :corner2)
(:corner1, :corner2)

julia> values_shape = ((100, 100), (200, 200))
((100, 100), (200, 200))

julia> shape_item2 = NamedTuple{names_shape}(values_shape)
(corner1 = (100, 100), corner2 = (200, 200))

ドット記号を使用して.タプルにアクセスできます:

サンプル

julia> shape_item2.corner1
(100, 100)

julia> shape_item2.corner2
(200, 200)

2、キー(key)と値(value)を同時に1つのタプル内に置く

キー(key)と値(value)は同時に1つのタプル内に置くことができます。例は以下のとおりです:

サンプル

julia> shape_item = (corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))
(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))

ドット記号を使用して.タプルにアクセスできます:

サンプル

julia> shape_item.corner1
(1, 1)

julia> shape_item.corner2
(-1, -1)

julia> shape_item.center
(0, 0)

julia> (shape_item.center,shape_item.corner2)
((0, 0), (-1, -1))

通常のタプルと同じようにすべての値にアクセスすることもできます。次のようになります:

サンプル

julia> c1, c2, center = shape_item
(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))

julia> c1
(1, 1)

3、名前付きの2つのタプルをマージする

次の関数merge()を使用して、名前付きの2つのタプルをマージできます。例は以下のとおりです:

サンプル

julia> colors_shape = (top = "red", bottom = "green")
(top = "red", bottom = "green")

julia> shape_item = (corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))
(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))

julia> merge(shape_item, colors_shape)
(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0), top = "red", bottom = "green")

関数の引数としてのタプル

以下の例では、testFunc関数を作成し、タプルoptionsを引数として渡します:

サンプル:test.jl ファイルコード

# 関数を作成する
function testFunc(x, y, z; a=10, b=20, c=30)
    println("x = $x, y = $y, z = $z; a = $a, b = $b, c = $c")
 end

# タプルを作成する
 options = (b = 200, c = 300)
 
# 関数を実行し、タプルを引数として渡す
 testFunc(1, 2, 3; options...)

次のコマンドjuliaを使用して上記のファイルを実行すると、出力結果は次のとおりです:

$ julia test.jl
x = 1, y = 2, z = 3; a = 10, b = 200, c = 300

指定したパラメータがタプルの後ろにある場合は、タプル内の既存のパラメータを上書きします:

サンプル

# 関数を作成する
function testFunc(x, y, z; a=10, b=20, c=30)
    println("x = $x, y = $y, z = $z; a = $a, b = $b, c = $c")
 end

# タプルを作成する
 options = (b = 200, c = 300)

# 関数を実行し、タプルを引数として渡す。指定したパラメータがタプルの前にある場合は上書きされない
testFunc(1, 2, 3; b = 1000_000, options...)

# 関数を実行し、タプルを引数として渡す。指定したパラメータがタプルの後ろにある場合は上書きされる
testFunc(1, 2, 3; options..., b= 1000_000)

次のコマンドjuliaを使用して上記のファイルを実行すると、出力結果は次のとおりです:

$ julia test.jl
x = 1, y = 2, z = 3; a = 10, b = 200, c = 300
x = 1, y = 2, z = 3; a = 10, b = 1000000, c = 300
その他の拡張