IT虾米网

Scala Range与Stream

developer 2022年06月17日 编程语言 263 0

一 Range

scala>1 to 10
res20: scala.collection.immutable.Range.Inclusive=Range1 to 10

scala>1 to 10 by 2
res21: scala.collection.immutable.Range= inexact Range1 to 10 by 2

scala>(1 to 10).toList
res22:List[Int]=List(1,2,3,4,5,6,7,8,9,10)

scala>1 until 10
res23: scala.collection.immutable.Range=Range1 until 10
二 Stream  
scala>1#::2 #::3 #::Stream.empty
res24: scala.collection.immutable.Stream[Int]=Stream(1,?)

scala> val stream =(1 to 100000).toStream
stream: scala.collection.immutable.Stream[Int]=Stream(1,?)

scala> stream.head
res25:Int=1

scala> stream.tail
res26: scala.collection.immutable.Stream[Int]=Stream(2,?)


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Scala基础语法之变量