Ch 12 - Tuples Flashcards
Think Julia
What is a Tuple?
A tuple is a sequence of values. The values can be of any type, and they are indexed by integers, so in that respect tuples are a lot like arrays. The important difference is that tuples are immutable and that each element can have its own type.
Syntactically, a tuple is a comma-separated list of values:
julia > t = ‘a’, ‘b’, ‘c’, ‘d’, ‘e’
(‘ a’, ‘b’, ‘c’, ‘d’, ‘e’)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4587-4599). Kindle Edition.
Think Julia
Must a tuple be enclosed in parentheses?
Although it is not necessary, it is common to enclose tuples in parentheses:
julia > t = (‘ a’, ‘b’, ‘c’, ‘d’, ‘e’)
(‘ a’, ‘b’, ‘c’, ‘d’, ‘e’)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4603-4606). Kindle Edition.
Think Julia
What must you do to create a tuple with just one element?
To create a tuple with a single element, you have to include a final comma:
julia > t1 = (‘ a’,)
(‘ a’,)
julia > typeof( t1)
Tuple{ Char}
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4607-4611). Kindle Edition.
Think Julia
What function can be used to create a tuple?
Another way to create a tuple is using the built-in function tuple. With no argument, it creates an empty tuple:
julia > tuple()
()
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4617-4621). Kindle Edition.
Think Julia
How can you modify the elements of a tuple?
Because tuples are immutable, you can’t modify the elements.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4647-4649). Kindle Edition.
Think Julia
What is a: tuple assignment
This solution is cumbersome; tuple assignment is more elegant:
a, b = b, a
The left side is a tuple of variables; the right side is a tuple of expressions. Each value is assigned to its respective variable. All the expressions on the right side are evaluated before any of the assignments.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4668-4672). Kindle Edition.
Think Julia
How many variables can a function return?
Tuples as Return Values
Strictly speaking, a function can only return one value, but if the value is a tuple, the effect is the same as returning multiple values. For example, if you want to divide two integers and compute the quotient and remainder, it is inefficient to compute x ÷ y and then x % y. It is better to compute them both at the same time.
The built-in function divrem takes two arguments and returns a tuple of two values, the quotient and remainder. You can store the result as a tuple:
julia > t = divrem( 7, 3)
(2, 1)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4695-4707). Kindle Edition.
Think Julia
What can a tuple do as a return variable?
Tuples as Return Values
Strictly speaking, a function can only return one value, but if the value is a tuple, the effect is the same as returning multiple values. For example, if you want to divide two integers and compute the quotient and remainder, it is inefficient to compute x ÷ y and then x % y. It is better to compute them both at the same time.
The built-in function divrem takes two arguments and returns a tuple of two values, the quotient and remainder. You can store the result as a tuple:
julia > t = divrem( 7, 3)
(2, 1)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4695-4707). Kindle Edition.
Think Julia
What is a: …
Variable-Length Argument Tuples
Functions can take a variable number of arguments. A parameter name that ends with … gathers arguments into a tuple. For example, printall takes any number of arguments and prints them:
function printall( args…)
….println( args)
end
The gather parameter can have any name you like, but args is conventional. Here’s how the function works:
julia > printall( 1, 2.0, ‘3’)
(1, 2.0, ‘3’)
The complement of gather is scatter. If you have a sequence of values and you want to pass it to a function as multiple arguments, you can use the … operator. For example, divrem takes exactly two arguments; it doesn’t work with a tuple:
julia > t = (7, 3);
julia > divrem( t)
ERROR: MethodError: no method matching divrem(:: Tuple{ Int64, Int64})
But if you scatter the tuple, it works:
julia > divrem( t…)
(2, 1)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4739-4752). Kindle Edition.
Think Julia
What is a: gather (with regard to arguments)
Variable-Length Argument Tuples
Functions can take a variable number of arguments. A parameter name that ends with … gathers arguments into a tuple. For example, printall takes any number of arguments and prints them:
function printall( args…)
….println( args)
end
The gather parameter can have any name you like, but args is conventional. Here’s how the function works:
julia > printall( 1, 2.0, ‘3’)
(1, 2.0, ‘3’)
The complement of gather is scatter. If you have a sequence of values and you want to pass it to a function as multiple arguments, you can use the … operator. For example, divrem takes exactly two arguments; it doesn’t work with a tuple:
julia > t = (7, 3);
julia > divrem( t)
ERROR: MethodError: no method matching divrem(:: Tuple{ Int64, Int64})
But if you scatter the tuple, it works:
julia > divrem( t…)
(2, 1)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4739-4752). Kindle Edition.
Think Julia
What is a: scatter (with regard to arguments)
Variable-Length Argument Tuples
Functions can take a variable number of arguments. A parameter name that ends with … gathers arguments into a tuple. For example, printall takes any number of arguments and prints them:
function printall( args…)
….println( args)
end
The gather parameter can have any name you like, but args is conventional. Here’s how the function works:
julia > printall( 1, 2.0, ‘3’)
(1, 2.0, ‘3’)
The complement of gather is scatter. If you have a sequence of values and you want to pass it to a function as multiple arguments, you can use the … operator. For example, divrem takes exactly two arguments; it doesn’t work with a tuple:
julia > t = (7, 3);
julia > divrem( t)
ERROR: MethodError: no method matching divrem(:: Tuple{ Int64, Int64})
But if you scatter the tuple, it works:
julia > divrem( t…)
(2, 1)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4739-4752). Kindle Edition.
Think Julia
What is a: zip()
zip is a built-in function that takes two or more sequences and returns a collection of tuples where each tuple contains one element from each sequence. The name of the function refers to a zipper, which joins and interleaves two rows of teeth.
This example zips a string and an array:
julia > s = “abc”;
julia > t = [1, 2, 3];
julia > zip( s, t)
Base.Iterators.Zip{ Tuple{ String, Array{ Int64,1}}}((“ abc”, [1, 2, 3]))
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4768-4777). Kindle Edition.
Think Julia
What is a: iterator
The result is a zip object that knows how to iterate through the pairs. The most common use of zip is in a for loop:
julia > for pair in zip( s, t)
…………….println( pair)
…………end
(‘ a’, 1)
(‘ b’, 2)
(‘ c’, 3)
A zip object is a kind of iterator, which is any object that iterates through a sequence. Iterators are similar to arrays in some ways, but unlike arrays, you can’t use an index to select an element from an iterator.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4778-4788). Kindle Edition.
Think Julia
How can tuple assignment be used when looping?
You can use tuple assignment in a for loop to traverse an array of tuples:
julia > t = [(‘ a’, 1), (‘ b’, 2), (‘ c’, 3)];
julia > for (letter, number) in t
…………….println( number, “ “, letter)
…………end
1 a
2 b
3 c
Each time through the loop, Julia selects the next tuple in the array and assigns the elements to letter and number. The parentheses around (letter, number) are compulsory.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4803-4817). Kindle Edition.
Think Julia
How can a dictionary be used in a for loop?
Dictionaries and Tuples
Dictionaries can be used as iterators that iterate the key-value pairs. You can use a dictionary in a for loop like this:
julia > d = Dict(‘ a’ = > 1, ‘b’ = > 2, ‘c’ = > 3);
julia > for (key, value) in d
…………….println( key, “ “, value)
…………end
a 1
c 3
b 2
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4841-4854). Kindle Edition.