6

可能重复:
在 Scala 文档中搜索 #::

我正在浏览Stream的文档

过滤器方法有这样的代码:

def naturalsFrom(i: Int): Stream[Int] = i #:: naturalsFrom(i + 1)
naturalsFrom(1)  10 } filter { _ % 5 == 0 } take 10 mkString(", ")

#:: 运算符是什么?这是否映射到某处的函数调用?

4

2 回答 2

13

正如 SHildebrandt 所说,#:: 是 Streams 的 cons 运算符。

换句话说,#:: 是流式传输, :: 是列表

val x = Stream(1,2,3,4)                   //> x  : scala.collection.immutable.Stream[Int] = Stream(1, ?)
10#::x                                    //> res0: scala.collection.immutable.Stream[Int] = Stream(10, ?)

val y = List(1,2,3,4)                     //> y  : List[Int] = List(1, 2, 3, 4)
10::y                                     //> res1: List[Int] = List(10, 1, 2, 3, 4)
于 2012-11-23T19:19:37.100 回答
11
x #:: xs

返回

Stream.cons(x, xs)

它返回一个元素 x 的 Stream,后跟一个 Stream xs。

于 2012-11-23T17:42:37.293 回答