You are on page 1of 20

Lab 4

Concepts of programming languages


Important Notes

1. There will be a Quiz in the week following the midterm exam,


starting from 25/3.

2. Every student must attend in his/her section, otherwise you will


lose -25% of the total grade.

3. Please bring your own laptop during the quiz.

4. Please don’t be late for your section time.


Agenda
1. Foreach
2. Map
3. Filter
4. Reduce
5. FoldLeft
6. Return Function
7. Hands on
Foreach
1.  The foreach function is applicable to Scala's collection data structures.
2. The foreach method takes a function as parameter and applies it to
every element in the collection.
3. you can use foreach method to loop through all elements in a
collection.
Foreach
object Main { var sum = 0
def main(args: Array[String]): s1.foreach(num => sum += num);
Unit = { println(sum);
// Creating a list
val s1 = List(3, 7, 12, 9, 21)

// Applying foreach method


s1.foreach(x => println(x))
s1.foreach( println(_))
}
}Output: Output:
3 52
7
12
9
Foreach
def myFunc(x: Int): Unit = {
var result = x * 2
Output: 2
println(result) 4
} 6
8
10
12
var ran = Range(1,10) 14
16
ran.foreach(myFunc) 18
Map

1.  The map is a function that transforms one collection into another


collection.
2. It’s your job to describe how to transform each element. every
element in the collection.
Map

//Function as Parameter
val salaries = List(20000, 70000, 40000)
val doubleSalary = (x: Int) => x * 1.5
val newSalaries = salaries.map(doubleSalary)
println(newSalaries)
Output: List(30000.0, 105000.0, 60000.0)
Filter
1. The filter allows us to clear a collection from elements we are not
interested in
2. It accepts a predicate
3. This predicate should evaluate to a boolean (true or false)
4. It removes all elements that do not satisfy that predicate
Filter

val numbers = Range(1, 20) val numbers=List("one","two","three")


val evenNumbers =
numbers.filter(_ % 2 == 0) val filteredNumbers=numbers.filter(x
=> x.length==3 && x.contains(‘o’))
println(evenNumbers)
println(filteredNumbers)
Output:Vector(2, 4, 6, 8, 10, 12,
14, 16, 18)

Output:List(one, two)
Reduce

1. The reduce method is a higher order method that takes all the
elements in a collection (list ,array ,seq..) and combine them using a
binary operation to produce a single value.
2. It necessary to make sure that the operations are commutative and
associative.
3. This Anonymous functions are passed as a parameter to the reduce
function.
4. The result should be of the same type as that of the collection elements
Reduce
var collection = List(200, val ch = List("A", "B", "C",
400, 100, 30, 1000, 500) "D", "E", "F")

val result = val str = ch.reduce((x1, x2)


collection.reduce((x1, x2) => x1 +x2)
=> x1 max x2)
println(str)
println(result)
Output: ABCDEF
Output: 1000
Foldleft

1. The foldleft iterates through the list from left to right, accumulating
elements in that order.
2. FoldLeft is quite similar to reduce
3. The difference is that we can set an initial value for the accumulator
Foldleft
val array = List(1,2,3,4,5)

val output = array.foldLeft(25)(_+_)

println(“the sum of list after adding


25 is : ” + output)

Output: the sum of list


after adding 25 is : 40
Return Function

1.  You want to return a function from a function


▪ Define a function that returns an algorithm (an anonymous function),
▪ assign that to a new function,
▪ and then call that new function.
Return Function
• The following code declares an anonymous function that takes a String
argument and returns a String:

(s: String) => { prefix + " " + s }

• You can return that anonymous function from the body of another function as
follows:
def saySomething(prefix: String): String=>String =
(s: String) => {prefix + " " + s}
Return Function
• val sayHello = saySomething("Hello")
• Because saySomething returns a function, you can assign that resulting
function to a variable.
• sayHello(“Ali”)

val sayHello = saySomething("Hello")

println(sayHello("Ali”))
Output: Hello Ali
Hands on (Return Function)

• Create a greet method that returns a function.

• That function will take a string parameter and print it using println.

• p.S : To simplify, greet won’t take any input parameters; it will just
build a function and return it.
Hands on (Return Function)

// a method that returns a function


def greet(): String => Unit =
(name: String) => println(“Hello “ +name)
// call greet
val greetFunction = greet()
//call greetFunction
greetFunction("Joe") // prints "Hello, Joe"
Thank you.

You might also like