Res11: (String, Int, Boolean, String) = (Scala,1,true,yay) We can access elements with the apply method just like Seqs: scala> t1(0)Īnd we can concatenate tuples with the usual operators: scala> t1 :+ "yay" But with Shapeless we now have head and tail: scala> t1.head T1: (String, Int, Boolean) = (Scala,1,true) The last import brings in the helpers for tuples. Type in expressions to have them evaluated.įirst, import the packages that we need: scala> import shapeless._ Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25). Then launch the Scala REPL with the console command: > console > set libraryDependencies += "com.chuusai" %% "shapeless" % "2.2.4" Launch sbt (or activator) and enter the following commands to bring in the library: > set scalaVersion := "2.11.7" Let's take a look at this in action! As usual, I will use sbt and the REPL to interactively explore Shapless. Does this sound kind of like a Tuple2? Shapeless provides a little syntax sugar that makes a standard Tuple behave like a collection. This is defining a collection that has two elements with a String first and an Int second. ![]() Conceptually, an HList with a String and Int could be thought of as HList. Contrast this to the standard Scala collections where every element in the collection must be the same type. Shapeless provides a type called HList, which is a collection of heterogenous elements where each element in the collection has it's own type. The first element in this Tuple2 must be a String while the second must be an Int. A Tuple with a String and Int is typed as Tuple2. The standard Tuple has a type annotation for each element. We can make this situation better with the addition of the shapeless library. By default we're limited to accessing the elements through methods like _1 and _2, and have none of the handy features of Seq. It's a reasonable expectation, but unfortunately they don't do this. :13: error: value _4 is not a member of (Int, String, Console.type)īefore moving forward, let’s take an example to demonstrate how we use tuples.Many students in my Scala classes expect that Tuples will behave like collections by providing methods like head and tail, or providing access to elements by an index like Seq types provide. If we try to access an index that doesn’t exist in the tuple, it raises a console error: scala> t._4 Note that here, the indexing doesn’t begin at 1 for once. This is how Scala decides the types of tuples- as Tuple1, Tuple2, Tuple3, and so on. That for (“Red”,”Golden”,’o’,7,’g’,4,”Ayushi”) is Tuple6(String, String, Char, Int, Char, Int, String). ![]() The type for (7,”Ayushi”) is Tuple2(Int, String). The number of elements in a Scala tuple and their types decides a tuple’s overall type. ![]() We can also use Scala tuples to pass a list of data values as messages between actors in concurrent programming. You can also declare a tuple the following way: Scala> val t=new Tuple3(1,"Ayushi",Console) Note that this is syntactic sugar for this: T: (Int, String, Console.type) = (1,Ayushi, ) Unlike a list or an array, we can put different types of objects in it they are also immutable. Scala tuple holds together a fixed number of items so we can pass them around as a whole. ![]() What are Scala Tuples? How do we iterate over them? How do we convert tuples in Scala to strings or swap their elements? These are the questions we will answer in this article. Keeping you updated with latest technology trends, Join TechVidvan on Telegram 1.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |