Scala函数
padTo
定义
:def padTo(len: Int, elem: A): Array[A]
val a=Array(1,2,3,4)
val b=a.padTo(8,0)//修改数组长度为8,不足补0
println(b.mkString(","))//1,2,3,4,0,0,0,
par
定义:def par: ParArray[T]
val a=Array(1,2,3,4)
val b=a.par//并行产生一个序列,同b++=a
println(b.mkString(","))//1,2,3,4
ArrayBuffer
val a=Array(1,2,3,4,5,6,7,8)
val d=new ArrayBuffer[Int]()
d ++=a
partition
定义:def partition(p: (T) ⇒ Boolean): (Array[T], Array[T])
val a=Array(1,2,3,4,5,6,7,8)
val b=a.partition(x=>x>4)//符合条件在第一个数组
println(b._1.mkString("-"),b._2.mkString("-"))//(5-6-7-8,1-2-3-4)
patch
定义:def patch(from: Int, that: GenSeq[A], replaced: Int): Array[A]
val a =Array(1,2,3,4)
val b=Array(11,22,33)
val c=a.patch(1,b,2)//从下边为1开始,往后两位被数组b取代
println(c.mkString(","))//1,11,22,33,4
permutations
定义:def permutations: collection.Iterator[Array[T]]
val a=Array('a','b','c','d')
val b=a.permutations.toList//将数组排列组合去重
b.foreach(x=>
println
(x.mkString(
","
)+""))
a,b,c,d a,b,d,c a,c,b,d a,c,d,b a,d,b,c a,d,c,b b,a,c,d b,a,d,c b,c,a,d b,c,d,a b,d,a,c b,d,c,a c,a,b,d c,a,d,b c,b,a,d c,b,d,a c,d,a,b c,d,b,a d,a,b,c d,a,c,b d,b,a,c d,b,c,a d,c,a,b d,c,b,a
prefixLength
定义:def prefixLength(p: (T) ⇒ Boolean): Int
val a=Array(1,2,3,4)
val b=a.prefixLength(x=>x<3)//返回一个前置数列的长度,这个数列中的元素都满足p,条件3前面的数列长度,如果没有返回0
println(b)//2
product
定义:def product: A
描述:返回所有元素乘积的值
val a=Array(1,2,3)
val b=a.product
println(b)
reduce
定义:def reduce[A1 >: A](op: (A1, A1) ⇒ A1): A1
描述:同 fold,不需要初始值
val a = Array(1, 2, 3, 4)
val b = a.reduce((x,y)=>x+y)
println(b)//10
def seqno(m: Int, n: Int): Int = {
val s = "seq_exp = %d + %d"
println(s.format(m, n))
m + n
}
def main(args: Array[String]) {
val a = Array(1, 2, 3, 4)
val b = a.reduce(seqno)
println("b = " + b)
/**
* seq_exp = 1 + 2
* seq_exp = 3 + 3
* seq_exp = 6 + 4
* b = 10
*/
}
reduceLeft(同reduce)
定义:def reduceLeft[B >: A](op: (B, T) ⇒ B): B
描述:同 foldLeft,从左向右计算,不需要初始值
reduceRight
定义:def reduceRight[B >: A](op: (B, T) ⇒ B): B
描述:同 foldRight,从右向左计算,不需要初始值
val a = Array(1, 2, 3, 4)
val b = a.reduce((x,y)=>x+y)
println(b)//10
seq_exp = 3 + 4 seq_exp = 2 + 7 seq_exp = 1 + 9
b = 10
reduceLeftOption
定义:def reduceLeftOption[B >: A](op: (B, T) ⇒ B): Option[B]
描述:同 reduceLeft,返回 Option
seq_exp = 1 + 2
seq_exp = 3 + 3
seq_exp = 6 + 4
b=some(10)
reduceRightOption
定义:def reduceRightOption[B >: A](op: (T, B) ⇒ B): Option[B]
描述:同 reduceRight,返回 Option
seq_exp = 3 + 4 seq_exp = 2 + 7 seq_exp = 1 + 9
b=some(10)
reverse
定义:def reverse: Array[T]
描述:反转序列
val a
=
Array
(
1
,
2
,
3
,
4
)
val b
=
a
.
reverse
b.foreach(x => print(x + " ")) // 4 3 2 1
reverseIterator
定义:def reverseIterator: collection.Iterator[T]
描述:生成反向迭代器,只可遍历一次
val a
=
Array
(
1
,
2
,
3
,
4
)
val b
=
a
.
reverseIterator //只可遍历一次
b
.
foreach
(
x
=
>
print
(
x
+
" "
)
)
// 4 3 2 1
reverseMap
定义:def reverseMap[B](f: (A) ⇒ B): Array[B]
描述:同 map,方向相反
val a = Array(1, 2, 3, 4)
a.reverseMap(x=>print(x+" "))//
4 3 2 1
目前只整理了一小部分,后面会有更多,大家见谅啊
++ count hasDefiniteSize maxBy sameElements toBuffer
deep head min scan toIndexedSeq
+: diff headOption minBy scanLeft toIterable
/: distinct indexOf mkString scanRight toIterator
:+ drop indexOfSlice nonEmpty segmentLength toList
:\ dropRight indexWhere orElse seq toMap
addString dropWhile indices padTo size toSeq
aggregate elemManifest init par slice toSet
andThen elemTag inits partition sliding toStream
apply endsWith intersect patch sortBy toTraversable
applyOrElse exists isDefinedAt permutations sortWith toVector
array filter isEmpty prefixLength sorted transform
canEqual filterNot isTraversableAgain product span transpose
clone find iterator reduce splitAt union
collect flatMap last reduceLeft startsWith unzip
collectFirst flatten lastIndexOf reduceLeftOption stringPrefix unzip3
combinations fold lastIndexOfSlice reduceOption sum update
companion foldLeft lastIndexWhere reduceRight tail updated
compose foldRight lastOption reduceRightOption tails view
contains forall length repr take withFilter
containsSlice foreach lengthCompare reverse takeRight zip
copyToArray genericBuilder lift reverseIterator takeWhile zipAll
copyToBuffer groupBy map reverseMap to zipWithIndex
corresponds grouped max runWith toArray