Software DevelopmentLearn How To Use Packages And Data Structures In Scala

Learn How To Use Packages And Data Structures In Scala

Data Structures
Packages are units that are used to organize code. Entities that can be contained in packages include classes, traits and objects. Using packages has several benefits. You prevent conflicts in naming and make it easy to locate and use components like classes and enumerations. This tutorial assumes you have set up a development environment in Eclipse. If you have not please refer to the setting up a development environment tutorial. Start eclipse and create a new project eduonix that will be used to demonstrate concepts. Click on file, new then scala project and give it a name.
new project
To define a package in Scala you use the keyword package that is then followed by name of the package. To package your code you create a Scala object (.scala) and place it there then store it in your preferred folder in project structure. The path to the folder where you store your Scala object becomes the name of the package. In the project created above create a scala object by clicking on file, new then Scala object. Give it a name and specify the folder where it will be stored. This folder will now become our package name.
object
In Scala there are two approaches to defining packages. You can use the Java like declaration or C# which relies on scope definition using curly braces. We will demonstrate the two approaches below. In Java like approach you begin with package name, declarations that are available at the package level then the code of the package. This is demonstrated below

//declare package and give it a name
package eduonix.src
//declare package level objects
//fields
val courseNumber = 56
// methods
def coursePoints(points: Int) {println(points)}
// enumerations
case class tutor(name: String, course: String)
object hadoop extends tutor(“sammy”, “Spark machine learning”)
object scala extends tutor (“sammy”, “Scala functions”)
object python extends tutor (“sammy”, “Python classes”)
//write your code here

package example
When you use the C# like approach you use curly braces to enforce scoping. The package above would now be created as shown below. Create a new scala object named clikePackage and add the code.

//declare package and give it a name
package eduonix
//declare package level objects
package object tutorials {
//fields
val courseNumber = 56
// methods
def coursePoints(points: Int) {println(points)}
// enumerations
case class tutor(name: String, course: String)
object hadoop extends tutor(“sammy”, “Spark machine learning”)
object scala extends tutor (“sammy”, “Scala functions”)
object python extends tutor (“sammy”, “Python classes”)
{//write your code here

  }
}

clikepackagescala
Package nesting is supported when writing Scala packages unlike Java which does not allow you to nest packages. To see how nesting happens look at the example below. Here the package spark is contained in package hadoop. When making a reference in class hql to scala class you use the short hand notation instead of using the long hand notation that begins at the base package.

 package hadoop {
      package spark {
        class scala
      }
      package hive {
        class hql {
          // No need to make a reference to hadoop.spark.scala
          val nav = new spark.scala
        }
      }
    }

To access the objects in packages you have created you use the import keyword. For example to access all objects in the Java like package we created above you add the objects in your code by importing as shown below. To import all objects you use an underscore

import eduonix.src._

To import a specific a object you specify its name instead of using an underscore as shown below. Now you are able to use the coursePoints method directly without using a prefix.

import eduonix.src.coursePoints

Importing packages in Scala is not restrictive as it is in Java. Scala offers the flexibility to import packages anywhere, rename and show only some members and refer to objects and packages.

Whenever you create a .scala source some implicit imports happen that import all objects in scala package, Predef package and java.lang package so you are able to reference them directly. It is like manually performing the imports below

import java.lang._
import scala._
import Predef._

Standard java classes are contained in the java.lang package. Standard classes and objects in Scala are contained in scala package. The Predef package contains types, methods and conversions frequently used by Scala programs.

Scala provides an extensive choice of mutable and immutable collections. Immutable collections can not be changed while mutable collections can be changed through an update or extension. All collections are found in the scala.collections package. Mutable collections are in the mutable sub package while immutable objects are in the immutable sub package. The collections in Scala are very extensive to be covered in a few paragraphs. Here we will demonstrate a few commonly used collections and leave the reader to refer to documentation available at http://docs.scala-lang.org/overviews/collections/overview.html.

Start Scala in interactive mode by running scala at the command line. Before we can use any collection we need to perform relevant imports.

import scala.collection.mutable._
import scala.collection.immutable._
import scala.collection.generic._

import pkg
Lists are one of the collections available in Scala. An example of how a list is created is shown below.
List(2, 8, 48, 34)
list
A set is a collection that can only contain unique values. An example is shown below. Observe from the output the duplicate value hadoop is dropped.

Set("hadoop", "spark", "scala", "hadoop")

set
A tuple is used to represent a collection of items while avoiding the use of a class. An example is illustrated below.

val courseCode = (“Scala”, 10)

tuple
Arrays are another collection available in Scala. Their use is shown below.

val ages = Array(23, 45, 67, 89, 41)

array
The different collection types do not give the same level of performance. They perform differently on operations such as insertions, prepending, updating and appending. This is what informs the decision on which collection type to use. A complete comparison of performance of collection types is available here .

This tutorial introduced you to packages which are components used in code organization. Definition of packages and where to store them was discussed. The two approaches for organizing packages were discussed. Package nesting was discussed. Importing code in packages so that it is available to other objects was discussed. Finally collections that are available were discussed.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -