scala
=====
what is scala?
Scala is a general purpose programming language.
it runs on Java Virtual Machine (JVM). Scala file translates to Java bytecode
It’s a pure object-oriented programming (OOP) language. Every variable is an ob‐
ject, and every “operator” is a method.
We can use java classes or methods in scala
scala installation
==================
java JDK should be installed
scala
https://www.scala-lang.org/download/
Set SCALA_HOME and add in the PATH variable
scala REPL
==========
REPL - Read Evaluate Print Loop
The Scala REPL is a tool (scala) for evaluating expressions in Scala.
In interactive mode, the REPL reads expressions at the prompt,
wraps them in an executable template, and then compiles and executes the result.
examples on REPL
================
in Scala , scala defines the datatype of the variables based on the type of data
val - immutable (read only)
var - mutable (read and write)
Variable Type Inference
val x = 10
var y = 20
y = 30
Assigning two variables at a time
val x, y = 1
x + y
print(res3)
val a = Array(1, 2, 3)
a.sum
Scala datatypes
===============
Byte
Short
Int
Long
Float
Double
Char
String
Boolean
Unit
Null
Nothing
Any
AnyRef
intializing the variable with datatype
======================================
val a:Int = 10
val b : Double = 20.0
val c : Float = 20.0f
First program on scala shell
============================
HelloWorld.scala
object HelloWorld{
def main(args:Array[String]){
print("Hello Scala")
}
}
HelloWorld.main(Array("123"))
package imports in scala
========================
import java.util._
import org.apache.spark.sql.{DataFrame, SaveMode,}
import org.apache.spark.SparkEnv
classes and objects
===================
To create a launching point for your applications, you have two choices.
First, you can define an object which extends App
object ScalaFirstObject extends App {
println("Hello First object Scala")
}
// Or you can define an object that contains a main method
object ScalaSecondObject
{
def main(args: Array[String]): Unit = {
println("Hello Second object Scala")
}
}
Singleton objects
// create a singleton
object CashRegister {
def open { println("opened") }
def close { println("closed") }
}
// call the CashRegister methods just like static methods
object Main extends App {
CashRegister.open
CashRegister.close
}
classes
======
class Point(val xc: Int, val yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println ("Point x location : " + x);
println ("Point y location : " + y);
}
}
object Demo {
def main(args: Array[String]) {
val pt = new Point(10, 20);
// Move to a new location
pt.move(10, 10);
}
}
Static methods in Scala
========================
import java.util.Calendar
import java.text.SimpleDateFormat
object DateUtils {
// as "Wednesday, October 20"
def getCurrentDate:String = getCurrentDateTime("EEEE, MMMM d")
// as "6:20 p.m."
def getCurrentTime: String = getCurrentDateTime("K:m aa")
// a common function used by other date/time functions
private def getCurrentDateTime(dateTimeFormat: String): String = {
val dateFormat = new SimpleDateFormat(dateTimeFormat)
val cal = Calendar.getInstance()
dateFormat.format(cal.getTime())
}
}
Strings in scala
================
Everthing is treated as object
1. "Hello, scala".getClass.getName
Scala String is a Java String, so you can use all the normal Java string methods.
2. val s = "Hello, world"
3. s.length // to find the length of the string
4. val s = "Hello" + " world" // normally u should assign hello and world in seperate variables
5. "hello".foreach(println) // to iterate the characters of the string
6. for (c <- "hello") println(c) // You can treat a String as a sequence of characters in a for loop
7. s.getBytes.foreach(println) // You can also treat it as a sequence of bytes
8. val result = "hello world".filter(_ != 'l') // to filter 'l' character
9. "scala".drop(2).take(2).capitalize
//testing String equality
10. val s1 = "Hello" , val s2 = "Hello" , val s3 = "H" + "ello"
s1 == s2
s1 == s3
11. val s4: String = null
s3 == s4
s4 == s3
12. val s1 = "Hello" , val s2 = "hello"
s1.toUpperCase == s2.toUpperCase
13. val s1: String = null
val s2: String = null
s1.toUpperCase == s2.toUpperCase // throws Null pointer Exception
14. val a = "Marisa" , val b = "marisa"
a.equalsIgnoreCase(b)
// Creating Multiline Strings
15. val foo = """This is a
multiline String"""
16. val speech = """Four score and |
seven years ago""".stripMargin
17. val speech = """Four score and #
seven years ago""".stripMargin('#')
18. val speech = """Four score and
|seven years ago
|our fathers""".stripMargin.replaceAll("\n", " ") // To convert this multiline string into one continuous line
19. val s = """This is known as a
|"multiline" string
|or 'heredoc' syntax.""". stripMargin.replaceAll("\n", " ") // includes single quotes and convert into single line
//Splitting Strings
20. "hello world".split(" ") // result is Array[String]
21. "hello world".split(" ").foreach(println) // prints sequentially
22. val s = "eggs, milk, butter, Coco Puffs"
s.split(",")
s.split(",").map(_.trim)
23. "hello world, this is Al".split("\\s+")
24. "hello world".split(" ") // split with a String argument
25. "hello world".split(' ') // split with a Char argument
// Substituting Variables into Strings
26. val name = "Fred"
val age = 33
val weight = 200.00
println(s"$name is $age years old, and weighs $weight pounds.") // s is a method and also string interpolation
string interpolation functionality was not there Prior to version 2.10
println(s"Age next year: ${age + 1}") // include expressions while substituting
println(s"You are 33 years old: ${age == 33}")
//pending
27. case class Student(name: String, score: Int) // You’ll also need to use curly braces when printing object fields
val hannah = Student("Hannah", 95)
println(s"${hannah.name} has a score of ${hannah.score}")
println(s"$hannah.name has a score of $hannah.score") // without curly braces will not work
28.
println(f"$name is $age years old, and weighs $weight%.2f pounds.") // f string interpolator
println(f"$name is $age years old, and weighs $weight%.0f pounds.")
// Processing a String One Character at a Time
29. val upper = "hello, world".map(c => c.toUpper)
30. val upper = "hello, world".map(_.toUpper)
31. val upper = "hello, world".filter(_ != 'l').map(_.toUpper)
32. for (c <- "hello") println(c)
33. val upper = for (c <- "hello, world") yield c.toUpper
Adding yield to a for loop essentially places the result from each loop iteration into a temporary holding area.
When the loop completes, all of the elements in the holding area are returned as a single collection.
34. val result = for {
c <- "hello, world" if c != 'l'
} yield c.toUpper
35. hello".foreach(println) // Scala treats a string as a sequence of characters
36. "HELLO".map(c => (c.toByte+32).toChar)
37. "HELLO".map{ c =>
(c.toByte+32).toChar
}
38. def toLower(c: Char): Char = (c.toByte+32).toChar // write your own method that operates on a character
39. "HELLO".map(toLower) // use that method with map
40. val s = "HELLO" , for (c <- s) yield toLower(c)
// Finding Patterns in Strings
41. val numPattern = "[0-9]+".r
val address = "123 Main Street Suite 101"
val match1 = numPattern.findFirstIn(address)
val matches = numPattern.findAllIn(address)
matches.foreach(println)
val matches = numPattern.findAllIn(address).toArray
// Replacing Patterns in Strings
42. val address = "123 Main Street".replaceAll("[0-9]", "x")
val regex = "[0-9]".r
val newAddress = regex.replaceAllIn("123 Main Street", "x")
val result = "123".replaceFirst("[0-9]", "x")
val regex = "H".r
val result = regex.replaceFirstIn("Hello world", "J")
// Accessing a Character in a String
43 "hello".charAt(0) // you could use the Java charAt method
"hello"(0)
"hello"(1)
"hello".apply(1) // behind the scenes, Scala converts your code into this
// Add Your Own Methods to the String Class
44. implicit class StringImprovements(s: String)
{ def increment = s.map(c => (c + 1).toChar)
}
val result = "HAL".increment
// Put the implicit class in an object
45.
package com.alvinalexander.utils
object StringUtils {
implicit class StringImprovements(val s: String) {
def increment = s.map(c => (c + 1).toChar)
} }
46. package foo.bar
import com.alvinalexander.utils.StringUtils._
object Main extends App { println("HAL".increment)
}
47. implicit class StringImprovements(val s: String) //the return type of String is made explicit:
{
// being explicit that each method returns a String
def increment: String = s.map(c => (c + 1).toChar)
def decrement: String = s.map(c => (c − 1).toChar)
def hideAll: String = s.replaceAll(".", "*")
}
48. implicit class StringImprovements(val s: String) //class demonstrates several dif‐ ferent types of string conversion methods
{
def increment = s.map(c => (c + 1).toChar)
def decrement = s.map(c => (c − 1).toChar)
def hideAll: String = s.replaceAll(".", "*")
def plusOne = s.toInt + 1 def asBoolean = s match {
case "0" | "zero" | "" | " " => false
case _ => true
} }
49. "4".plusOne
"0".asBoolean
"1".asBoolean
=====
what is scala?
Scala is a general purpose programming language.
it runs on Java Virtual Machine (JVM). Scala file translates to Java bytecode
It’s a pure object-oriented programming (OOP) language. Every variable is an ob‐
ject, and every “operator” is a method.
We can use java classes or methods in scala
scala installation
==================
java JDK should be installed
scala
https://www.scala-lang.org/download/
Set SCALA_HOME and add in the PATH variable
scala REPL
==========
REPL - Read Evaluate Print Loop
The Scala REPL is a tool (scala) for evaluating expressions in Scala.
In interactive mode, the REPL reads expressions at the prompt,
wraps them in an executable template, and then compiles and executes the result.
examples on REPL
================
in Scala , scala defines the datatype of the variables based on the type of data
val - immutable (read only)
var - mutable (read and write)
Variable Type Inference
val x = 10
var y = 20
y = 30
Assigning two variables at a time
val x, y = 1
x + y
print(res3)
val a = Array(1, 2, 3)
a.sum
Scala datatypes
===============
Byte
Short
Int
Long
Float
Double
Char
String
Boolean
Unit
Null
Nothing
Any
AnyRef
intializing the variable with datatype
======================================
val a:Int = 10
val b : Double = 20.0
val c : Float = 20.0f
First program on scala shell
============================
HelloWorld.scala
object HelloWorld{
def main(args:Array[String]){
print("Hello Scala")
}
}
HelloWorld.main(Array("123"))
package imports in scala
========================
import java.util._
import org.apache.spark.sql.{DataFrame, SaveMode,}
import org.apache.spark.SparkEnv
classes and objects
===================
To create a launching point for your applications, you have two choices.
First, you can define an object which extends App
object ScalaFirstObject extends App {
println("Hello First object Scala")
}
// Or you can define an object that contains a main method
object ScalaSecondObject
{
def main(args: Array[String]): Unit = {
println("Hello Second object Scala")
}
}
Singleton objects
// create a singleton
object CashRegister {
def open { println("opened") }
def close { println("closed") }
}
// call the CashRegister methods just like static methods
object Main extends App {
CashRegister.open
CashRegister.close
}
classes
======
class Point(val xc: Int, val yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println ("Point x location : " + x);
println ("Point y location : " + y);
}
}
object Demo {
def main(args: Array[String]) {
val pt = new Point(10, 20);
// Move to a new location
pt.move(10, 10);
}
}
Static methods in Scala
========================
import java.util.Calendar
import java.text.SimpleDateFormat
object DateUtils {
// as "Wednesday, October 20"
def getCurrentDate:String = getCurrentDateTime("EEEE, MMMM d")
// as "6:20 p.m."
def getCurrentTime: String = getCurrentDateTime("K:m aa")
// a common function used by other date/time functions
private def getCurrentDateTime(dateTimeFormat: String): String = {
val dateFormat = new SimpleDateFormat(dateTimeFormat)
val cal = Calendar.getInstance()
dateFormat.format(cal.getTime())
}
}
Strings in scala
================
Everthing is treated as object
1. "Hello, scala".getClass.getName
Scala String is a Java String, so you can use all the normal Java string methods.
2. val s = "Hello, world"
3. s.length // to find the length of the string
4. val s = "Hello" + " world" // normally u should assign hello and world in seperate variables
5. "hello".foreach(println) // to iterate the characters of the string
6. for (c <- "hello") println(c) // You can treat a String as a sequence of characters in a for loop
7. s.getBytes.foreach(println) // You can also treat it as a sequence of bytes
8. val result = "hello world".filter(_ != 'l') // to filter 'l' character
9. "scala".drop(2).take(2).capitalize
//testing String equality
10. val s1 = "Hello" , val s2 = "Hello" , val s3 = "H" + "ello"
s1 == s2
s1 == s3
11. val s4: String = null
s3 == s4
s4 == s3
12. val s1 = "Hello" , val s2 = "hello"
s1.toUpperCase == s2.toUpperCase
13. val s1: String = null
val s2: String = null
s1.toUpperCase == s2.toUpperCase // throws Null pointer Exception
14. val a = "Marisa" , val b = "marisa"
a.equalsIgnoreCase(b)
// Creating Multiline Strings
15. val foo = """This is a
multiline String"""
16. val speech = """Four score and |
seven years ago""".stripMargin
17. val speech = """Four score and #
seven years ago""".stripMargin('#')
18. val speech = """Four score and
|seven years ago
|our fathers""".stripMargin.replaceAll("\n", " ") // To convert this multiline string into one continuous line
19. val s = """This is known as a
|"multiline" string
|or 'heredoc' syntax.""". stripMargin.replaceAll("\n", " ") // includes single quotes and convert into single line
//Splitting Strings
20. "hello world".split(" ") // result is Array[String]
21. "hello world".split(" ").foreach(println) // prints sequentially
22. val s = "eggs, milk, butter, Coco Puffs"
s.split(",")
s.split(",").map(_.trim)
23. "hello world, this is Al".split("\\s+")
24. "hello world".split(" ") // split with a String argument
25. "hello world".split(' ') // split with a Char argument
// Substituting Variables into Strings
26. val name = "Fred"
val age = 33
val weight = 200.00
println(s"$name is $age years old, and weighs $weight pounds.") // s is a method and also string interpolation
string interpolation functionality was not there Prior to version 2.10
println(s"Age next year: ${age + 1}") // include expressions while substituting
println(s"You are 33 years old: ${age == 33}")
//pending
27. case class Student(name: String, score: Int) // You’ll also need to use curly braces when printing object fields
val hannah = Student("Hannah", 95)
println(s"${hannah.name} has a score of ${hannah.score}")
println(s"$hannah.name has a score of $hannah.score") // without curly braces will not work
28.
println(f"$name is $age years old, and weighs $weight%.2f pounds.") // f string interpolator
println(f"$name is $age years old, and weighs $weight%.0f pounds.")
// Processing a String One Character at a Time
29. val upper = "hello, world".map(c => c.toUpper)
30. val upper = "hello, world".map(_.toUpper)
31. val upper = "hello, world".filter(_ != 'l').map(_.toUpper)
32. for (c <- "hello") println(c)
33. val upper = for (c <- "hello, world") yield c.toUpper
Adding yield to a for loop essentially places the result from each loop iteration into a temporary holding area.
When the loop completes, all of the elements in the holding area are returned as a single collection.
34. val result = for {
c <- "hello, world" if c != 'l'
} yield c.toUpper
35. hello".foreach(println) // Scala treats a string as a sequence of characters
36. "HELLO".map(c => (c.toByte+32).toChar)
37. "HELLO".map{ c =>
(c.toByte+32).toChar
}
38. def toLower(c: Char): Char = (c.toByte+32).toChar // write your own method that operates on a character
39. "HELLO".map(toLower) // use that method with map
40. val s = "HELLO" , for (c <- s) yield toLower(c)
// Finding Patterns in Strings
41. val numPattern = "[0-9]+".r
val address = "123 Main Street Suite 101"
val match1 = numPattern.findFirstIn(address)
val matches = numPattern.findAllIn(address)
matches.foreach(println)
val matches = numPattern.findAllIn(address).toArray
// Replacing Patterns in Strings
42. val address = "123 Main Street".replaceAll("[0-9]", "x")
val regex = "[0-9]".r
val newAddress = regex.replaceAllIn("123 Main Street", "x")
val result = "123".replaceFirst("[0-9]", "x")
val regex = "H".r
val result = regex.replaceFirstIn("Hello world", "J")
// Accessing a Character in a String
43 "hello".charAt(0) // you could use the Java charAt method
"hello"(0)
"hello"(1)
"hello".apply(1) // behind the scenes, Scala converts your code into this
// Add Your Own Methods to the String Class
44. implicit class StringImprovements(s: String)
{ def increment = s.map(c => (c + 1).toChar)
}
val result = "HAL".increment
// Put the implicit class in an object
45.
package com.alvinalexander.utils
object StringUtils {
implicit class StringImprovements(val s: String) {
def increment = s.map(c => (c + 1).toChar)
} }
46. package foo.bar
import com.alvinalexander.utils.StringUtils._
object Main extends App { println("HAL".increment)
}
47. implicit class StringImprovements(val s: String) //the return type of String is made explicit:
{
// being explicit that each method returns a String
def increment: String = s.map(c => (c + 1).toChar)
def decrement: String = s.map(c => (c − 1).toChar)
def hideAll: String = s.replaceAll(".", "*")
}
48. implicit class StringImprovements(val s: String) //class demonstrates several dif‐ ferent types of string conversion methods
{
def increment = s.map(c => (c + 1).toChar)
def decrement = s.map(c => (c − 1).toChar)
def hideAll: String = s.replaceAll(".", "*")
def plusOne = s.toInt + 1 def asBoolean = s match {
case "0" | "zero" | "" | " " => false
case _ => true
} }
49. "4".plusOne
"0".asBoolean
"1".asBoolean