Scala Collections List, Sequence, reduceLeft, filter, foldLeft, map and flatMap

Category : Scala | Sub Category : Scala Programs | By Runner Dev Last updated: 2020-10-10 09:31:01 Viewed : 236


Scala Collections List, Sequence, reduceLeft, filter, foldLeft, map and flatMap

Seq:

Creates a collection with the specified elements.

def apply[A](elems: A*): Seq[A] 

LinearSeq:

Creates a collection with the specified elements.

def apply[A](elems: A*): LinearSeq[A]

IndexedSeq:

Creates a collection with the specified elements.

def apply[A](elems: A*): IndexedSeq[A] 

List:

Converts this range to a list.

def toList: List[Int]

reduceLeft:

Applies a binary operator to all elements of this list, going left to right.

override def reduceLeft[B >: Int](op: (B, Int) => B): B

filter:

Selects all elements of this list which satisfy a predicate.

def filter(p: Int => Boolean): List[Int]

foldLeft:

Applies a binary operator to a start value and all elements of this list, going left to right.

override def foldLeft[B](z: B)(op: (B, Int) => B): B

map:

Builds a new collection by applying a function to all elements of this list.

final override def map[B, That](f: Int => B)(implicit bf: CanBuildFrom[List[Int], B, That]): That

flatMap:

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

final override def flatMap[B, That](f: Int => GenTraversableOnce[B])(implicit bf: CanBuildFrom[List[Int], B, That]): That 

Example:

Following example illustrates about Scala Collections List, Sequence, reduceLeft, filter, foldLeft, map and flatMap.

Save the file as −  SeqMainApp.scala.  

SeqMainApp.scala  

 package runnerdev

import scala.collection.immutable.LinearSeq 

object SeqMainApp extends App {

  val seq = Seq(123)

  println("seq " + seq) 

  val set = Set(123)

  println("set " + set) 

  val map = Map(1 -> "a"2 -> "b"3 -> "c")

  println("map " + map) 

  val indexedSeq = IndexedSeq(123)

  println("indexedSeq " + indexedSeq) 

  val linearSeq = LinearSeq(123)

  println("linearSeq " + linearSeq) 

  //val isOdd: Int => Boolean

  val isOdd = (elem: Int) => elem % 2 == 0

  //val isEven: Int => Boolean

  val isEven = (elem: Int) => elem % 2 != 0 

  var listMod = List(123).filter(x => x % 2 == 1)

  println("listMod " + listMod)

  var listOddFilter = List(123).filter(x => isOdd(x))

  println("listOddFilter " + listOddFilter)

  var listOdd = List(123).filter(isOdd)

  println("listOdd " + listOdd) 

  //Transformation // val lowerCapsList: List[String]

  val lowerCapsList = List("A""Cat").map(s => s.toLowerCase)

  println("lowerCapsList " + lowerCapsList) 

  //val largest: (Int, Int) => Int

  val largest = (a: Int, b: Int) => if (a > ba else b

  //Reduce

  var reduceLeft = List(86222).reduceLeft((ab) => a max b)

  println("reduceLeft " + reduceLeft)

  var reduceLeft1 = List(86222).reduceLeft((ab) => largest(ab))

  println("reduceLeft1 " + reduceLeft1) 

  var reduceLeft2 = List(86222).reduceLeft(largest)

  println("reduceLeft2 " + reduceLeft2)

  var reduceLeftmax = List(86222).reduceLeft(_ max _)

  println("reduceLeftmax " + reduceLeftmax) 

  var maxList = List(12).reduceLeft { (ab) =>

    println("(a -> b) " + (a -> b))

    a max b

  }

  println("maxList " + maxList)

  //reduceLeft throws an exception on an Nil (empty) List

  var listFoldLeft = List(1234).foldLeft(0)(_ + _)

  println("listFoldLeft " + listFoldLeft//10

  var listFoldLeft1 = List(1234).foldLeft(1)(_ * _)

  println("listFoldLeft1 " + listFoldLeft1) //24

  val n = (1 to 3).toList

  println("n " + n)

  val resultMap = n.map(i => n.map(j => i * j))

  println("resultMap " + resultMap)

  val resultFlateMap = n.flatMap(i => n.map(j => i * j))

  println("resultFlateMap " + resultFlateMap) 

} 

compile and run the above example as follows 

scala> scalac SeqMainApp.scala

scala> scala SeqMainApp 

Output:

seq List(1, 2, 3)

set Set(1, 2, 3)

map Map(1 -> a, 2 -> b, 3 -> c)

indexedSeq Vector(1, 2, 3)

linearSeq List(1, 2, 3)

listMod List(1, 3)

listOddFilter List(2)

listOdd List(2)

lowerCapsList List(a, cat)

reduceLeft 22

reduceLeft1 22

reduceLeft2 22

reduceLeftmax 22

(a -> b) (1,2)

maxList 2

listFoleLeft 10

listFoleLeft1 24

n List(1, 2, 3)

resultMap List(List(1, 2, 3), List(2, 4, 6), List(3, 6, 9))

resultFlateMap List(1, 2, 3, 2, 4, 6, 3, 6, 9)

Search
Sub-Categories
Related Articles

Leave a Comment: