Hyercar
Hyercar
c. List of Integers
scala> val evenNumbers = List (2,4,6,8)
evenNumbers: List[Int] = List(2, 4, 6, 8)
f. 2 Dimensional List
scala> val matrix = List(
| List(1,2,3),
| List(4,5,6),
| List(7,8,9)
| )
matrix: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6),
List(7, 8, 9))
scala> evenNumbers(0)
res0: Int = 2
scala> evenNumbers(1)
res1: Int = 4
scala> evenNumbers(2)
res2: Int = 6
scala> evenNumbers(3)
res3: Int = 8
scala> evenNumbers(0)
res5: Int = 2
scala> evenNumbers(1)
res6: Int = 4
scala> evenNumbers(2)
res7: Int = 6
scala> evenNumbers(3)
res8: Int = 8
scala> colors.isEmpty
res14: Boolean = false
f. size: Check the size of List
scala> colors.size
res15: Int = 3
scala> numbers.size
res16: Int = 0
b. Add Blue at the end. It wont work because you defined as a val
scala> colors = colors:+"Blue"
<console>:8: error: reassignment to val
colors = colors:+"Blue"
^
d. Create a new list with “Blue” and overwrite colors (ONLY POSSIBLE BECAUSE colors IS
DEFINED AS var )
scala> colors = colors:+"Blue"
colors: List[String] = List(Red, Green, Yellow, Blue)
scala> colors.foreach(println(_))
Voilet
Orange
Red
Green
Yellow
Blue
scala> colors.foreach(println)
Voilet
Orange
Red
Green
Yellow
Blue
scala> val numbers = List(2,4,6,8)
numbers: List[Int] = List(2, 4, 6, 8)
c. Modify the element in index 2 [YOU CAN NOT MODIFY THE CONTENTS OF LIST BECAUSE LIST
IS IMMUTABLE]
scala> colors(2) = "RED"
<console>:9: error: value update is not a member of
List[String]
colors(2) = "RED"
^
NOTE: EVEN IF THE LIST WAS DEFINED AS VAR, YOU STILL CAN NOT MODIFY THE CONTENTS OF
LIST BECAUSE LIST IS IMMUTABLE
scala> var colors = List("Voilet", "Orange", "Red", "Green",
"Yellow", "Blue")
colors: List[String] = List(Voilet, Orange, Red, Green,
Yellow, Blue)
scala> colors(2) = "RED"
<console>:9: error: value update is not a member of
List[String]
colors(2) = "RED"
^
d. Filter the elements
scala> var colorsWithLength3 = colors.filter(x=>x.length==3)
colorsWithLength3: List[String] = List(Red)
scala> colorsWithLength5orMore.exists(x=>x=="Green")
res35: Boolean = true
scala> colorsWithLength5orMore.exists(x=>x=="Blue")
res36: Boolean = false
NOTE: IF THINGS ARE NOT CLEAR, JUST PUT A PRINT STATEMENT, SO YOU MAKE OUT THINGS
BETTER, EXAMPLE BELOW
scala> colorsWithLength5orMore.exists(color=>{println(color);
color=="Blue"})
Voilet
Orange
Green
Yellow
res41: Boolean = false
List (Day 2 of 2)
Explaination
rows\columns 0 1 2
0 0 0 0
1 0 1 2
2 0 2 4
scala> numbers.reverse
res0: List[Int] = List(18, 25, 1, 9, 5, 2, 1, 0)
Note: Contents of list will be changed, List is immutable
scala> numbers
res1: List[Int] = List(0, 1, 2, 5, 9, 1, 25, 18)
scala> newNumbers
res2: List[Int] = List(18, 25, 1, 9, 5, 2, 1, 0)
B. sortBy
scala> numbers.sortBy(x=>x)
res6: List[Int] = List(0, 1, 1, 2, 5, 9, 18, 25)
scala> numbers.sortBy(x=>x).reverse
res7: List[Int] = List(25, 18, 9, 5, 2, 1, 1, 0)
Complex
scala> listOfCars.sortBy(_.cost)
res14: List[cars] = List(cars(Mercedes,50000),
cars(Jaquar,70000), cars(BMW,80000))
C. sortWith
scala> val numbers = List(0, 1, 2, 5, 9, 1, 25, 18)
numbers: List[Int] = List(0, 1, 2, 5, 9, 1, 25, 18)
scala> numbers.sortWith((x,y)=>x<y)
res18: List[Int] = List(0, 1, 1, 2, 5, 9, 18, 25)
scala> numbers.sortWith((x,y)=>x>y)
res19: List[Int] = List(25, 18, 9, 5, 2, 1, 1, 0)
scala> numbers.sortWith(_>_)
res20: List[Int] = List(25, 18, 9, 5, 2, 1, 1, 0)
Complex
# More complex
scala> listOfCars.sortWith(sortingLogic)
80000BMW
50000Mercedes
70000Jaquar
80000BMW
80000BMW
70000Jaquar
70000Jaquar
50000Mercedes
70000Jaquar
80000BMW
80000BMW
70000Jaquar
res25: List[cars] = List(cars(BMW,80000), cars(Jaquar,70000),
cars(Mercedes,50000))
a. import
import scala.collection.mutable.ListBuffer
scala> numbers+=1
res28: numbers.type = ListBuffer(0, 1)
scala> numbers+=2
res29: numbers.type = ListBuffer(0, 1, 2)
scala> numbers+=(2,4,6,8,10)
res30: numbers.type = ListBuffer(0, 1, 2, 2, 4, 6, 8, 10)
d. Remove elements from ListBuffer
scala> numbers-=(2,10)
res31: numbers.type = ListBuffer(0, 1, 2, 4, 6, 8)
f. You can not add or remove elements in a List because List is immutable
scala> numbersList+=5
<console>:11: error: value += is not a member of List[Int]
numbersList+=5
^
g. Use Tab to see all the methods that you can apply to a ListBuffer
scala> numbers.
++ ++: ++=
++=: +: +=
+=: - --
--= -= /:
/:\ :+ :\
addString aggregate andThen
append appendAll apply
applyOrElse asInstanceOf canEqual
clear clone collect
collectFirst combinations companion
compose contains containsSlice
copyToArray copyToBuffer corresponds
count diff distinct
drop dropRight dropWhile
endsWith exists filter
filterNot find flatMap
flatten fold foldLeft
foldRight forall foreach
genericBuilder groupBy grouped
hasDefiniteSize head headOption
indexOf indexOfSlice indexWhere
indices init inits
insert insertAll intersect
isDefinedAt isEmpty isInstanceOf
isTraversableAgain iterator last
lastIndexOf lastIndexOfSlice lastIndexWhere
lastOption length lengthCompare
lift map mapResult
max maxBy min
minBy mkString nonEmpty
orElse padTo par
partition patch permutations
prefixLength prepend prependAll
prependToList product readOnly
reduce reduceLeft reduceLeftOption
reduceOption reduceRight reduceRightOption
remove repr result
reverse reverseIterator reverseMap
runWith sameElements scan
scanLeft scanRight segmentLength
seq size sizeHint
sizeHintBounded slice sliding
sortBy sortWith sorted
span splitAt startsWith
stringPrefix sum tail
tails take takeRight
takeWhile to toArray
toBuffer toIndexedSeq toIterable
toIterator toList toMap
toSeq toSet toStream
toString toTraversable toVector
transform transpose trimEnd
trimStart union unzip
unzip3 update updated
view withFilter zip
zipAll zipWithIndex