Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3598388
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:14:10+00:00 2026-05-18T20:14:10+00:00

I’m working my way through Programming in Scala , and though I’m tempted to

  • 0

I’m working my way through Programming in Scala, and though I’m tempted to look at things from the perspective of Python, I don’t want to program “Python in Scala.”

I’m not quite sure what to do as far as control flow goes: in Python, we use for x in some_iterable to death, and we love it. A very similar construct exists in Scala which Odersky calls a for expression, probably to distinguish it from the Java for loop. Also, Scala has a foreach attribute (I guess it would be an attribute, I don’t know enough about Scala to name it correctly) for iterable data types. It doesn’t seem like I can use foreach to do much more than call one function for each item in the container, though.

This leaves me with a few questions. First, are for expressions important/heavily used constructs in Scala like they are in Python, and second, when should I use foreach instead of a for expression (other than the obvious case of calling a function on each item of a container)?

I hope I’m not being terribly ambiguous or overbroad, but I’m just trying to grok some of the design/language fundamentals in Scala (which seems very cool so far).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:14:10+00:00Added an answer on May 18, 2026 at 8:14 pm

    python uses for in list comprehensions and generator expressions. Those are very similar to the scala for expression:

    this is python

    >>> letters = ['a', 'b', 'c', 'd']
    >>> ints = [0, 1, 2, 3]
    >>> [l + str(i) for l in letters for i in ints if i % 2 == 0]
    ['a0', 'a2', 'b0', 'b2', 'c0', 'c2', 'd0', 'd2']
    

    this is scala

    scala> val letters = List('a', 'b', 'c', 'd')
    scala> val ints = List(0, 1, 2, 3)
    scala> for (l <- letters; i <- ints if i % 2 == 0) yield l.toString + i
    res0: List[java.lang.String] = List(a0, a2, b0, b2, c0, c2, d0, d2)
    

    Each construct can take a number of generators/iterators, apply filters expressions and yield a combined expression. In python the (expr for v1 in gen1 if expr1 for v2 in gen2 if expr2) is roughly equivalent to:

    for v1 in gen1:
      if expr1:
        for v2 in gen2:
          if expr2:
            yield expr
    

    In scala for (v1 <- gen1 if expr1; v2 <- gen2 if expr2) yield expr is roughly equivalent to:

    gen1.withFilter(expr1).flatMap(v1 => gen2.withFilter(expr2).map(v2 => expr))
    

    If you love the python for x in xs syntax, you’ll likely love the scala for expression.

    Scala has some additional syntax and translation twists. Syntax wise for can be used with braces so that you can put statements on separate lines. You can also perform value assignments.

    val res = for {
        i <- 1 to 20; i2 = i*i
        j <- 1 to 20; j2 = j*j
        k <- 1 to 20; k2 = k*k
        if i2 + j2 == k2
      } yield (i, j, k)
    

    Also v1 <- gen1 really performs a match case v1 => gen1. If there is no match those elements are ignored from the iteration.

    scala> val list = List(Some(1), None, Some(2))
    scala> for (Some(i) <- list) yield i
    res2: List[Int] = List(1, 2)
    

    I think for has an important place in the language. I can tell from the fact there is a whole chapter (23) about it in the book you’re reading!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.