Scala (scalable language) is a general-purpose programming language providing support for functional programming and a strong static type system. Designed to be concise, many of Scala’s design decisions aimed to address criticisms of Java. Scala is a pure object-oriented language in the sense that every value is an object. Types and behavior of objects are described by classes and traits. Classes are extended by sub classing and a flexible mixing-based composition mechanism as a clean replacement for multiple inheritance. Scala is also a functional language in the sense that every function is a value.
Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and supports currying. Scala’s case classes and its built-in support for pattern matching model algebraic types used in many functional programming languages. Singleton objects provide a convenient way to group functions that aren’t members of a class.class.
Furthermore, Scala’s notion of pattern matching naturally extends to the processing of XML data with the help of right-ignoring sequence patterns, by way of general extension via extractor objects. In this context, for comprehensions are useful for formulating queries. These features make Scala ideal for developing applications like web services.
Scala is created by Martin Odersky (the father of javac), influenced by Java, Ruby, Smalltalk, ML, Haskell, and Erlang and was first released in 2003. It’s being used in the busiest websites like Twitter, LinkedIn, Tumblr, Netflix, and etc.
What is Scala?
Scala stands for scalable language. It is a modern multi paradigm programming language. It combines functional and object oriented programming. Object oriented make it simple to design complex systems and to adapt them to new demands and functional programming make it simple to create things rapidly from simple parts and it is compatible with Java.
Who is the author of Scala Programming language?
It is designed by Martin Oderskey, a German computer scientist. He also teaches a course, Functional Programming Principles in Scala and Functional Program Design in Scala on Coursera.
What makes Scala scalable?
There are the two feature of Scala i.e.
Object Oriented
Functional
Because of these feature Scala is scalable.
What is a ‘Scala set’?
What are methods through which operation sets are expressed? Scala set is a collection of pairwise elements of the same type. Scala set does not contain any duplicate elements. There are two kinds of sets, mutable and immutable.
What is a ‘Scala map’?
Scala Map is a collection of key value pairs wherein the value in a map can be retrieved using the key. Values in a Scala Map are not unique but the keys are unique. Scala supports two kinds of maps- mutable and immutable. By default, Scala supports immutable map and to make use of the mutable map, programmers have to import the scala.collection.mutable.Map class explicitly.
When programmers want to use mutable and immutable map together in the same program then the mutable map can be accessed at mutable. Map and the immutable map can just be accessed with the name of the map.
What are the major advantages of Scala Language?
Advantages of Scala Language are:-
Simple and Concise Code
Very Expressive Code
More Readable Code
100% Type-Safe Language
Immutability and No Side-Effects
More Reusable Code
More Modularity
Do More With Less Code
Very Flexible Syntax
Supports all OOP Features
Supports all FP Features. Highly Functional.
Less Error Prone Code
Better Parallel and Concurrency Programming
Highly Scalable and Maintainable code
Highly Productivity
Distributed Applications
Full Java Interoperability
Powerful Scala DSLs available
What are the drawbacks of Scala Language?
Less Readable Code
Bit tough to Understand the Code for beginners
Complex Syntax to learn
Less Backward Compatibility
What are the Scala variables?
Values and variables are two shapes that come in Scala. A value variable is constant and cannot be changed once assigned. It is immutable, while a regular variable, on the other hand, is mutable, and you can change the value.
The two types of variables are
var myVar : Int=0;
val myVal: Int=1;
What is recursion tail in scala?
‘Recursion’ is a function that calls itself. A function that calls itself, for example, a function ‘A’ calls function ‘B’, which calls the function ‘C’. It is a technique used frequently in functional programming. In order for a tail recursive, the call back to the function must be the last function to be performed.
What is ‘scala trait’ in scala?
‘Traits’ are used to define object types specified by the signature of the supported methods. Scala allows to be partially implemented but traits may not have constructor parameters. A trait consists of method and field definition, by mixing them into classes it can be reused.
When do you use Scala Traits?
Traits are mostly used, when we require dependency injection. Unlike Java, through Spring framework, dependency injection is achieved through annotations. In Scala, there are no annotations or no special package to be imported. We just need to initialize the class with the trait and done, dependency is injected.
What is Case Classes?
Case classes provides a recursive decomposition mechanism via pattern matching, it is a
Regular class which export their constructor parameter. The constructor parameters of case classes can be accessed directly and are treated as public values.
What is the use of tuples in Scala?
Scala tuples combine a fixed number of items together so that they can be passed around as whole. A tuple is immutable and can hold objects with different types, unlike an array or list.
What is function currying in Scala?
Currying is the technique of transforming a function that takes multiple arguments into a function that takes a single argument Many of the same techniques as language like Haskell and LISP are supported by Scala. Function currying is one of the least used and misunderstood one.
What are implicit parameters in Scala?
Implicit parameter is the way that allows parameters of a method to be “found”. It is similar to default parameters, but it has a different mechanism for finding the “default” value. The implicit parameter is a parameter to method or constructor that is marked as implicit. This means if a parameter value is not mentioned then the compiler will search for an “implicit” value defined within a scope.
What is a closure in Scala?
A closure is a function whose return value depends on the value of the variables declared outside the function.
What is Monad in Scala?
The simplest way to define a monad is to relate it to a wrapper. Any class object is taken wrapped with a monad in Scala. Just like you wrap any gift or present into a shiny wrapper with ribbons to make them look attractive, Monads in Scala are used to wrap objects and provide two important operations
Identity through “unit” in Scala
Bind through “flatMap” in Scala
What is Scala anonymous function?
In a source code, anonymous functions are called ‘function literals’ and at run time, function literals are instantiated into objects called function values. Scala provides a relatively easy
Syntax for defining anonymous functions.
What is an Expression?
Expression: Expression is a value that means it will evaluate to a Value. As an Expression returns a value, we can assign it to a variable.
Example:- Scala’s If condition, Java’s Ternary operator.
What is a Statement?
Statement: Statement defines one or more actions or operations. That means Statement performs actions. As it does not return a value, we cannot assign it to a Variable.
Example:- Java’s If condition.
What is the difference between var and value?
In scala, you can define a variable using either a, val or var keywords. The difference between val and var is, var is much like java declaration, but val is little different. We cannot change the reference to point to another reference, once the variable is declared using val. The variable defined using var keywords are mutable and can be changed any number of times.
What is the difference between concurrency and parallelism?
People often confuse with the terms concurrency and parallelism. When several computations execute sequentially during overlapping time periods it is referred to as concurrency whereas when processes are executed simultaneously it is known as parallelism. Parallel collection, Futures and Async library are examples of achieving parallelism in Scala.
What is the difference between a trait and an abstract class?
The first difference is that a class can only extend one other class, but an unlimited number of traits. While traits only support type parameters, abstract classes can have constructor parameters. Also, abstract classes are interoperable with Java, while traits are only interoperable with Java if they do not contain any implementation.
What’s the difference ‘Nil’, ‘Null’, ‘None’ and ’Nothing’ in Scala?
Nil – It’s a handy way of initializing an empty list since, Nil, is an object, which extends List [Nothing].
Null – It’s a sub-type of AnyRef type in Scala Types hierarchy. As Scala runs on JVM, it uses NULL to provide the compatibility with Java null keyword, or in Scala terms, to provide type for null keyword, Null type exists. It represents the absence of type information for complex types that are inherited from AnyRef.
None – In programming, there are many circumstances, where we unexpectedly received null for the methods we call. In java these are handled using try/catch or left unattended causing errors in the program. Scala provides a very graceful way of handling those situations. In cases, where you don’t know, if you would be able to return a value as expected, we can use Option [T]. It is an abstract class, with just two sub-classes, Some [T] and none. With this, we can tell users that, the method might return a T of type Some [T] or it might return none.
Nothing – It’s a sub-type of all the types exists in Scala Types hierarchy. It helps in providing the return type for the operations that can affect a normal program’s flow. It can only be used as a type, as instantiation of nothing cannot be done. It incorporates all types under AnyRef and AnyVal. Nothing is usually used as a return type for methods that have abnormal termination and result in an exception.
What are option, some and none in scala?
‘Option’ is a Scala generic type that can either be ‘some’ generic value or none. ‘Queue’ often uses it to represent primitives that may be null.
How can you format a string?
To format a string, use the .format () method, in scala you can use
Val formatted= “%s %i”.format (mystring.myInt)
Why scala prefers immutability?
Scala prefers immutability in design and in many cases uses it as default. Immutability can help when dealing with equality issues or concurrent programs.
What are the four types of scala identifiers?
The four types of identifiers are
Alpha numeric identifiers
Operator identifiers
Mixed identifiers
Literal identifiers
What are the different types of Scala literals?
The different types of literals in scala are
Integer literals
Floating point literals
Boolean literals
Symbol literals
Character literals
String literals
Multi-Line strings
What are High Order Functions in Scala?
High order functions are functions that can receive or return other functions. Common examples in Scala are the filter, map, and flatMap functions, which receive other functions as arguments.
What is a Closure in Scala?
A closure is also known as an anonymous function whose return value depends upon the value of the variables declared outside the function.
What is ‘Unit’ and ‘()’ in Scala?
The ‘Unit’ is a type similar to void in Java. You can say it is a Scala equivalent of the void in Java, while still providing the language with an abstraction over the Java platform. The empty tuple ‘()’ is a term representing a Unit value in Scala.
What is Akka, Play, and Sleek in Scala?
Akka is a concurrency framework in Scala which uses Actor based model for building highly concurrent, distributed, and resilient message-driven applications on the JVM. It uses high-level abstractions like Actor, Future, and Stream to simplify coding for concurrent applications.
It also provides load balancing, routing, partitioning, and adaptive cluster management. If you are interested in learning Akka, I suggest reading Akka in Action and Akka documentation; both are great resources to learn Akka.
Play is another Scala framework to build web applications in both Java and Scala. The Play Framework is based on a lightweight, stateless, web-friendly architecture. It is built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.
Slick is a modern database query and access library for Scala. It allows you to work with stored data almost as if you were using Scala collections while at the same time giving you full control over when a database access happens and which data is transferred. You can write your database queries in Scala instead of SQL, thus profiting from the static checking, compile-time safety, and compositionality of Scala. Slick features an extensible query compiler which can generate code for different backends.
What is SBT tool?
SBT stands for Scala Build Tool. It is an open-source build tool for Scala and Java projects, similar to Java’s Maven and Ant. It helps to compile/build/test code.
What is a monad?
A monad is something to which we can pass functions and manipulate the underlying object’s data. We don’t need to manipulate the object directly. Hence, a monad is an object that wraps another.
What are vararg arguments?
With varargs, we can pass a variable number of arguments to a method.
In Scala to Append into a List, We have the following methods:
use “:+” single value var myList = List.empty[String] myList :+= “a”
How do you define a function in Scala?
def keyword is used to define the Function in Scala.
object add {def addInt( a:Int, b:Int ) : Int = {var sum:Int = 0sum = a + breturn sum}}
Can you explain tail-recursion?
Recursion is when a function makes a call to itself. When we place this call as the last action performed in the function, we can call the function tail-recursive.
scala> def factorial(n:Int):Int={
| if(n==1) return 1
| n*factorial(n-1)
| }
factorial: (n: Int)Int
scala> factorial(5)
Int = 120
How do you create a singleton object in Scala?
Using object keyword we can create a singleton object.
In the Source code, Anonymous functions are called ‘Function literals’ and at run time, function literals are instantiated into objects called Function values. Scala provides a relatively easy Syntax for defining Anonymous functions.
Syntax:(z:Int, y:Int)=> z*yOr(_:Int)*(_Int)
What is a HOF?
Higher Order Function (HOF) is also a function but which performs one, two or both of the following things: