Collection Basics See the previous blog post Scala quick Learning (ii).
This article is primarily acombinator, because it is useful to find in practice. Main reference: http://www.importnew.com/3673.html
List(1,2,3) map squared
The function is applied separately on each element of the list, squared
and a new list is returned, possibly List(1,4,9)
. We refer to map
operations like this as a combination .
Features: The parameters of the assembly are all functions, and the input and output of this function is a list element. The most common way is that the anonymous function is defined with + = and the left input is the right output.
Ps: Distinguish, <-is mainly used in iterative,-> is mainly used in map,=> is anonymous function.
- Map: Computes a function on each element in the list, and returns a list containing the same number of elements.
Scala> Numbers.map ((i:int) = i * 2= List (2, 4, 6, 8) Scala> Def timestwo (i:int): Int = i * 2< C2>timestwo: (i:int) Int Scala>= List (2, 4, 6, 8)
- Foreach:foreach and map use similar methods, except that it has no return value, and foreach is used to manipulate the original list.
- Filter: Removes any element that causes the passed-in function to return FALSE. So a function argument is an assertion function (a function that returns a Boolean type is generally called an assertion function).
- Zip: synthesizes Two list elements into a list of element pairs.
Scala> list (1, 2, 3). zip (List ("A", "B", "C"= List (1,a), (2,b), (3,C))
partition:
The list is split based on the return value of the assertion function.
scala> val numbers = List (1, 2, 3, 4, 5, 6, 7, 8, 9, ten) Scala> numbers.partition (_%2 = =0 = (List ( 2, 4, 6, 8, ten), List (1, 3, 5, 7, 9))
- Find: Returns the first element in the collection that matches an assertion function.
- Drop: Discards the first I element.
dropWhile:
removes the first few elements that match the assertion function.
- Fold,foldleft,foldright: Cumulative operation. The fold method in list needs to enter two parameters: the initial value and a function. The input function also needs to enter two parameters: the cumulative value and the index of the current item. Fold indefinite order, foldleft from left to right.
- Flatten: The nested structure is expanded.
Scala> list (list (1, 2), List (3, 4= List (1, 2, 3, 4)
- FlatMap: A common combinator that combines the functions of map and flatten. Flatmap receives a function that can handle nested lists and then joins the returned results.
scala> val nestednumbers = list (list (1, 2), List (3, 4= List (list (1, 2), List (3, 4)) Scala> Nestedn Umbers.flatmap (x = X.map (_ * 2= List (2, 4, 6, 8)
All of the function combinations shown above can handle the map as a list of key-value pairs, so that the function you write can handle the key and value in the map. You can use a pattern match to gracefully get key and value.
Scala> Extensions.filter ({ case (name, extension)= Extension < $ = Map ((steve,100), (Bob, 101))
Scala Advanced: Function combination (combinator)