YouTip LogoYouTip

Julia Tuples

Julia's tuples are similar to arrays, both are ordered collections of elements, but the difference is that tuple elements cannot be modified. Additionally, tuples use parentheses (...), while arrays use square brackets [...]. Creating a tuple is simple, just add elements in parentheses and separate them with commas. Many functions that work with arrays can also be used with tuples. The following example: ## Example ```julia julia> tupl=(5,10,15,20,25,30)# Create a tuple (5, 10, 15, 20, 25, 30) julia> tupl (5, 10, 15, 20, 25, 30) julia> tupl[3:end]# Output tuple from third to last element (15, 20, 25, 30) julia> tupl = ((1,2),(3,4))# Create a 2D tuple ((1, 2), (3, 4)) julia> tupl# Access 2D tuple element, output first dimension tuple (1, 2) julia> tupl# Access 2D tuple element, output second element of first dimension tuple 2 The elements of a tuple cannot be modified. If we try to modify it, we will get an error: ## Example ```julia julia> tupl2=(1,2,3,4) (1, 2, 3, 4) julia> tupl2=0 ERROR: MethodError: no method matching setindex!(::NTuple{4, Int64}, ::Int64, ::Int64) Stacktrace: top-level scope @ REPL:1 * * * ## Named Tuples We can name tuples to make them easier to access. The following lists several different ways to name tuples. ### 1. Separate naming of keys and values in tuples The keys and values in a tuple can be named separately. Here's an example: ## Example ```julia 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)) We can use the . dot notation to access tuples: ## Example ```julia julia> shape_item2.corner1 (100, 100) julia> shape_item2.corner2 (200, 200) ### 2. Keys and values in the same tuple Keys and values can be in the same tuple. Here's an example: ## Example ```julia julia> shape_item = (corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0)) (corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0)) We can use the . dot notation to access tuples: ## Example ```julia 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)) We can also access all values like a regular tuple, as shown below: ## Example ```julia julia> c1, c2, center = shape_item (corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0)) julia> c1 (1, 1) ### 3. Merging two named tuples We can use the merge() function to merge two named tuples. Here's an example: ## Example ```julia 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") * * * ## Tuples as Function Parameters In the following example, we create a **testFunc** function and pass the tuple **options** as a parameter: ## Example: test.jl file code ```julia # Create function 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 # Create tuple options = (b = 200, c = 300) # Execute function, pass tuple as parameter testFunc(1, 2, 3; options...) Use the julia command to execute the above file, and the output is: ```bash $ julia test.jl x = 1, y = 2, z = 3; a = 10, b = 200, c = 300 If the specified parameters come after the
← Julia Complex And Rational NumPython3 Func Reload β†’