Labels

Tuesday, April 12, 2022

Functional Programming (FP) in Scala (Class -33)

When programs get larger, you need some way to divide them into smaller, more manageable pieces. For dividing up control flow, Scala offers an approach familiar to all experienced programmers: divide the code into functions.

 In fact, Scala offers several ways to define functions that are not present in Java.

 The most common way to define a function is as a member of some object; such a function is called a ‘method’.

 The main function in Scala is defined as,

def main(args: Array[String]) {


 Functions in Scala are called First Class Citizens. Not only can you define functions and call them, but you can write down functions as unnamed literals and then pass them around as values. In other words, If you can treat a Function as a Value, it is a First Class Function.

a)   We can assign an object to a function à function object.

b)      We can assign one function object to another function object.

c)       Function object can be passed as a parameter to another function / method.

d)      Functions object are returned from a method or function.

 Point’s c & d are Higher Order Functions. 

Match Expressions in Scala (Class -32)

 Scala has a concept of a match expression. This is also called “Pattern Matching”.

    Here, “match” keyword is used instead of switch statement. “Match” is always defined in Scala’s root class to make its availability to the all objects. This can contain a sequence of alternatives. Each alternative will start from case keyword. Each case statement includes a pattern and one or more expression which get evaluated if the specified pattern gets matched. To separate the pattern from the expressions, arrow symbol (=>) is used.





Collections in Scala (Class -31)

Collection in programming is a simple object used to collect data. It groups together elements into a single entity (object). You can do the operation is to add, delete, update data using collections. The Collection in Scala can be mutable as well as immutable. 

A Mutable collection is a collection whose elements can be updated and elements are added or removed from it. It allows all these operations.

An Immutable collection does not allow the user to do the update operation or add and remove operation on it. There is an option to do this, but on every operation, a new collection is created with updated value and the old one is discarded.

Scala collections have a rich hierarchy. The traversable trait is at the root of Scala hierarchy, all classes inherit some traits that are required for the general functioning of the collections.


Monday, April 11, 2022

Operators in Scala (Class -30)

 An Operator is a symbol that represents an operation that is to be performed in the program. 

      Operators tell the compiler to perform a specific operation; each operator is associated with one and has a unique identification code. Operators play an important role in programming and they are used to make an expression that executes to perform a task. 

Scala as a huge range of operators:

a)      a)   Arithmetic operators

b)     Relational operators

c)      Logical operators

d)     Bitwise operators

e)      Assignment operators


Data Types in Scala (Class -29)

 In a programming language, a data type is also known as type, tells the compiler about the type of data that is used by the programmer.

The Scala data types are taken as it is from Java and the storage and length are the same. There are many different types of data types in Scala.

1 Byte = 8 Bits.

Data Types

Values

Storage

Default Value

Usage

Boolean

True / False

2 Bytes

FALSE

Only 2 values.

Integer

minus 2147483648 to 2147483647

4 bytes

0

Commonly used in programming.

Float

IEEE 754 single-precision float.

4 Bytes

0.0F

For the decimal point numbers.

Double

IEEE 754 double-precision float.

8 Bytes

0.0D

To handle decimal point numbers that needs larger value and more precision.

Char

0 to 216-1 Unicode

2 Bytes

\u000'

To handle character assignments stored as unsigned Unicode characters.

String

any length

40 Bytes (Empty String)

Null

Used to store a character sequence in a program.