SCALA : Knowing #TOP10 facts , Why Scala is so popular
Scala is pure object oriented
programming language and it had lots of improvement over Java semantics like
generics and type casting. It has clean up the issues like diamond relationship
or implicit objects to move more closure to functional programming language.
Is Scala is better than other
programming language?
· Lots of focus on using
generics like in Arrays and implementation of Any, AnyRef and AnyVal makes it
best use to unstructural data processing.
· Scala has immutable “val” as
a first class language feature. The “val” of scala is similar to Java final
variables. we have Var which can be mutable.
· Scala lets ‘if blocks’,
‘for-yield loops’, and ‘code’ in braces to return a value. It is more
preferable, and eliminates the need for a separate ternary operator.
· Singleton has singleton
objects rather than C++/Java/ C# classic static. It is a cleaner solution
· Persistent immutable
collections are the default and built into the standard library.
· It has native currying, case
classes & tuples and a concise code
· It has no boiler plate code
and very less syntax binding.
Difference between an object
and a class
A class is a definition for a
description. It defines a type in terms of methods and composition of other
types. A class is a blueprint of the object. While, an object is a singleton,
an instance of a class which is unique. An anonymous class is created for every
object in the code, it inherits from whatever classes you declared object to
implement.
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.
Case Classes
Case
classes provides a recursive decomposition mechanism via pattern matching, it
is a regular classes which export their constructor parameter. The constructor
parameters of case classes can be accessed directly and are treated as public
values.
Function currying
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.
//CURRYING
def
amountCheck3 = (amount: Double) (type :String) = if (type =='cr')(amount <
10) true else false
var
isAlllowed = amountCheck3(5000)_ // it will return function with partial
implementation like it has implemented with default value 5000
isAlllowed("CR")
Implicit parameters
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.
Closure in Scala?
A
closure is a function whose return value depends on the value of the variables
declared outside the function.
//CLOSURE
var amountCheck = (amount: Double) => if
(amount < minAmount) true else false
var minAmount = 10;
Comments
Post a Comment