How does I iterate over a Scala List (or more generally, a sequence) using the Theforeach method or for loop?

Source: Internet
Author: User

Scala list/sequence faq:how do I iterate over a Scala List (or more generally, a sequence) using the foreach method or for loop?

There is a number of ways to iterate over a Scala List using the foreach method (which was available to Scala sequences like ,,,, List Array ArrayBuffer Vector Seq , etc.) for and comprehension, and I ll show a few of those approaches here.

1) iterating lists with foreach

A common-to-iterate over a Scala List was with the foreach method. Here's a quote about from the book foreach Programming in Scala:

foreach takes a procedure-a function with a result type – as the right Unit operand. It simply applies the procedure to each List element. The result of the operation is again Unit ; no list of results is assembled.

Here's a simple example showing what to use to foreach print every item in a List :

val x = List ( all in all) X.foreach {println}123

If you've used a programming language like Ruby, this syntax'll look familiar to you.

Note that this is a relatively common the foreach method. Because foreach takes a procedure that doesn ' t return anything, and Because the result foreach Unit of is also, the foreach meth OD is typically used for it side effects--something like this example where output are printed for a user to see.

This next example shows a-to-sum all of the elements in a list using foreach :

var sum = 0 val x = List ( all in all) X.foreach (sum + = _) println (sum)6

Note that this second example are not a common or preferred-to-use foreach ; I ' m just trying to show some different possibilities. (when I first wrote this example it wasn ' t the worst thing in the world to use a var field, but with more and more devel Opers preferrring Functional programming, the use of are var discouraged.)

2) Scala Lists and the for comprehension

The Scala for comprehension isn't specific to lists, it's an extremely powerful a-to-operate on a and List Other sequences. Here's a simple example by iterate over a sequence using the for comprehension (also known as a " for loop"):

val names = Vector ("Bob", "Fred", "Joe", "Julia", "Kim") For (name <-names) println (name) Bobfredjoejuliakim

So far, so good. Now let's add a simple clause to the comprehension to print only the if for elements we want to print:

val names = Vector ("Bob", "Fred", "Joe", "Julia", "Kim") For (name <-names if Name.startswith ("J"))     println (name) Joejulia

If you already know on for the comprehension, you know so can add multiple clauses if , and much more functio Nality. I could easily write a entire tutorial on the Scala for comprehension, so-keep this tutorial short, I'll stop here F or now.

Before leaving, I'll add these notes however, from the book Programming in Scala:

Scala provides the for comprehension, which provides syntactically pleasing nesting of map , flatMap , and filter ... The for comprehension is not a looping construct, but was a syntactic construct the compiler reduces map to, c11/>, and filter .

3) More detailed examples

I apologize that these examples is not as detailed as I prefer. If I had more free time I ' d expand on them here, but sadly I don ' t has that free time right now. So I'll just has to say, "Please see the Scala Cookbook, where I cover the For loop and foreach method in great detail":

4) summary:iterating Scala lists with foreach and for

I hope this short tutorial on what to iterate over a Scala List (and other sequences) using the foreach method and for com Prehension has been helpful. As can tell from these examples, there's much more power available to you with both approaches, which is one of the GR Eat things about the Scala programming language.

How does I iterate over a Scala List (or more generally, a sequence) using the Theforeach method or for loop?

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.