Interview :: Scala
Higher order function is a function that either takes a function as an argument or returns a function. In other words, we can say a function which works with function is called a higher-order function.
Example
In Scala, functions can be composed from other functions. It is a process of composing in which a function represents the application of two composed functions.
Example
An anonymous function is a function that has no name but works as a function. It is good to create an anonymous function when you don't want to reuse it later. You can create anonymous function either by using ⇒ (rocket) or _ (underscore) wildcard in Scala.
Example
Expressions those are written in multiple lines are called multiline expression. In Scala, be careful while using multiline expressions.
Example
The above program does not evaluate the complete expression and return b here.
In Scala, the method may have multiple parameter lists. When a method is called with a fewer number of parameter lists, this will yield a function taking the missing parameter lists as its arguments.
Example
In Scala, you can define the function of variable length parameters. It allows you to pass any number of arguments at the time of calling the function.
Example
The object is a real-world entity. It contains state and behavior. Laptop, car, cell phone are the real world objects. An object typically has two characteristics:
1) State: data values of an object are known as its state.
2) Behavior: functionality that an object performs is known as its behavior.
The object in Scala is an instance of a class. It is also known as runtime entity.
The class is a template or a blueprint. It is also known as a collection of objects of similar type.
In Scala, a class can contain:
- Data member
- Member method
- Constructor
- Block
- Nested class
- Superclass information, etc.
Example
In Scala, you can create an anonymous object. An object which has no reference name is called an anonymous object. It is good to create an anonymous object when you don't want to reuse it further.
Example
In Scala, the constructor is not a special method. Scala provides primary and any number of auxiliary constructors. It is also known as default constructor.
In Scala, if you don't specify a primary constructor, the compiler creates a default primary constructor. All the statements of the class body treated as part of the constructor.