Scala is statically typed:
-> Statically typed language binds the type to a variable for its entire scope.
-> Dynamically typed languages bind the type to the actual value referenced by a variable.
Mixed paradigm - OOP
Functional Programming :
-> Pure functional languages don't allow any mutable state, thereby avoiding the need for synchronization on shared access to mutable state.
Python is interepreted language and scala is compiled language
Scala and Java are very similar in most of cases.
Same byte code can use in Java, similar to java can compiles one run any where with jvm
Installation of Scala
1) click - https://www.scala-lang.org/
2) First, make sure you have the Java 8 JDK installed.
3) Download the Scala binaries for windows
Verifying installation
REPL ( Read Evaluate Print Loop)
--> Whatever type here, will read by scala and will be printing here
eg:
scala> 'a'
res3: Char = a
scala>"bigdata"
res4: String = bigdata
scala> :quit ( to exit from scala prompt
About variables: examples
immutable variable : val ( Once we define we can't modify value)
mutable variable : var
Scala is a statically typed, dynamically inferred programming language.
example for statically typed :
scala>val x= "a2zmyworld"
x: String = a2zmyworld
scala x = "Devendra"
<console>:12: error: reassignment to val
x = "Devendra"
Error
Dynamically inferredd:
scala> var y = "a2zmyworld"
y: String = raghu
scala> y = 123
<console> :123:
When defined ( originally variable is string , and can't assign integer value)
String Manipulation examples :
scala> y.toLo
toLong toLowerCase
scala>y.toLowerCase
res9: String = devendra123
scala>y.toUpperCase
res10: String = devendra123
scala>y.length
res11: Int = 10
scala> y = "Hello World"
y: String = Hello Worldd
Block Expression :
scala> var add = {var a=10; var b=20; a+b}
add: Int = 30
scala> var add = {var a = 10; var b= 20; a+b; a-b; b/a}
add: Int = 2
Scala documentation
Very basic for beginers - very useful of this tour
https://docs.scala-lang.org/tour/tour-of-scala.html
A scala tutorial for Java Programmers
https://docs.scala-lang.org/tutorials/scala-for-java-programmers.html
About "Laziness " in Scala
----------------------------------
Goto paste mode
scala> :paste
//Entering paste mode ( ctrl-D to finish)
lazy val x = {println("foo");10} ( Here x will not be initialize)
println("bar")
println(x)
only one deference with lazy x
in previious example
val x = {println("foo"); 10}
here it was output
bar
foo
10
Since x was defined as lazy, the output will differ as compared in the screen.
One realtime scenario where laziness is much useful :
Let us say we have a Spark cluster.
A = load 'abc.json';
abc.json is a 1 TB file. For example, when you have entered this command, and for some reason you went for break, at this time, it will be loaded entire 1 TB file.
In this scenario, we can define as
lazy A = load 'abc.json';
count A
Another scenario :
scala command to read a file :
scala> val file = scala.io.source.fromFile("devendra.txt").mkString
scala> lazy val file = scala.io.source.fromFile("devendra.txt").mkString
file: String = <lazy>
About Singleton Object in Scala :
Scala has the idea of "Singleton Object".
This is equivalane to static classes in Java.
object if_else{
def main(arg: Array[String]){
var fruit = "apple"
val color = if(fruit == "apple") println("red")
else println("no color")
}
}
Example - 2
object while_loop{
def main(arg: Array[String]){
var i = 10
while(i > 0) {
println ( "Hello: "+i)
i = i -1
}
}
}
Example for For loop :
object for_loop{
def main(arg: Array[String]){
for(i <- 1 to 3; j < - 1 to 3)
println(i,j)
}
}
Example for Array :
object Magic{
def main(arg: Array[String]){
var numguess = 0
do {
println("Enter number :")
numguess = readline.toInt
}while(numguess ! = 10)
println("You got the number!!!")
}
}
No comments:
Post a Comment