-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListOperations.scala
80 lines (64 loc) · 1.8 KB
/
ListOperations.scala
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
class ListOperations {
/* def area(shape:String,length:Int,breadth: Int, f:(Int,Int)=> Int):Int = {
var length = 0
var breadth = 0
f(length * breadth)
f({(length * breadth)/2)
}*/
def sumOfList(listNew1: List[Int], listNew2: List[Int]): List[Int] = {
val vector1 = for (iteration <- 0 until listNew1.length) yield listNew1 (iteration) + listNew2 (iteration)
val Sum = vector1.toList
Sum
}
def findLength(newList: List[Int])
{
val xiteration= 0
var temp = 0
for (xiteration <- newList)
{
temp = xiteration
}
print ("Length = " + temp+ "/n")
}
//**************************************************
def mapUse(list: List[Int])
{
print("List before using map: "+ list+"/n")
print("List after using map:/n " + list.map(x => x * 2))
}
def fibo(prev: Int,next:Int,num: Int):Int = {
num match {
case no1 => prev
case no2 => next
case _ => fibo(next, prev + next, num-1)
}
}
def reverseOfList(list1: List[Int])= list1.reverse
def reverseList(list1: List[Int]):List[Int] = {
list1 match {
case rev1:: tail => reverseOfList(tail) ::: List(rev1)
case Nil => Nil
}
}
}
object Operations{
def main(arg:Array[String]) {
val listob = new ListOperations
val value1 = 1
val value2 = 2
val value3 = 3
val value4 = 4
val newList = List (value1,value2,value3,value4)
val listNew1 = List (value1,value2,value3,value4)
val listNew2 = List (value1,value2,value3,value4)
print("Reverse Of a List is: " + listob.reverseOfList(newList)+"/n")
listob.findLength(newList)
val list =List(4,5,6)
listob.mapUse (list)
print("SumOfList" + listob.sumOfList(listNew1,listNew2)+"/n")
val fib1 = 0
val fib2 = 1
val fib3 = 3
println("Fibonacci: " + listob.fibo(fib1,fib2,fib3))
}
}