{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "Tour of Scala
\n", "\n", "\n", "# Tuples\n", "\n", "In Scala, a tuple is a class that can hold elements of different types.\n", "Tuples are immutable.\n", "\n", "Tuples come in handy when we have to return multiple values from a function.\n", "\n", "A tuple can be created as:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "data": { "text/plain": [ "\u001b[36mingredient\u001b[39m: (\u001b[32mString\u001b[39m, \u001b[32mInt\u001b[39m) = (\u001b[32m\"Sugar\"\u001b[39m, \u001b[32m25\u001b[39m)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val ingredient = (\"Sugar\" , 25):Tuple2[String, Int]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This creates a tuple containing a String element and an Int element.\n", "\n", "Tuple in Scala is a series of classes: Tuple2, Tuple3, etc., through Tuple22.\n", "So when we create a tuple with n elements(n lying between 2 and 22), Scala basically instantiates\n", "one of the corresponding classes from the group, parameterized with types of constituent elements.\n", "For eg., ingredient is of type Tuple2[String, Int].\n", "\n", "## Accessing the elements\n", "\n", "Tuple elements are accessed using underscore syntax.\n", "'tuple._n' gives nth element(given there are that many elements)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sugar\n", "25\n" ] } ], "source": [ "println(ingredient._1) // Sugar\n", "\n", "println(ingredient._2) // 25" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Destructuring tuple data\n", "\n", "Scala tuple also supports destructuring." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sugar\n", "25\n" ] }, { "data": { "text/plain": [ "\u001b[36mname\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"Sugar\"\u001b[39m\n", "\u001b[36mquantity\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m25\u001b[39m" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val (name, quantity) = ingredient\n", "\n", "println(name) // Sugar\n", "\n", "println(quantity) // 25" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuple destructuring can be used in pattern matching too." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mercury is 57.9 millions km far from Sun\n", "Venus is 108.2 millions km far from Sun\n", "Blue planet is 149.6 millions km far from Sun\n", "Too far....\n", "Too far....\n" ] }, { "data": { "text/plain": [ "\u001b[36mplanetDistanceFromSun\u001b[39m: \u001b[32mList\u001b[39m[(\u001b[32mString\u001b[39m, \u001b[32mDouble\u001b[39m)] = \u001b[33mList\u001b[39m(\n", " (\u001b[32m\"Mercury\"\u001b[39m, \u001b[32m57.9\u001b[39m),\n", " (\u001b[32m\"Venus\"\u001b[39m, \u001b[32m108.2\u001b[39m),\n", " (\u001b[32m\"Earth\"\u001b[39m, \u001b[32m149.6\u001b[39m),\n", " (\u001b[32m\"Mars\"\u001b[39m, \u001b[32m227.9\u001b[39m),\n", " (\u001b[32m\"Jupiter\"\u001b[39m, \u001b[32m778.3\u001b[39m)\n", ")" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val planetDistanceFromSun = List((\"Mercury\", 57.9), (\"Venus\", 108.2), (\"Earth\", 149.6 ), (\"Mars\", 227.9), (\"Jupiter\", 778.3))\n", "\n", "planetDistanceFromSun.foreach{ tuple => {\n", " \n", " tuple match {\n", " \n", " case (\"Mercury\", distance) => println(s\"Mercury is $distance millions km far from Sun\")\n", " \n", " case p if(p._1 == \"Venus\") => println(s\"Venus is ${p._2} millions km far from Sun\")\n", " \n", " case p if(p._1 == \"Earth\") => println(s\"Blue planet is ${p._2} millions km far from Sun\")\n", " \n", " case _ => println(\"Too far....\")\n", " \n", " }\n", " \n", " }\n", " \n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or, in 'for' comprehension." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "-21\n", "1120\n" ] }, { "data": { "text/plain": [ "\u001b[36mnumPairs\u001b[39m: \u001b[32mList\u001b[39m[(\u001b[32mInt\u001b[39m, \u001b[32mInt\u001b[39m)] = \u001b[33mList\u001b[39m((\u001b[32m2\u001b[39m, \u001b[32m5\u001b[39m), (\u001b[32m3\u001b[39m, \u001b[32m-7\u001b[39m), (\u001b[32m20\u001b[39m, \u001b[32m56\u001b[39m))" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val numPairs = List((2, 5), (3, -7), (20, 56))\n", "\n", "for ((a, b) <- numPairs) {\n", "\n", " println(a * b)\n", " \n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The value () of type Unit is conceptually the same as the value () of type Tuple0. There can only be one value of this type since it has no elements.\n", "\n", "Users may sometimes find hard to choose between Tuples and case classes. As a rule, case classes are preferred choice if elements carry more meaning.\n", "\n", "\n", "Tour of Scala
\n", "" ] } ], "metadata": { "kernelspec": { "display_name": "Scala (2.13)", "language": "scala", "name": "scala213" }, "language_info": { "codemirror_mode": "text/x-scala", "file_extension": ".scala", "mimetype": "text/x-scala", "name": "scala", "nbconvert_exporter": "script", "version": "2.13.1" } }, "nbformat": 4, "nbformat_minor": 4 }