Can you give me a brief description of Tuple
? And when to use it over List
and Vector
?
A tuple is an object that can hold a number of elements and a vector containing multiple number of such tuple is called a vector of tuple. The elements can be of different data types. The elements of tuples are initialized as arguments in order in which they will be accessed.
An n-tuple vector x is defined as an ordered set of n numbers. Usually we write these numbers x1,...,xn in a column in the order indicated by their subscripts. The transpose of x is the row vector x = [x1,...,xn] and the transpose of x is x again.
The primary difference between tuples and lists is that tuples are immutable as opposed to lists which are mutable. Therefore, it is possible to change a list but not a tuple. The contents of a tuple cannot change once they have been created in Python due to the immutability of tuples.
Tuple is usually represented in Clojure through an associative data structure such as map {:name "david" :age 35}
or record.
A vector ["david" 35]
offers fast positional access (= 35 (nth ["david" 35] 1))
, and you can store different types.
A list ("david" 35)
or ("david" "justin" "david")
offers fast access from the head and fast forward traversal. Although it can hold different types it would be most common for it to contain a single type, possibly containing duplicates, in a determined order. Contrast to a set #{"david" "justin"}
which would contain no duplicates and is optimised for checking membership/presence.
Sorted sets (sorted-set
) and maps (sorted-map
) maintain the order of objects using a comparator.
Check out 4clojure and clojuredocs.org. Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With