简体   繁体   中英

what does the operator “:\” do in this scala code?

I just came across this on the scalacheck documentation and google was not very helpful in trying to lookup what the ":\\" operator does:

abstract sealed class Tree[T] {
  def merge(t: Tree[T]) = Internal(List(this, t))

  def size: Int = this match {
    case Leaf(_) => 1
    case Internal(children) => (children :\ 0) (_.size + _)
  }
}

case class Internal[T](children: Seq[Tree[T]]) extends Tree[T]

case class Leaf[T](elem: T) extends Tree[T]

Also does anyone have any tips as to how to search for such funky symbols other than trawling through the nebulous scaladoc?

Thanks

It's a foldRight.

(children :\ 0) (_.size + _)

is equivalent to

children.foldRight(0)(_.size + _)

and is also equivalent to

children.foldRight(0)((a,b) => a.size + b)

If you check the library code for Seq[T], the ':\\' method is defined as:

def :\[B](z: B)(op: (A, B) => B): B = foldRight(z)(op)

:\\ is alternate syntax for foldRight .

For searching for funky symbols you could use Scalex or sbt-man .

It's alternate syntax for foldRight .

scala-lang.org documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM