【问题标题】:What is the #:: operator in a scala Stream? [duplicate]scala Stream中的#::运算符是什么? [复制]
【发布时间】:2012-11-24 03:19:37
【问题描述】:

可能重复:
Searching the Scala documentation for #::

我正在查看Stream的文档

过滤方法有这样的代码:

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

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

【问题讨论】:

    标签: scala


    【解决方案1】:

    正如 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)
    

    【讨论】:

      【解决方案2】:
      x #:: xs
      

      返回

      Stream.cons(x, xs)
      

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

      【讨论】:

        猜你喜欢
        • 2014-05-09
        • 2012-07-15
        • 2021-02-13
        • 1970-01-01
        • 2014-01-15
        • 2021-11-26
        • 2016-08-28
        • 2012-12-29
        • 2012-05-10
        相关资源
        最近更新 更多