Great start with filter, map, flatMap, and for comprehension

Smart Searching Through Trillion of Research Papers with Apache Spark ML
Reading Time: 2 minutes

Scala has a very large collection set. Collections are like containers that hold some linear set of values, and we apply some operations like filter, map, flatMap and for comprehension of the collections and manipulate them in a new collection set.

filter

Selects all elements of the collection that satisfy a predicate.

def filter(p:A=>boolean):List[A] 
  • Params: p- It used to test elements
  • Returns: A new collection consisting of all elements of this collection that satisfy the given predicates p. that order of the elements is preserved.

Example:

val numList = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

val evenNumList = numList.filter(n => n % 2 == 0)

print(evenNumList)

output:

List(2, 4, 6, 8, 10)

map

Build a new collection by applying a function to all elements of the collection.

def map(f: A => B): List[B]
  • Params: f- the function apply each element.
  • Type parameters: B- the element type of the returned collection.
  • Returns: a new collection resulting from applying the given function f to each element of the collection and collection of the results.

Example:

val list = List(1,2,3,4)

val AddOneInEachElement = list.map(n=>n+1)

print(AddOneInEachElement)

Output:

List(2, 3, 4, 5)

flatMap

Builds a new collection by applying a function to all elements of the collection and using the elements of the resulting collections.

def flatMap(f: A => IterableOnce[B]): List[B]
  • Params: f- the function to apply to each element.
  • Type parameters: B- the element type of the returned collection.
  • Returns: A new collection resulting from applying the given collection-valued function f to each element of the collection and concatenating the results.

Example:

val num = List(1, 2)

val char = List('a', 'b')

println(num.flatMap(x => char.map(y => s"$x$y")))

Output:

List(1a, 1b, 2a, 2b)

for comprehension

Comprehension is a construct in Scala which allows us to evaluate certain expressions. The For Comprehensions has the form for (enumerators) yield e, where enumerators refer to a semi-colon separated list of enumerators.

The enumerator can either be a generator that iterates the list or a filter. Comprehension evaluates the body e for each result generated by the enumerator and returns a list of these values.

Example:

val num = List(1, 2)

val char = List('a', 'b')

val combination = for{

    n<-num

    c<-char

  }yield s"$n$c"
  
print(combination)

Output

List(1a, 1b, 2a, 2b)

Conclusion

Through this blog, You will get the basic knowledge about filter, map, for comprehension, and flatMap also It provides us with a more concise and readable code.

Written by 

Vineet Chauhan is a Software Consultant at Knoldus Inc. His practice area is Scala. He is a good team worker. He is always ready to face any challenging task.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading