标签:int def dem amd val scala ble array equal
一、流
动态产生无穷多的数据,末尾元素遵循lazy规则
object StreamDemo { //生成流的方法 def numStream(n: BigInt): Stream[BigInt] = n #:: numStream(n + 1) def main(args: Array[String]): Unit = { val stream = numStream(1) //初始只有一个元素 println(stream) //Stream(1, ?) //获取第一个元素 println(s"head = ${stream.head}") //head = 1 //获取尾部 println(s"tail = ${stream.tail}") //tail = Stream(2, ?) //调用tail方法之后,动态产生了元素2 println(stream) //Stream(1, 2, ?) //为什么不是Stream(2, 4, ?) println(stream.map(_ * 2)) //Stream(2, ?) } }
二、视图
产生一个懒执行的集合
def main(args: Array[String]): Unit = { val seq: immutable.Seq[Int] = (1 to 100).filter(n => n.toString.equals(n.toString.reverse)) println(seq) //Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99) //调用结果view之前,并不会去执行filter等操作 val view: SeqView[Int, immutable.IndexedSeq[Int]] = (1 to 100).view.filter(n => n.toString.equals(n.toString.reverse)) println(view) //SeqViewF(...) }
标签:int def dem amd val scala ble array equal
原文地址:https://www.cnblogs.com/noyouth/p/12759608.html