Scala higher-order method in the List class

Source here: https://www.cnblogs.com/gaopeng527/p/4102937.html

Among a list of mapping: map, flatMap and foreach

1.xs map f the operation returns a list of the new function f xs after each list element thus composed. Such as:

 

scala> List(1, 2, 3) map (_ + 1)
res0: List[Int] = List(2, 3, 4)

scala> val words = List("the", "quick", "brown", "fox")
words: List[String] = List(the, quick, brown, fox)

scala> words map (_.length)
res1: List[Int] = List(3, 5, 5, 3)

scala> words map (_.toList.reverse.mkString)
res2: List[String] = List(eht, kciuq, nworb, xof)

 

2.flatMap map operator with similar, but its right operand is a function to return a list of elements. It lists each element of the method is called, and then connect the results of all methods and return. FlatMap difference map and exemplified as follows:

 

scala> words map (_.toList)
res3: List[List[Char]] = List(List(t, h, e), List(q, u, i, c, k), List(b, r, o,
w, n), List(f, o, x))

scala> words flatMap (_.toList)
res4: List[Char] = List(t, h, e, q, u, i, c, k, b, r, o, w, n, f, o, x)

 

List.range tool that can create a list of all integers method within a certain range. E.g:

scala> List.range(1, 5) flatMap (i => List.range(1, i) map (j => (i, j)))
res6: List[(Int, Int)] = List((2,1), (3,1), (3,2), (4,1), (4,2), (4,3))

3.foreach third mapping is similar to the operation. It is the right operand the process (function returns Unit). It just calls for each list element process again. The result of the operation is still Unit, will not produce the results list. E.g:

 

scala> var sum = 0
sum: Int = 0

scala> List(1, 2, 3, 4, 5) foreach (sum += _)

scala> sum
res9: Int = 15

 

List Filter: filter, partition, find, takeWhile , dropWhile and span
1.xs filter operation generates P xs that match p (x) is a list of all the elements of the true composition. Such as:

scala> List (1, 2, 3, 4, 5) filter (_ % 2 == 0)
res10: List[Int] = List(2, 4)

scala> words filter (_.length == 3)
res11: List[String] = List(the, fox)

2.partition methods and filter similar, but returns a list of pairs. One contains all the elements of the thesis is true, the other false assertion that contains all the elements.
xs partition p is equivalent to (xs filter p, xs filter ( ! p ()))

For example as follows:

scala> List(1, 2, 3, 4, 5) partition (_ % 2 ==0)
res12: (List[Int], List[Int]) = (List(2, 4),List(1, 3, 5))

3.find same method and filter method is similar, but returns the first element satisfies the given argument, but not all. Find xs xs p operable to list p of operands and judgment. Optional return value. If the element is present xs x such that p (x) is true, Some (x) returns. Otherwise, if p is not true for all elements, None will be returned. For example as follows:

scala> List(1, 2, 3, 4, 5) find (_ % 2 == 0)
res13: Option[Int] = Some(2)

scala> List(1, 2, 3, 4, 5) find (_  <= 0)
res15: Option[Int] = None

4. xs takeWhile p xs operation returns a list of longest prefix of p can be satisfied. E.g:

scala> List(1, 2, 3, -4, 5) takeWhile (_ > 0)
res16: List[Int] = List(1, 2, 3)

5.xs dropWhile p removing operation to meet the longest prefix of p. For example as follows:

scala> words dropWhile (_ startsWith "t")
res17: List[String] = List(quick, brown, fox)

The method and takeWhile 6.span dropWhile combined into one operation. And it returns the list, the definition is consistent with the following equation:
XS span equivalent to P (xs takeWhile p, xs dropWhile p )

scala> List(1, 2, 3, -4, 5) span (_ >0)
res18: (List[Int], List[Int]) = (List(1, 2, 3),List(-4, 5))

 Thesis list: forall and exists

1. xs forall p if all the elements of the list that p returns true.

2. xs exists p If there is a list of values ​​that satisfy p returns true.

For example, to find out whether the expression of matrix as a list of lists there are rows of all zeros:

 

scala> def hasZeroRow(m: List[List[Int]]) = m exists (row => row forall (_ == 0))
hasZeroRow: (m: List[List[Int]])Boolean

scala> val m= List(List(3,0,0), List(0,3,0), List(0,0,3))
m: List[List[Int]] = List(List(3, 0, 0), List(0, 3, 0), List(0, 0, 3))

scala> hasZeroRow(m)
res21: Boolean = false

 

 Folding list: /: and: \

scala> def sum(xs: List[Int]): Int = (0 /: xs) (_ + _)
sum: (xs: List[Int])Int

sum (List (a, b, c)) is equivalent to 0 + a + b + c

scala> def product(xs: List[Int]): Int = (1 /: xs) (_ * _)
product: (xs: List[Int])Int

product (List (a, b, c)) is equivalent to 1 * a * b * c
left folding operation "(z /: xs) ( op)" and related three objects: start value z, a list of xs, and di- yuan operation op. Folding op result is applied to the prefix and the value of z each adjacent element.

: \ Operator is referred to as a right folded. As with the left fold with three operand, but the opposite order of appearance of the first two: the first operand is a list to be folded, the second start value. (Xs \: z) (op)

Sort the list: sortWith

Xs list of operations, the method xs sortWith before, sorting can be performed on the list of elements, wherein "before" is the comparison element. For example as follows:

 

scala> List(1, -3, 4, 2, 6) sortWith (_ < _)
res2: List[Int] = List(-3, 1, 2, 4, 6)

scala> words sortWith (_.length > _.length)

res5: List[String] = List(quick, brown, the, fox)

 

Guess you like

Origin blog.csdn.net/helloworld0906/article/details/95318356