English 中文(简体)
Julia - Tuples
  • 时间:2024-11-03

Jupa - Tuples


Previous Page Next Page  

Similar to an array, tuple is also an ordered set of elements. Tuples work in almost the same way as arrays but there are following important differences between them −

    An array is represented by square brackets whereas a tuple is represented by parentheses and commas.

    Tuples are immutable.

Creating tuples

We can create tuples as arrays and most of the array’s functions can be used on tuples also. Some of the example are given below −


jupa> tupl=(5,10,15,20,25,30)
(5, 10, 15, 20, 25, 30)

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

jupa> tupl[3:end]
(15, 20, 25, 30)

jupa> tupl = ((1,2),(3,4))

((1, 2), (3, 4))

jupa> tupl[1]
(1, 2)

jupa> tupl[1][2]
2

We cannot change a tuple:
jupa> tupl[2]=0
ERROR: MethodError: no method matching setindex!(::Tuple{Tuple{Int64,Int64},Tuple{Int64,Int64}}, ::Int64, ::Int64)
Stacktrace:
 [1] top-level scope at REPL[7]:1

Named tuples

A named tuple is simply a combination of a tuple and a dictionary because −

    A named tuple is ordered and immutable pke a tuple and

    Like a dictionary in named tuple, each element has a unique key which can be used to access it.

In next section, let us see how we can create named tuples −

Creating named tuples

You can create named tuples in Jupa by −

    Providing keys and values in separate tuples

    Providing keys and values in a single tuple

    Combining two existing named tuples

Keys and values in separate tuples

One way to create named tuples is by providing keys and values in separate tuples.

Example


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

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

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

We can access the elements by using dot(.) syntax −


jupa> shape_item2.corner1
(100, 100)

jupa> shape_item2.corner2
(200, 200)

Keys and values in a single tuple

We can also create named tuples by providing keys and values in a single tuple.

Example


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

We can access the elements by using dot(.) syntax −


jupa> shape_item.corner1
(1, 1)

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

jupa> shape_item.center
(0, 0)

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

We can also access all the values as with ordinary tuples as follows −


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

jupa> c1
(1, 1)

Combining two named tuples

Jupa provides us a way to make new named tuples by combining two named tuples together as follows −

Example


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

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

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

Named tuples as keyword arguments

If you want to pass a group of keyword arguments to a function, named tuple is a convenient way to do so in Jupa. Following is the example of a function that accepts three keyword arguments −


jupa> function ABC(x, y, z; a=10, b=20, c=30)
         println("x = $x, y = $y, z = $z; a = $a, b = $b, c = $c")
      end
ABC (generic function with 1 method)

It is also possible to define a named tuple which contains the names as well values for one or more keywords as follows −


jupa> options = (b = 200, c = 300)
(b = 200, c = 300)

In order to pass the named tuples to the function we need to use; while calpng the function −


jupa> ABC(1, 2, 3; options...)
x = 1, y = 2, z = 3; a = 10, b = 200, c = 300

The values and keyword can also be overridden by later function as follows −


jupa> ABC(1, 2, 3; b = 1000_000, options...)
x = 1, y = 2, z = 3; a = 10, b = 200, c = 300

jupa> ABC(1, 2, 3; options..., b= 1000_000)
x = 1, y = 2, z = 3; a = 10, b = 1000000, c = 300
Advertisements