Good programmers to share large data set of learning path manipulation functions Scala Series

Good programmers continue to share large data line learning Scala series of set operations functions for everyone
4.6 a collection of important functions
4.6.1sum / max / min / count
find the maximum or minimum is a very common requirement in sequence, as follows:
Val a Numbers Seq = (. 11, 2,. 5,. 1,. 6,. 3,. 9)
numbers.max //. 11
numbers.min. 1 //
more advanced example, a book that contains a sequence
case class book (title: String, pages: int)
Val Books = Seq (Book ( "Future of Scala Developers", 85),
Book ( "Parallel algorithms", 240),
Book ( "Object Oriented Programming", 130.),
Book ( "Mobile Development", 495))
Book // (Mobile Development, 495)
books.maxBy (Book => book.pages)
// Book (Scala Developers of Future, 85)
books.minBy (Book => book.pages)
shown above, minBy & maxBy method solve the problem of complex data. You simply select the maximum or minimum attribute decision data.
4.6.2 Filter
A digital filter List, acquiring only the odd elements.
Numbers Seq = Val (1,2,3,4,5,6,7,8,9,10) numbers.filter (n-=> n-% 2 == 0)
Val Books = Seq (Book ( "Future of Scala Developers ", 85),
Book (" Parallel algorithms ", 240),
Book (" Object Oriented Programming ", 130.),
Book (" Mobile Development ", 495))
books.filter (Book => book.pages> = 120 )
4.6.3 The Flatten
Val ABCD Seq = ( 'A', 'B', 'C', 'D')
Val efgj Seq = ( 'E', 'F', 'G', 'H')
Val = IJKL Seq ( 'I', 'J', 'K', 'L')
Val MNOP = Seq ( 'm', 'n-', 'O', 'P')
Val QRST = Seq ( 'Q', 'R & lt ',' s', 't '




List // (A, B, C, D, E, F, G, H, I, J, K, L, m, n-, O, P, Q, R & lt, S, T,
// U, V, W, X, Y, Z)
alphabet.flatten
when there is a collection of collections, and you want to set all of these elements when operated, it will be used flatten.
4.6.4 operation between the set of
difference, intersection and union
Val num1 Seq = (. 1, 2,. 3,. 4,. 5,. 6)
Val num2 Seq = (. 4,. 5,. 6,. 7,. 8,. 9)
/ / List (. 1, 2,. 3)
num1.diff (num2)
// List (. 4,. 5,. 6)
num1.intersect (num2)
// List (. 1, 2,. 3,. 4,. 5,. 6,. 4,. 5 ,. 6,. 7,. 8,. 9)
num1.union (num2)
above example union retained duplicate elements. If we do not need to repeat how to do? Then you can use distinct functions
// List (. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9)
num1.union (num2) .distinct
The following is an illustration of the above functions:
Good programmers to share large data set of learning path manipulation functions Scala Series
4.6.5 Map (Mapping ) list element
map Scala is a collection of the most commonly used functions. It's very powerful:
Val a Numbers = Seq (1,2,3,4,5,6)
List // (2,. 4,. 6,. 8, 10, 12 is)
numbers.map (n-=> * n-2)
Val chars Seq = ( 'A', 'B', 'C', 'D')
// List (a, B, C, D)
chars.map (CH => ch.toUpper)
logical map function is to traverse element in the set and each element of the calling function.
FlatMap 4.6.6
flatMap by these two functions of the following composition:
Map & Flatten
example:
Val ABCD Seq = ( 'A', 'B', 'C', 'D')
// List (A, A, B , B, C, C, D, D)
abcd.flatMap (CH => List (ch.toUpper, CH))
4.6.7 for inspection of the entire set of conditions

  1. val numbers = Seq(3, 7, 2, 9, 6, 5, 1, 4, 2)
  2. //ture numbers.forall(n => n < 10)
  3. // false numbers.forall (n => n > 5)
    and forall function is to deal with such demands created.
    4.6.8 collection of packets
    such as the collection of a set of split into even and odd set, partition functions can help us to do this:
    Val Seq Numbers = (. 3,. 7, 2,. 9,. 6,. 5,. 1, . 4, 2)
    // (List (2,. 6,. 4, 2), List (. 3,. 7,. 9,. 5,. 1))
    numbers.partition (n-=> n-% 2 == 0)
    4.6.9 Fold
    other a popular operation is the fold, can generally be considered foldLeft and foldRight. They are doing the same work from different aspects:
    Val Seq Numbers = (. 1, 2,. 3,. 4,. 5)
    // 15 numbers.foldLeft (0) ((RES, n-) => + n-RES)
    Val words = SEQ ( "Apple", "Dog", "Table")
    // 13 is words.foldLeft (0) ((ResultLength, Word) => + ResultLength word.length)
    foldLeft, reduceRight, and foldRight
    method foldLeft method is working with reduceLeft elephant, but it allows you to specify a value as the first element.
    scala> val a = Array (1 , 2, 3)
    a: Array[Int] = Array(1, 2, 3)

scala> a.reduceLeft(+)
res6: Int = 6

scala> a.foldLeft(100)(+)
res7: Int = 106

scala> a.foldLeft(200)(+)
res8: Int = 206

Guess you like

Origin blog.51cto.com/14479068/2437113