- Receiving a membership function
map
Conversion elements, is mainly used in the set of immutable(1 to 10).map(i => i * i) (1 to 10).flatMap(i => (1 to i).map(j => i * j))
transform
Themap
same, except for a variable set of direct conversionArrayBuffer("Peter", "Paul", "Mary").transform(_.toUpperCase)
collect
Receiving partial function (PartialFunction
) as a parameter; pattern matching is also a partial function"-3+4".collect { case '+' => 1 ; case '-' => -1 } // Vector(-1, 1)
groupBy
Packet specified function, returnsMap
val words = Array("Abc", "ab") val map = words.groupBy(_.substring(0, 1).toUpperCase) // Map(A -> Array(Abc, ab))
- Receiving binary function
reduceLeft
From left to right Statutef(f(f(a, b), c), d)
List(1, 7, 2, 9).reduceLeft(_ - _) // ((1 - 7) - 2) - 9 = 1 - 7 - 2 - 9 = -17
reduceRight
Statute from right to leftf(a, f(b, f(c, d)))
List(1, 7, 2, 9).reduceRight(_ - _) // 1 - (7 - (2 - 9)) = 1 - 7 + 2 - 9 = -13
foldLeft
Providing an initial value + binary function, the folded left to right, on the left each time the calculation results- Available
/:
(a left side tree) represented operator,(init /: collection)(function)
- Available
foldRight
Providing an initial value + binary function, folded from right to left, each calculation result in the right- Available
:\
(a right side tree) represented operator,(collection :\ init)(function)
List(1, 7, 2, 9).foldLeft(0)(_ - _) (0 /: List(1, 7, 2, 9))(_ - _) // 0 - 1 - 7 - 2 - 9 = -19
- Available
scanLeft
AndscanRight
a combination of folding and mapping, as a result of all intermediate process values(1 to 10).scanLeft(0)(_ + _) // Vector(0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55)
zip
Zipper, i.e. each set of two cross-elements joined together like a zipperList(1,2,3) zip List("a","b","c") // List((1,a), (2,b), (3,c))
- Places inconsistent set length smaller length prevail
zipAll
Shorter length sets the default value,this.zipAll(that, thisDefault, thatDefault)
zipWithIndex
And returns the element corresponding to subscript"Scala".zipWithIndex // Vector((S,0), (c,1), (a,2), (l,3), (a,4))
view
Create a view as a set delay
scala val lazyView = (1 to 1000000).view lazyView.take(100).last //100
- Manipulation of view will not be immediately calculated (not including the first element)
- And
Stream
different, will not cache any value apply
The method of calculating the entire view forces, usinglazyView.take(i).last
instead oflazyView(i)
par
Parallel sets with subsequent application methods will concurrent computation
scala for (i <- (0 until 100).par) print(s" $i") // 1-99
- A good solution to the problem of concurrent programming
- The set of changes to the implementation of parallel
- For the result, a serial manner consistent with (e.g.
for...yield...
) - It may be used
seq
,toArray
or the like to restore the set - The method is not part of concurrent operations
- Use
reduce
AlternativelyreduceLeft
, the first set of operations for each portions, and the polymerization results, but the operation must be met associativity - Use
aggregate
AlternativelyfoldLeft
, the first set of operations on each part, and operation results of the polymerization with another
str.par.aggregate(Set[Char]())(_ + _, _ ++ _) // 等价于 str.foldLeft(Set[Char]())(_ + _)
- Use
Scala Collection Method
Guess you like
Origin www.cnblogs.com/yuanzam/p/11617032.html
Recommended
Ranking