What good are right-associative methods in Scala?

前端 未结 3 2047
有刺的猬
有刺的猬 2020-11-27 16:55

I\'ve just started playing around with Scala, and I just learned about how methods can be made right-associative (as opposed to the more traditional left-associ

3条回答
  •  生来不讨喜
    2020-11-27 17:00

    Right-associativity and Left-associativity plays an important role when you are performing List fold operations. For eg:

    def concat[T](xs: List[T], ys: List[T]): List[T] = (xs foldRight ys)(_::_)
    

    This works perfectly fine. But you cannot perform the same using foldLeft operation

    def concat[T](xs: List[T], ys: List[T]): List[T] = (xs foldLeft ys)(_::_)
    

    ,because :: is right-associative.

提交回复
热议问题