Scala express syntax | data structure

Keywords: Scala

Scala express syntax (11) | data structure

map mapping function

  • The map function returns a new collection
  • Pass the elements of the collection through the functions in ()
  //Create list
  val list = List(1, 2, 3, 4, 5)
  //Each element * 2
  println(list.map(_ * 2))

flatmap mapping: flat is flattening, flattening and flattening mapping

  • flatmap flattens all elements in the collection, that is, it breaks up all elements
 val names = List("Alice", "Bob", "Nick")
  //The requirement is to List all elements in the List set,
  val names2 = names.flatMap(upper)
  println("names2=" + names2)
  }

  def upper( s : String ) : String = {
    s. toUpperCase
  }
  //Output: names2=List(A, L, I, C, E, B, O, B, N, I, C, K)

filter collection element filtering

  • filter places the qualified elements in the collection in the new collection
  • The passed in function returns a Boolean value
    //Returns A string that begins with A
    val name2 = names.filter(_.startsWith("A"))
    println(name2)

reduce/reduceLeft/reduceRight

  • reduce passes the first two values in the set into the function to get the value, and then passes it into the first parameter, and the second parameter uses the next value in the set
  • reduceLeft is from left to right by default
  • The return value of the passed in function is required to be of the same type as the collection element
   val list = List(1, 2, 3, 4, 5)
   //list accumulation
   println(list.reduce(_ + _))

fold/foldLeft/foldRight fold

  • fold passes in an initial value as the first parameter, and the function continues to pass the value returned in the previous step as the first parameter of the function to participate in the operation until all elements in the list are traversed
  • reduceLeft can be regarded as a simplified foldLeft
  • foldLeft and foldRight are abbreviated as: /: and:\
  val list = List(1, 2, 3, 4, 5)
  println(list.fold(10)(_ + _ ))
  
  var i6 = (1 /: list4) (minus) // =Equivalence = > list4.foldleft (1) (pass in function)
  i6 = (100 /: list4) (minus) //=Equivalence = > list4.foldleft (100) (pass in function)

Scan scan

  • Scan scan, that is, fold all elements of a set, but save all intermediate results in a set
  • Intermediate results are saved
  val list = List(1, 2, 3, 4, 5)
  val list2 = list.scan(0)(_ + _)
  println(list2)
  
  //Result List(0, 1, 3, 6, 10, 15)

Zipper (combined)

  • zip in the development, when we need to combine the dual elements of two sets, we can use zipper
  // zipper
  val list1 = List(1, 2, 3)
  val list2 = List(4, 5, 6)
  val list3 = list1.zip(list2) // (1,4),(2,5),(3,6)
  println("list3=" + list3)

The essence of zipper is the merging operation of two sets. After merging, each element is a dual tuple
If the number of two sets does not correspond, the data will be lost
The set is not limited to List, but can also be other sets, such as Array
If you want to extract the data of each dual tuple after merging, you can traverse it in tuple mode

Iterator iterator

val iterator = List(1, 2, 3, 4, 5).iterator // Get iterator

//ergodic
while (iterator.hasNext) {
  println(iterator.next())
}

//Traversal 2
for(enum <- iterator) {
  println(enum) //
}

Stream stream

  • stream is a collection. This set can be used to store an infinite number of elements, but these infinite elements will not be produced at one time, but will be produced dynamically according to the required interval. The last element follows the lazy rule
//Create Stream
def numsForm(n: BigInt) : Stream[BigInt] = n #:: numsForm(n + 1)
val stream1 = numsForm(1)
println(stream1) 

//The first element of the created set is n, and the generation rule of subsequent elements is n + 1
//The rules for generating subsequent elements are specified, such as numsform (n * 4)

View view

  • With the lazy loading feature of Stream, you can also apply the view method to other collections to achieve similar results
  • The view method produces a collection that is always lazy
  • The View does not cache data and needs to be recalculated every time, such as when traversing the View
//Please find all numbers in 1-100, which are arranged in reverse order and are the same as itself. (1 2, 11, 22, 33 ...)

//If this number is equal to the original number in reverse order, it returns true; otherwise, it returns false
def eq(i: Int): Boolean = {
  println("eq Called..")
  i.toString.equals(i.toString.reverse)
}


//Use view to complete this problem. In the program, map,filter,reduce,fold the collection
//If you do not want to execute immediately, but only after using the results, you can use view for optimization
val viewSquares2 = (1 to 100).view.filter(eq)
  println(viewSquares2)
  //ergodic
  for (item <- viewSquares2) {
    println("item=" + item)
  }
}

Posted by nashyboy on Thu, 04 Nov 2021 18:23:12 -0700