Spark: Apache Spark is a fast, in-memory data
processing engine with elegant and expressive development APIs in Scala, Java,
Python, and R that allow data workers to efficiently execute machine learning
algorithms that require fast iterative access to datasets. Spark on Apache Hadoop
YARN enables deep integration
with Hadoop and other YARN enabled workloads in the enterprise.
Scala: Scala is a
general-purpose programming language. It supports object oriented, functional
and imperative programming approaches. It is a strong static type language. In
scala, everything is an object whether it is a function or a number. It does
not have concept of primitive data.
It was
designed by Martin Odersky. It was officially released for java platform in
early 2004 and for .Net framework in June 2004. Later on, Scala dropped .Net
support in 2012.
Scala
is influenced by Java, Haskell, Lisp, Pizza etc. and influenced to F#, Fantom,
Red etc.
File
extension of scala source file may be either .scala or .sc.
You can
create any kind of application like web application, enterprise application,
mobile application, desktop based application etc
Scala is object-oriented
Scala is a pure object-oriented language in the
sense that every value is an object. Types and behavior of objects are
described by classes and traits which will be explained in subsequent chapters.
Classes are extended by subclassing and a
flexible mixin-based composition mechanism as a clean replacement for multiple
inheritance.
Scala is functional
Scala is also a functional language in the sense
that every function is a value and every value is an object so ultimately every
function is an object.
Scala provides a lightweight syntax for defining
anonymous functions, it supports higher-order functions, it allows functions to
be nested, and supports currying. These concepts will be explained in
subsequent chapters.
Scala is statically typed
Scala, unlike some of the
other statically typed languages (C, Pascal, Rust, etc.), does not expect you
to provide redundant type information. You don't have to specify a type in most
cases, and you certainly don't have to repeat it.
Scala runs on the JVM
Scala is compiled into Java Byte Code which is
executed by the Java Virtual Machine (JVM). This means that Scala and Java have
a common runtime platform. You can easily move from Java to Scala.
The Scala compiler compiles your Scala code into
Java Byte Code, which can then be executed by the 'scala' command. The 'scala'
command is similar to the java command, in that it executes your compiled Scala
code.
Scala can Execute Java Code
Scala enables you to use
all the classes of the Java SDK and also your own custom Java classes, or your
favorite Java open source projects.
Scala can do Concurrent & Synchronize
processing
Scala allows you to
express general programming patterns in an effective way. It reduces the number
of lines and helps the programmer to code in a type-safe way. It allows you to
write codes in an immutable manner, which makes it easy to apply concurrency
and parallelism (Synchronize).
Popularity of Scala:
- Twitter have announced that it had
switched large portions of its backend from Ruby to Scala and intended to
convert the rest.
- Apple Inc. uses Scala in certain
teams, along with Java and the Play framework.
- The New York Times revealed in
2014 that its internal content management system Blackbeard is built using
Scala, Akka and Play Framework.
- There are teams within Google that
use Scala, mostly due to acquisitions such as Firebase and Nest.
- The Walmart Canada Uses Scala for
their back end platform.
Identifiers, Literals, Values, Variables, and
Types:
• A literal
(or literal data) is data that appears directly in the source code,
like the
number 5, the character A, and the text “Hello, World.”
• A value is an immutable, typed storage unit. A value can be assigned data when it
is defined, but can never be reassigned.
number 5, the character A, and the text “Hello, World.”
• A value is an immutable, typed storage unit. A value can be assigned data when it
is defined, but can never be reassigned.
Value is a name which is used to refer memory
location. You can create unmutable value in scala.
val <identifier>[:
<type>] = <data>
• A variable is a mutable, typed storage unit. A variable can be assigned data when it
is defined and can also be reassigned data at any time.
• A variable is a mutable, typed storage unit. A variable can be assigned data when it
is defined and can also be reassigned data at any time.
Variable is a name which
is used to refer memory location. You can create mutable variable in scala.
var
<identifier>[: <type>] = <data>
Identfier Rules
-------------------------
1.A letter followed by zero or more letters and digits.
2.A letter followed by zero or more letters and digits, then an
underscore (_), and then one or more of either letters and digits or operator
characters.
3. One or more operator characters.
4. One or more of any character except a backquote, all enclosed
in a pair of backquotes.
Names enclosed in backquotes
can, unlike the other names
• A type
is the kind of data you are working with, a definition or
classification of data.
All data in Scala corresponds to a specific type, and all Scala types are defined as
classes with methods that operate on the data
All data in Scala corresponds to a specific type, and all Scala types are defined as
classes with methods that operate on the data
Byte, Short, Int, Long, Float, Double,Boolean,Char,String
Int(10,0x10) Float(4.0f), Double(5.0,5.0d), Long(5l)
Default Values:
Boolean-False Char- ‘\u0000’
Byte-0 Short-0
Int-0 Long-0L Float-0.0F Double-0.0D String-Null
Type inference:
The Scala compiler will then discern the
type of the value from its assignment, a process known as type inference.
Type operations:
isValid[Type]
maxValue
Conversion:
Implicit
Explicit
Operators: An operator is a symbol that tells the compiler
to perform specific mathematical or logical manipulations. Scala is rich in
built-in operators and provides the following types of operators
Airthmatic Operators
Bitwise
logical
Releational
Assignment
terinary
Control Statements:
if
if else
if else if ladder
match
while
(Ctrl+Z—Infinite loop)
do while
for loop
(filtering) for(i<-Range if
i<=5) for(I<-Range) yield i
break (import scala.util.control.Breaks._ )
use breakable to avoid Exception
Array : Array is a collection of mutable values. It is an index based
data structure which starts from 0 index to n-1 where n is length of array.
Scala arrays can be generic. It
means, you can have an Array[T], where T is a type parameter or abstract type.
Scala arrays are compatible with Scala sequences - you can pass an Array[T]
where a Seq[T] is required. It also supports all the sequence operations
val a=Array(1,2,3)
val a=new Array[Int](5)
val aa=Array(Array(1,2),Array(3,4))
val aa=Array.ofDim[Int](2,2)
val data=Array(10,20,30,40)
Accessing
Array: data(0),data(1)
Iterator : data.foreach(println)
Tuple: A tuple is a collection of elements in ordered form. If there is no
element present, it is called empty tuple. You can use tuple to store any type
of data. You can store similar type or mix type data also. You can use it to
return multiple values from a function.
val data= (10,20,30,40,”gnr”,5.0f)
Accessing
Tuple: data._1,data._2
Iterator : data.productIterator.foreach(println)
Key Value Pair Tuple : ’age’->’20’
String:
In scala, string is a combination of characters or we can say it
is a sequence of characters. It is index based data structure and use linear
approach to store data into memory. String is immutable in scala like java. You
can manipulate string and can apply method to get desire result but you can't
change original string object.
Var s=”Hello”
val mls = """Four score
and
#seven years ago""".stripMargin('#')
#seven years ago""".stripMargin('#')
s.split(“,”)
s.drop(2)
s.take(2)
s.toByte
s.getBytes
s.toUpperCase
s.toLowerCase
s.capitalize
S+”How are you”
Var s1=s+”How are you”
==
.equals()
compareTo()
+ or .concat() (Concatination)
.substring(s,e)
String
Interpolation: Starting in Scala 2.10.0, Scala offers a new
mechanism to create strings from your data. It is called string interpolation.
String interpolation allows users to embed variable references directly in
processed string literals. Scala provides three string interpolation methods:
s, f and raw.
println("value of pi = "+pi)
println(s"value of pi = $pi") (String Interpolation)
var s1 = "Scala \tstring \nexample"
var s2 = raw"Scala \tstring \nexample"
Scala File Handling: Scala provides predefined methods to deal with file. You can
create, open, write and read file. Scala provides a complete package scala.io for file handling.
Creating File : Scala doesn't provide file writing methods.
So, you have to use the Java PrintWriter or FileWriter methods.
Ex:
Import
java.io._
Val
f=new File(“gnr.txt”)
Val
p=new PrintWriter(f)
p.write(“hai
this sample”)
p.close();
Reading Each Line from
File:
Ex:
import
scala.io.source
import
scala.io._
val
data=source.fromFile(“gnr.txt”)
for(line<-data.getLines){
println(line)
}
data.close()
Reading Each Character
from File:
Ex:
import
scala.io.source
import
scala.io._
val
data=source.fromFile(“gnr.txt”)
while(data.hasNext){
println(data.next)
}
data.close()
Collections:
List:
Arithmetic Operators on Lists:
Mapping
Operation:
Math reduction operations:
Boolean reduction operations
Generic list reduction
operations
Java and Scala collection conversions
[cloudera@quickstart ~]$ spark-shell
scala> "hai"
res1: String = hai
scala> val s=res1
s: String = hai
scala> res1
res2: String = hai
scala> 1
res3: Int = 1
scala> val a=res3
a: Int = 1
scala> "hai"
res4: String = hai
scala> "hai".foreach(println)
h
a
i
scala> // A valus is an Immutable ,typed Storage unit. A
value can be assigned data when it is
defined, but can never be reassigned
scala> val a=10
a: Int = 10
scala> a=20
<console>:27: error: reassignment to val
a=20
^
scala> //A Variable is mutable,typed Storage unit. A
value can be assigned data when it is defined and can also reassigned data at
any time
scala> var a:Int
<console>:19: error: class $iwC needs to be abstract,
since variable a is not defined
(Note that variables need to be initialized to be defined)
class $iwC extends Serializable {
scala>//Var with default values(_)
scala> var a:Int=_
a: Int = 0
scala> a=20
a: Int = 20
------------------------
Type Inference
--------------------------
scala> val a=10 ///Compiler Automatically disern the type
of the value from its assignment
a: Int = 10
scala> val b="stringtype"
b: String = stringtype
scala> //type operation
scala> val a=10
a: Int = 10
scala> a.getClass
//Returns the type (i.e, the class) of a value
res6: Class[Int] = int
scala> val b="gnr"
b: String = gnr
scala> a.hashCode //Returns the hash code of the value
for hash based collections
res8: Int = 10
scala> b.hashCode //Returns the hash code of the value
for hash based collections
res9: Int = 102507
scala> //to<Type>
//to convert a value to compatable value
(Forcefully-string("10")-int)
scala> val a=10
a: Int = 10
scala> val b=a
b: Int = 10
scala> val c=a.toByte
c: Byte = 10
scala> val c=3200.toByte
c: Byte = -128
scala> val c=127.toByte
c: Byte = 127
scala> val c=129.toByte
c: Byte = -127
scala> 129.toDouble
res23: Double = 129.0
scala> 129.toFloat
res24: Float = 129.0
scala> 12.05.toInt
res25: Int = 12
scala> 12.05.toFloat
res26: Float = 12.05
scala> //asInstanceOf[Type] //coverts value to a value of desired
type ,(not compatable with new type
gives error (string to int) )
scala> 120
res27: Int = 120
scala> 120.asInstanceOf[Byte]
res28: Byte = 120
scala> 130.asInstanceOf[Byte]
res31: Byte = -126
scala> "10".toInt
res32: Int = 10
scala> "10".asInstanceOf[Int]
java.lang.ClassCastException: java.lang.String cannot be
cast to java.lang.Integer
scala> //isInstanceOf[Type] // Returns true if the given
value has the given type
scala> 10.isInstanceOf[Byte]
res38: Boolean = false
scala> 128.isInstanceOf[Byte]
res39: Boolean = false
scala> 127.isInstanceOf[Byte]
res40: Boolean = false
----------------------------------------------------------------------------------
Conversions (Implicit(lower to Higher) and Explicit(highr to
lower) using to<Type>)
------------------------------------------------------------------------------------
scala> val a=10
a: Int = 10
scala> val b:Long=a //assign lower type to higher
b: Long = 10
scala> val c:Byte=a
<console>:27: error: type mismatch;
found : Int
required: Byte
val c:Byte=a
^
scala> val c:Byte=a.toByte //assign higher to lower
c: Byte = 10
----------------------
Operators:--
----------------------
Operators(Am,Bitwise,Logical,Relational,Assignment,Terinary)
Operator is a symbol that tells the compiler to perform mathamatical
or logical operation
scala> val a=10+20
a: Int = 30
scala> val m=10*20
m: Int = 200
scala> val d=20/10
d: Int = 2
scala> val d=20/3
d: Int = 6
scala> val d:Float=20/3
d: Float = 6.0
scala> val d=20/3.0
d: Double = 6.666666666666667
-----------------------
Control Statements:--
-----------------------
if
if else
if else if ladder
match
while(we dont no no of iteration)
doo while (at least one time stmt)
for
scala> if(10>20)
|
println("true")
scala> val a=if(10>20)
| 20
a: AnyVal = ()
scala> val a=if(100>20) 20 else 30
a: Int = 20
scala> val a=if(10>20) 20 else 30
a: Int = 30
scala> val a=if(10>20) 20 else 30
cala> val a=10
a: Int = 10
scala> val s=a
match{
| case
10=>"ten"
| case
20=>"twenty"
| case _=>"not
found"
| }
s: String = ten
scala> val a=100
a: Int = 100
scala> val s=a
match{
| case
10=>"ten"
| case
20=>"twenty"
| case
_=>"not found"
| }
s: String = not found
scala> var a=5
a: Int = 5
scala> var i=1
i: Int = 1
scala> while(a>i){
| print(i+"
")
| i+=1
| }
1 2 3 4
scala> do{
| print(i+" ")
| i+=1
| }while(5>i)
1 2 3 4
scala> for(i<-1 to 10){
| print(i+"
")
| }
1 2 3 4 5 6 7 8 9 10
scala> for(i<-1 until 10){
| print(i+"
")
| }
1 2 3 4 5 6 7 8 9
scala> for(i<-1 to 10 by 2){
| print(i+"
")
| }
1 3 5 7 9
scala> for(i<-1 to 20 if(i%2==0)){
| print(i+"
")
| }
2 4 6 8 10 12 14 16 18 20
scala> for(i<-1 to 20 if(i%2==0)) yield i
res7: scala.collection.immutable.IndexedSeq[Int] = Vector(2,
4, 6, 8, 10, 12, 14, 16, 18, 20)
scala> for(i<-1 to 20 if(i%2==0)) yield i
res7: scala.collection.immutable.IndexedSeq[Int] = Vector(2,
4, 6, 8, 10, 12, 14, 16, 18, 20)
scala> val a=1 to 10
a: scala.collection.immutable.Range.Inclusive = Range(1, 2,
3, 4, 5, 6, 7, 8, 9, 10)
scala> val a=(1 to 10).toList
a: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> var i=0
i: Int = 0
scala> while(5>i){
| if(i==3)
| break
| print(i+"
")
| }
Scala>import scala.util.control.Breaks._
scala> while(5>i){
| if(i==3)
| break
| print(i+"
")
| i+=1
| }
0 1 2 scala.util.control.BreakControl
scala> val i=5
i: Int = 5
scala> for(a<-1 to 100){
| print(a+"
")
| if(a%i==0)
| println()
| }
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40
41 42 43 44 45
46 47 48 49 50
51 52 53 54 55
56 57 58 59 60
61 62 63 64 65
66 67 68 69 70
71 72 73 74 75
76 77 78 79 80
81 82 83 84 85
86 87 88 89 90
91 92 93 94 95
96 97 98 99 100
----------------
String:--
-----------------
String is Combination or Sequence of Characters. It is
indexed data structure and use linear approach to store data into memory.
String is immutable in scala like java,you can manipulate string and apply method to get
desire result but you cant change original string object
Methods
-----------------
scala> val g="gnr hyd spark"
g: String = gnr hyd spark
scala> val s=g.split(",")
s: Array[String] = Array(gnr hyd spark)
scala> s(0)
res0: String = gnr hyd spark
scala> val g="nageswararao"
g: String = nageswararao
scala> g.drop(2)
res1: String = geswararao
scala> g.dropRight(2)
res2: String = nageswarar
scala> g.take(2)
res3: String = na
scala> g.takeRight(2)
res4: String = ao
scala> g.getBytes
res8: Array[Byte] = Array(110, 97, 103, 101, 115, 119, 97,
114, 97, 114, 97, 111)
scala> g.toUpperCase
res9: String = NAGESWARARAO
scala> g.capitalize
res10: String = Nageswararao
scala> g.toLowerCase
res11: String = nageswararao
scala> g+" Hyd"
res15: String = nageswararao Hyd
scala> val s=g+" Hyd"
s: String = nageswararao Hyd
scala> g.substring(0,3)
res23: String = nag
scala> g.substring(1,3)
res24: String = ag
scala> g.substring(1)
res25: String = ageswararao
scala> val a="ramu"
a: String = ramu
scala> val b="ramu"
b: String = ramu
scala> val
c="Ramu"
c: String = Ramu
scala> a.equals(b)
res29: Boolean = true
scala> a.equals(c)
res30: Boolean = false
scala> a.equalsIgnoreCase(c)
res31: Boolean = true
scala> val comb=a.concat(b)
comb: String = ramuramu
scala> val comb1=a+b
comb1: String = ramuramu
---------------------------
String Interpolation:--
---------------------------
Starting in scala 2.10.0, Scala offers a new mechanisam to
create string from your data.It is called striing interpolation.
string interpolation allows users to create embeded variable
references directly in processiong string literals.
scala provides 3 string interpolation methods(s,fand raw)
Examples
-------
scala> val a=10
a: Int = 10
scala> println("Given valus is :"+a)
Given valus is :10
scala> println(s"Given Value is :$a")//String
Interpolation
Given Value is :10
scala> val s="Nageswararao Hyderabad"
s: String = Nageswararao Hyderabad
scala> val s1="Nageswararao\nHyderabad"
s1: String =
Nageswararao
Hyderabad
scala> val s1=raw"Nageswararao\nHyderabad"
s1: String = Nageswararao\nHyderabad
-------------------
Array:--
-------------------
Array is a collection
of mutable values.It is index based data structure which starts from 0 index to
n-1 where n is length of array.
Scala arrays can be generic.
scala> val a=Array(10,20,30,40,"gnr")
a: Array[Any] = Array(10, 20, 30, 40, gnr)
scala> val a1=Array[Int](10,20,30,"gnr")
<console>:25: error: type mismatch;
found : String("gnr")
required: Int
val
a1=Array[Int](10,20,30,"gnr")
scala> val a1=Array[Int](10,20,30)
a1: Array[Int] = Array(10, 20, 30)
^
Accessing Elements:
-------------------
scala> a(0)
res55: Int = 10
scala> a(1)
res56: Int = 20
scala> a(3)
java.lang.ArrayIndexOutOfBoundsException: 3
Iterator:
---------
scala> a.foreach(println)
10
20
30
--------------------
Tuple:--
--------------------
A tuple is collection of elements in ordered form.If there
is no element present,is is called empty tuple.
You can use tuple to store any type of data.
you can store similar type,or mix type data also.
scala> val t=(10,20,"gnr",30)
t: (Int, Int, String, Int) = (10,20,gnr,30)
Accessing Elements:
-------------------
scala> t._1
res68: Int = 10
Iterator:
---------
scala> t.productIterator.foreach(println)
10
20
gnr
30
Accessing Elements:
-------------------
scala> t._1
res68: Int = 10
scala> t._2
res69: Int = 20
-------------------------------
Collections:--
--------------------------------
A collection framework provides data structures for
collecting one or more valus of a given type such as Arrays,List,Set,Maps and
Trees.
Most pf th prograing languages have their own collections
frameworks. Because the data structures are the buildin blocks of modern
software prjects.
Scala has a higher-performence ,object-oriented , and type-parameterized collections framework.
Scala collection also have
higher-order operatioons like map,flatMap,filter and reduce.
It also has separate mutable versus immutable collection
type heiarchies,which makes switching between immutable data(for stablity)
and mutable data (when necessary)
convenient.
The root of all iterable collecctions, Iterable, provides a
common set of methods for iterating and maniulating collection data.
List
-------
List is an Immutabe singly linked list,you can create a list
by invoking it as a function,passing in its contents in the form of
comma-seperated parameters.
scala> val
l=List("spark","hadoop","oracle","java")
l: List[String] = List(spark, hadoop, oracle, java)
Set:
-------------
A Set is an immutable
and unordered collection of unique elements,but works similarly to List.
scala> val s=Set(10,20,30,10,20,50)
s: scala.collection.immutable.Set[Int] = Set(10, 20, 30, 50)
scala> s.toList
res118: List[Int] = List(10, 20, 30, 50)
Map
--------
A Map is an immutable key-value store ,also known as a
hashmap,dictinary,or associative arrayin other languages.
Values stored in a Map with a given unique keymay be
retrived using that key.
The key and values are type parameterized.
like(key->value)
scala> val
m=Map("a"->1,"b"->2,"c"->3)
m: scala.collection.immutable.Map[String,Int] = Map(a ->
1, b -> 2, c -> 3)
scala> m("a")
res119: Int = 1
scala> m("c")
res120: Int = 3
scala> m("d")
java.util.NoSuchElementException: key not found: d
LIst Examples
-------------------
scala> val l=List(10,20,30,40,50)
l: List[Int] = List(10, 20, 30, 40, 50)
scala> l.size //Returns size of the List
res126: Int = 5
scala> l.length //Returns size of the List
res127: Int = 5
scala> l.head //Returns First element
res128: Int = 10
scala> l.tail //Returns All elements except First
res129: List[Int] = List(20, 30, 40, 50)
scala> l.init //Returns All elements Except Last
res131: List[Int] = List(10, 20, 30, 40)
scala> l:+10 // appendes 10 to the List l
res134: List[Int] = List(10, 20, 30, 40, 50, 10)
scala> 10 +:l // prepends 10 to the List l
res135: List[Int] = List(10, 10, 20, 30, 40, 50)
scala> val l1=1 :: 2 :: 3 :: Nil //Appends individual elements to this list
,A right associative operator.
l1: List[Int] = List(1, 2, 3)
scala> l1:+10
res136: List[Int] = List(1, 2, 3, 10)
scala> List(1,2):::List(3,4) //::: Appends the same
res144: List[Int] = List(1, 2, 3, 4)
scala> List(1,2)++List(3,4) //++ Append any other
collection
res145: List[Int] = List(1, 2, 3, 4)
scala> List(1,2):::Set(3,4)
<console>:37: error: value ::: is not a member of
scala.collection.immutable.Set[Int]
List(1,2):::Set(3,4)
^
scala> List(1,2)++Set(3,4)
res147: List[Int] = List(1, 2, 3, 4)
scala> l
res153: (String, String) = (l1,l2)
scala> l1
res154: List[Int] = List(1, 2, 3)
scala> l==l1 //==
returns true both collection are same else flase
<console>:41: warning: comparing values of types
(String, String) and List[Int] using `==' will always yield false
l==l1
^
res155: Boolean = false
scala> val l=List(10,20,30,10,40,50)
l: List[Int] = List(10, 20, 30, 10, 40, 50)
scala> l.distinct // Returns a version of the list without duplicat
elements
res161: List[Int] = List(10, 20, 30, 40, 50)
scala> l.filter(_>=30) // Returns elemts from the list
that pass a true/flase function
res162: List[Int] = List(30, 40, 50)
scala> l filter(_>=30) // Returns elemts from the list
that pass a true/flase function
res163: List[Int] = List(30, 40, 50)
scala> l.filterNot(_>=30)
res251: List[Int] = List(10, 20, 10)
scala> l.reverse //Reverse the list
res164: List[Int] = List(50, 40, 10, 30, 20, 10)
scala> List(List(1,2),List(3,4)).flatMap(x=>x)
//Convert a list of lists into a single list
res172: List[Int] = List(1, 2, 3, 4)
scala> List(List(1,2),List(3,4)).flatten //Convert a list of lists into a single list
res173: List[Int] = List(1, 2, 3, 4)
scala> List(List(1,2),List(3,4)) flatten
warning: there were 1 feature warning(s); re-run with
-feature for details
res174: List[Int] = List(1, 2, 3, 4)
scala> l
res176: List[Int] = List(10, 20, 30, 10, 40, 50)
scala> l.partition(_>=30) //Groups elements into a tuple of two
lists based on the result of a
true/false function
res177: (List[Int], List[Int]) = (List(30, 40, 50),List(10,
20, 10))
scala> l partition(_>=30)
res178: (List[Int], List[Int]) = (List(30, 40, 50),List(10,
20, 10))
scala> l.slice(1,4)
//Returns a segment of the list
from 1 index up to 4-1(3) rd
index
res179: List[Int] = List(20, 30, 10)
scala> l.splitAt(2)
//Groups of elements into a tuple of two lists based on if they fall
before or after the given index
res180: (List[Int], List[Int]) = (List(10, 20),List(30, 10,
40, 50))
scala> l.zip(List(1,2,3,4,5)) //Combines two lists into
a list of tuples of elements at each
Index
res181: List[(Int, Int)] = List((10,1), (20,2), (30,3),
(10,4), (40,5))
scala> l.zipWithIndex
//Combines two lists into a list
of tuples of elements with their index number
res182: List[(Int, Int)] = List((10,0), (20,1), (30,2),
(10,3), (40,4), (50,5))
scala> l.sorted
//Orders a list
res183: List[Int] = List(10, 10, 20, 30, 40, 50)
Math reduction Operation
-----------------------------
scala> l
res195: List[Int] = List(10, 20, 30, 10, 40, 50)
scala> l.sum
res196: Int = 160
scala> l.max
res197: Int = 50
scala> l.min
res198: Int = 10
scala> l.product
res201: Int = 120000000
Operations to convert collections
-------------------------------------------
scala> l
res206: List[Int] = List(10, 20, 30, 10, 40, 50)
scala> l.mkString(",") //Returns a collection to a set using given
delimeters
res207: String = 10,20,30,10,40,50
scala>
l.mkString("{",",","}")
res208: String = {10,20,30,10,40,50}
scala> val l1=l.toBuffer //Converts immutable to mutale
One
l1: scala.collection.mutable.Buffer[Int] = ArrayBuffer(10,
20, 30, 10, 40, 50)
scala> m
res209: scala.collection.immutable.Map[String,Int] = Map(a
-> 1, b -> 2, c -> 3)
res210: List[(String, Int)] = List((a,1), (b,2), (c,3))
scala> l.toSet
//converts any collection to set
res211: scala.collection.immutable.Set[Int] = Set(10, 20,
50, 40, 30)
scala> l.toString //renders a collection to a string
,including Collection types
res212: String = List(10, 20, 30, 10, 40, 50)
Booelan reducion Operators
--------------------------------------------
scala> l
res214: List[Int] = List(10, 20, 30, 10, 40, 50)
scala> l.contains(10)
//Checks if the list contains given element
res215: Boolean = true
scala> l.exists(_>20)
//Checks if a predicate
holds true at least one element
res216: Boolean = true
scala> l.forall(_>20) //Checks if a predicate holdstrue
for every element in the list
res218: Boolean = false
scala> l.startsWith(List(10,20))
res220: Boolean = true
scala> l.endsWith(List(10,20))
res221: Boolean = false
scala> l.isEmpty
res222: Boolean = false
scala> l.nonEmpty
res226: Boolean = true
Based On Index Numbers
----------------------------
scala> l.indexOf(10) //return index number of given
element, not there return -1
res230: Int = 0
scala> l.indexOf(100) //return index number of given
element, not there return -1
res231: Int = -1
scala> l.lastIndexOf(10) //return last index number of given element, not there
return -1
res232: Int = 3
Other
----------
240: List[Int] = List(10, 20, 30, 10, 40, 50)
scala> l.drop(2)// act as dropLeft
res241: List[Int] = List(30, 10, 40, 50)
scala> l.dropRight(2)
res242: List[Int] = List(10, 20, 30, 10)
scala> l.take(2)
//actas takeLeft
res244: List[Int] = List(10, 20)
scala> l.takeRight(2)
res245: List[Int] = List(40, 50)
While(First element soud match then returns true values)
--------
scala> l
res262: List[Int] = List(10, 20, 30, 10, 40, 50)
scala> l.takeWhile(_>=10)
res263: List[Int] = List(10, 20, 30, 10, 40, 50)
scala> l.takeWhile(_>=20)
res264: List[Int] = List()
scala> l.dropWhile(_>=10)
res265: List[Int] = List()
scala> l.dropWhile(_<=10)
res266: List[Int] = List(20, 30, 10, 40, 50)
scala> l.dropWhile(_>=20)
res267: List[Int] = List(10, 20, 30, 10, 40, 50)
scala> l.indexWhere(_<=10)
res269: Int = 0
scala> l.indexWhere(_<=20)
res270: Int = 0
scala> l.lastIndexWhere(_<=20)
res271: Int = 3
scala> l.indexWhere(_>=10)
res272: Int = 0
scala> l.indexWhere(_>=20)
res273: Int = 1
fold & reduce & scan
-------------------------------------
res281: List[Int] = List(10, 20, 30, 10, 40, 50)
scala> l.reduce(_+_) // Reduces the list given a
reduction function,starting with the first element in the list
res282: Int = 160
scala> l.reduceLeft(_+_) // Reduces the from left to
right given a reduction function,starting with the first element in the list
res283: Int = 160
scala> l.reduceRight(_+_) //Reduces the from right to
left given a reduction function,starting with the first element in the list
res284: Int = 160
scala> l.fold(0)(_+_) // Reduces the list given a starting
value and a reducetion function
res285: Int = 160
scala> l.foldLeft(0)(_+_) // Reduces the list from left to right given a starting value and a
reducetion function
res286: Int = 160
scala> l.foldRight(0)(_+_) // Reduces the list from right to left given a starting value and a
reducetion function
res288: Int = 160
scala> l.scan(0)(_+_) //takes a starting valus and a
reduction function and returns list of each accumlated value
res289: List[Int] = List(0, 10, 30, 60, 70, 110, 160)
scala> l.scanLeft(0)(_+_) //takes a starting valus and a
reduction function and returnslist of each accumlated value from left to right
res290: List[Int] = List(0, 10, 30, 60, 70, 110, 160)
scala> l.scanRight(0)(_+_) //takes a starting valus and a
reduction function and returns list of each accumlated value from right to left
res291: List[Int] = List(160, 150, 130, 100, 90, 50, 0)
Permutations and combinations
-------------------------------------
scala> val l=List(1,2,3,4)
l: List[Int] = List(1, 2, 3, 4)
scala> l.combinations(3).foreach(x=>println(x.mkString(",")))
1,2,3
1,2,4
1,3,4
2,3,4
scala>
l.combinations(2).foreach(x=>println(x.mkString(",")))
1,2
1,3
1,4
2,3
2,4
3,4
^
scala>
l.permutations.foreach(x=>println(x.mkString(",")))
1,2,3,4
1,2,4,3
1,3,2,4
1,3,4,2
1,4,2,3
1,4,3,2
2,1,3,4
2,1,4,3
2,3,1,4
2,3,4,1
2,4,1,3
2,4,3,1
3,1,2,4
3,1,4,2
3,2,1,4
3,2,4,1
3,4,1,2
3,4,2,1
4,1,2,3
4,1,3,2
4,2,1,3g
4,2,3,1f
4,3,1,2
4,3,2,1
scala>
l.sliding(3).foreach(x=>println(x.mkString(",")))
1,2,3
2,3,4
scala>
l.inits.foreach(x=>println(x.mkString(",")))
1,2,3,4
1,2,3
1,2
1
scala>
l.tails.foreach(x=>println(x.mkString(",")))
1,2,3,4
2,3,4
3,4
4








Comments
Post a Comment