grouped: 将collection按照指定的size组合成多个Vector,返回这个List的iterator。 // groupedprintln("--- grouped ---")vallistgroup = (1to10)vallistgroupIterator = listgroup.grouped(3) println("grouped: "+ listgroup +" with size 3 to: "+ listgroupIterator) listgroupIterator foreach println 输出...
Vector vs List vs Stream Vector, List, Stream都是immutable。 Vector: 对于随机访问性能最好。推荐使用。 List: 对于head/tail的访问性能最好。 Stream: lazy估值,主要用于无限数列(infinite sequences)。 代码语言:javascript 代码运行次数:0 运行 AI代码解释 // Stream println("--- Stream ---") val fib...
res0: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3) 默认实现 使用特质的apply方法会给你默认实现的实例,例如,Iterable(1, 2)会返回一个列表作为其默认实现。 scala> Iterable(1, 2) res0: Iterable[Int] = List(1, 2) 序列Seq也是一样的,正如我们前面所看到的 scala> Seq(1, 2)...
|-String |-Vector ==|-List(列表)== |-Queue(FIFO) |-Stack(FIEO)25.2 List的操作//创建List val ages1 = List(12, 14, 16); val ages2 = List(10, 11, 19); //把两个集合合并在一起,从冒号的一边开始计算 val ages3 = ages1 ::: (ages2);...
res0: scala.collection.immutable.IndexedSeq[Int] = Vector( 1 , 2 , 3 ) 1. 2. 3. 4. 默认实现 在trait上使用apply方法会得到该trait的一个默认实现,例如,Iterable(1,2)返回一个List作为它的默认实现。 scala> Iterable( 1 , 2 ) res0: Iterable[Int] = List( 1 , 2 ) ...
scala> for(i<-1to10)yieldprintln(i%3)1201201201res47:scala.collection.immutable.IndexedSeq[Unit]=Vector((),(),(),(),(),(),(),(),(),())scala>val ret1 = for(i<-1to10)yield(i)ret1:scala.collection.immutable.IndexedSeq[Int]=Vector(1,2,3,4,5,6,7,8,9,10)scala>for(i <...
scala> for { | i <- 1 to 9 | j <- i to 9 | } yield i * j res0: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 6, 8, 10, 12, 14, 16, 18, 9, 12, 15, 18, 21, 24, 27, 16, 20, 24, 28, 32, 36, 25, 30, 35...
一个技巧是使用Tab补全来查看一个对象上的可用方法。我们可以通过输入1,然后输入一个小数点,接着再按<Tab>键来看看Tab补全是如何工作的。REPL的响应是列出Int实例上的几十个可用方法:你也可以通过输入方法名的开头部分,然后按<Tab>键来限制所显示的方法列表。比如,如果你想查看List上的所有方法,只需要输入List(1...
Rationale: List.size is O(n) so for performance reasons if .size is needed on a list that could be large, consider using an alternative with O(1), eg Array, Vector or ListBuffer. Redundant finalizer Checks for empty finalizers. This is redundant code and should be removed. Eg, override...
empty[Int] :+ 1 :+ 2 :+ 3 :+ 4 :+ 5 val revList3: Vector[Int] = Vector(1, 2, 3, 4, 5) // how to get a VectorPattern Matching on Repeated Parameters Speaking of repeated parameter lists, you can also use them in pattern matching: // src/script/scala/progscala3/pattern...