Scala Tutorial for Beginners
Introduction to Scala
Scala programming language designed to express common programming patterns in an elegant, concise and type-safe way. Scala is a hybrid programming language. Scala has many features like currying, type-inference, lazy evaluation, immutability, pattern matching.
What is Scala?
- Scala is the acronym for "Scalable Language".
- Scala is a general-purpose programming language.
- Scala is a modern multi-paradigm Concurrent, imperative, functional and object-oriented programming language.
- Moreover, Scala has an advanced type system supporting algebraic higher-order type covariance and contra-variance and an anonymous type.
- There are some features, which are present in Scala but not present in Java: Operator overloading, named parameters, optional parameters, and raw strings.
- Same way the feature Checked Exceptions is present in Java but not in Scala.
- The file extensions of Scala is '.scala,''.sc.'
History of Scala language
Scala software was released under Apache License. Martin Odersky started designing Scala in 2001. He combined the features of functional programming language and Petri nets to build the idea in the development of Scala whereas Petri nets are known as PT net or Place/Transition net. Petri nets are mathematical modeling language.
Petri nets are used for the description of Distributed Systems.
- Scala released publically in January 2004 on the Java platform.
- The second version of Scala released in 2006.
- The latest and stable version of Scala is 'Scala 2.12.8' released in December 2018.
The Scala Native Compiler: The Scala Native is a Scala Compiler. The Scala Native compiler targets the LLVM compiler infrastructure to produce executable code. The development of Scala native project starts in 2015, with the goal ‘to be faster than just-in-time compilation’ for the Java Virtual Machine. This project eliminates the initial runtime and provides the ability ‘to call the native routines directly.’ The Scala native project released first time in 14th March 2017.
The Reference Scala Compiler: The reference Scala compiler is used for .NET framework. The Reference Scala Compiler released in June 2004, but this reference compiler was dropped officially in 2012.
Testing the Scala Code
Testing of the Scala Code/ Scala Programs can be possible in several ways such as Junit, TestNG, Specs2, ScalaTest, ScalaCheck, and ScalaMock. Let us see them one by one.
Junit and TestNG: Junit and TestNGis the most popular testing framework and both are written in Java.
Specs2: Specs2 is a library. This library is used to write executable software specification.
ScalaTest: It supports multiple testing styles. ScalaTest can integrate with java based testing frameworks.
ScalaCheck: ScalaCheck is a library for testing. ScalaCheck is similar to Haskell's QuickCheck. QuickCheck is a library designed to assist in software testing. QuickCheck generates test cases for test suites.
Concurrency in Scala
Scala supports two important strategies for concurrency:
Thread-based concurrency and type-safe Actor-based concurrency. Thread-based concurrency derived from Java and Type-safe Actor-based Concurrency inspired by Erlang.
Software Transactional Memory and Parallel Collections are two significant additions to the Scala's Concurrency. Parallel Collections allow for parallelizing necessary collection transformers across multiple threads like a map, for each.
Features of Scala
Following is the list of features of Scala programming language:
- Type Inference
- Lazy Evaluation
- Statically typed
- Scala runs on JVM
- Scala can execute Java Code
Let us see all the Scala features in details:
- Type Inference
Scala programming language detects the type of expressions automatically. In Scala programming language, no need to declare the type. That will reduce the complexity of the programming language, for example, the difficulty occurs in case of typecasting.
In Scala programming language, each variable is immutable by default. Scala achieves the concurrency with the help of immutable data, and Immutability plays a vital role to manage the concurrency control.
- Lazy Non-Strict Evaluation
Scala performs a lazy evaluation. As the name says 'Lazy Evaluation', it is clear that 'it delays the evaluation of an expression.' Lazy Evaluation also is known as 'Call-by-needis' or ‘Non-Strict Evaluation.' Lazy evaluation is an evaluation strategy, which delays the evaluation/ computation/ calculation of expression until the result/ value needed. Lazy evaluation avoids repetitive calculations.
Scala programming language is concise. The meaning of concise is to give abundant information clearly in minimum words. Scala programming language uses the less number of lines of code in comparison with Java programming language for building the same logic.
Scala is a high-level programming language. The high-level programming language has strong abstraction from the inner details of the computer. A high-level language is friendlier with the user than low-level language.
- Scala is Extensible
Scala allows adding new language in the form of libraries. Result to this, and Scala can add extensions to domain-specific languages as extensions to develop the Domain Specific Projects.
- Statically typed
Statically typed language is a language in which type checking is done at compile time. Scala is a statically typed language. 'Type-Checking' is a process ‘to verify’ and ‘to enforce’ the constraints or rules of 'Types.' C, Pascal, and Rust are examples of statically typed language. Unlike those languages, Scala does not expect ‘to provide the redundant type information.'
Scala programming language is an object-oriented language. Everything present in the Scala is in the form of the object. Classes and traits describe objects type and behavior. Scala provides a concurrent ready environment to object-oriented on a Java Virtual Machine JVM platform.
Scala programming language is a functional language. It provides multiple features of functional programming like literal function, singleton objects, nested functions, curried functions, and higher-order functions.
- FunctionLiterals: Scala supports function literals. Function literals are the lightweight syntax for defining the anonymous function. The meaning of anonymous is unknown. An anonymous function does not have a name, i.e. function without a name.
- Singleton Objects: Scala supports singleton objects. Singleton objects provide a way to group the non-member functions of the class.
- Nested Functions: Scala Programming language allows nested functions.
- Curried Functions: Scala programming supports curried function. A curried function is a function, which accepts multiple arguments, one at a time. Let us understand with an example. Suppose function has three parameters. The curried version of that function 'accepts a single argument and returns a function, which accepts the second argument and second function accepts the third argument like cascading and returns the final result.'
- Higher Order Function: Scala programming language supports higher-order function. Higher order function is a function in which either function takes a function as input or function has functioned as return type.
Function Return Type: Function
Function Parameter: Function
- Scala runs on the JVM
When Scala compiler compiles the scala source code, then Scala source code is converted into ‘java byte code.' Generated java byte code executes on Java virtual machine, i.e. JVM.
11. Scala can execute Java code
Scala runs Java code. Scala programming language allows using java classes from the Java software development kit SDK. Scala allows for creating custom Java classes.
Technologies Built in Scala
Following is the list of technologies built in Scala programming language.
- Apache Spark
- Apache Kafka
- Apache Samza
- Kafka Manager Console
Technologies built in Scala categorized based on "data processing speed", "Concurrency", 'Performance' and other cool stuff. As the first category 'data processing speed' Apache Spark, Apache Kafka, Scalding and Apache Samza are examples of technology, which can process event stream in real-time. Finagle, and Akka is the example of technologies having high concurrency and high performance. Adam and Lichess come under the category of cool stuff.
Following diagram clears how the technologies categorized into groups based on their features.
Hadoop community realized the need for the replacement of MapReduce from last few years. Apache Spark is the replacement for the MapReduce. This replacement was done in late 2013. Apache spark becomes the fastest and ‘General Purpose Engine' for largescale data processing. Apache Spark developed at UC Berkeley.
Twitter built Scalding using Scala programming language. Functional programming is ideal for Big Data. Scalding provides the functional operations and provides all benefits of Scala syntax.
Apache Kafka is a publish-subscribe messaging system. Apache Kafka has written in Scala programming language and highlights the power of Scala programming language.
LinkedIn built the ‘Apache Kafka,' and it is the center of LinkedIn infrastructure. Apache Kafka handles hundreds of megabytes of read-write traffic per second from thousands of clients.
Kafka Manager Console
Kafka manager console is built by Yahoo. Kafka manager console is an open source console. Most of Yahoo teams and media analytics team uses Kafka manager console.
Apache Samza has written in Scala programming language. Apache Samza is a distributed framework, and it is a stream processing framework. Apache Samza uses 'Kafka' for messaging purpose. Besides, Apache Samza uses "Apache Hadoop Yarn" for multiple tasks such as fault tolerance, security, resource management, and processor isolation.
Akka is a high concurrency tool. Akka is written in Scala programming language. Akka is a very fast and concurrent framework and this framework used for building the distributed applications. Akka handles all low-level IO operations.
Finagle is one of the best use cases of Scala programming language. Finagle used for constructing high scalability services through concurrency and optimal use of system resources. Finagle is an extensible Remote procedure call RPC system for the Java virtual system JVM. This Finagle RPC system used to construct high concurrency servers. Finagle implements uniform server APIs and client APIs for several protocols.