To access name, it is easier to remember the word rather than its sequence. For example, a tuple has 5 elements and name is 3 elements. Remembering names that are logically matching is easier than remembering the number. This method is quite useful when you have a handful of elements in your tuple. Code to explain the initialization and accessing of tuples object M圜lass įor ease of access, you can optionally assign the elements of a tuple name which can later be used to call them. n, you have to type in tuple._n, where the tuple is the name of a tuple and n th element is fetched.įor example, if we need fetch 6 th element of the tuple t then we will type t._6 to access it. The access method for accessing an element of a tuple is quite easy and convenient. There is one more method to define a tuple by using the -> operator: Val tuple_name = new tuple 2(data 1, data 2) You can define a tuple by mentioning its tuple class, the class is simply tuples, where n is the number of elements the tuple has. This is a shortcut way of defining a tuple. You can easily use tuples in your scala program for usage. The maximum number of miscellaneous elements that a tuple can have is twenty-two. The Scala tuples are immutable, i.e the objects of different type can be stored in the tuple but the value of these objects cannot be changed. The elements of Scala tuples may or may not be the same data type. Submitted by Shivang Yadav, on July 20, 2019Ī tuple is a data structure that has multiple elements. Scala programming also supports tuples and in this Scala tutorials on tuples, we will learn about tuples with examples. The following commands are used to compile and execute this program.Scala Tuples: Tuple is a collection of similar or different multiple elements. Try the following example program to swap the elements. You can use Tuple.swap method to swap the elements of a Tuple2. The following commands are used to compile and execute this program. Println("Concatenated String: " + t.toString() ) Try the following example program to convert to String. You can use Tuple.toString() method to concatenate all the elements of the tuple into a string. Try the following example program to iterate over tuples. You can use Tuple.productIterator() method to iterate over all the elements of a Tuple. The type class TupleFunction provides a way to abstract directly over a function of any arity converting it to an equivalent function that receives all arguments in a single tuple. For each TupleN type, where 1 scalac Demo.scala There currently is an upper limit of 22 in the Scala if you need more, then you can use a collection, not a tuple. Tuples are of type Tuple1, Tuple2, Tuple3 and so on. Thus, the type of (99, "Luftballons") is Tuple2. The actual type of a tuple depends upon the number and of elements it contains and the types of those elements. Which is syntactic sugar (short cut) for the following − The following is an example of a tuple holding an integer, a string, and the console. Unlike an array or list, a tuple can hold objects with different types but they are also immutable. Scala tuple combines a fixed number of items together so that they can be passed around as a whole. val otherExample: (Double, String) ('this is not a Double', 'this is a String') This line will fail to compile because the first element should be a Double but is assigned a String. Scala Collections - Multi-Dimensional Array Tuple are very powerful in Scala because you can use the compiler to help you.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |