Scala's classic 82nd: How does the listbuffer of the list in Scala implement efficient traversal calculations?

Source: Internet
Author: User

Package Com.dt.scala.list


Object Listbuffer_internals {


def main (args:array[string]) {

Val list = list (1,2,3,4,5,6,7,8,9)

Increment (list)

Increment_moreeffective (list)

Increment_mosteffective (list)

}

def increment (List:list[int]): list[int] = list match {

Case list () = List ()

Case HEAD:: tail = head + 1:: Increment (tail)

}

def increment_moreeffective (List:list[int]): list[int] = {

var result = List[int] ()

for (element <-list) result = Result::: List (element + 1)

Result

}

def increment_mosteffective (List:list[int]): list[int] = {

Import Scala.collection.mutable.ListBuffer

var buffer = new Listbuffer[int]

for (element <-list) Buffer + = element + 1

Buffer.tolist

}


}


3 ways:

1. Stacks that generate too many function calls
2. Many intermediate results have been produced
3. Optimal


Information from DT Big Data Dream Factory, public number: Dt_spark
Video address:http://edu.51cto.com/lesson/id-71305.html

Scala's classic 82nd: How does the listbuffer of the list in Scala implement efficient traversal calculations?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.