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

  • Home
  • SEARCH
  • 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 901573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:34:36+00:00 2026-05-15T15:34:36+00:00

So I’m learning Scala and came across some examples like this: val doubleEven =

  • 0

So I’m learning Scala and came across some examples like this:

val doubleEven = for (i <- 1 to 10; if i % 2 == 0)
  yield i * 2

Now, what’s the added benefit to having this special syntax built into the for loop as opposed to the time-honored

val doubleEven = for(i <- 1 to 10){
  if(i % 2 ==  0)
    yield i*2
}

style if?

EDIT: Of course, the latter example won’t actually work. But I was curious why the Scala folks decided to go with a separate syntax.

  • 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-15T15:34:37+00:00Added an answer on May 15, 2026 at 3:34 pm

    Contents of file A.scala:

    object A {
      val doubleEven1 = for (i <- 1 to 10; if i % 2 == 0) yield i * 2
    }
    

    Output of scalac -Xprint:jvm A.scala:

    [[syntax trees at end of jvm]]// Scala source: A.scala
    package <empty> {
      final class A extends java.lang.Object with ScalaObject {
        private[this] val doubleEven1: scala.collection.immutable.IndexedSeq = _;
        <stable> <accessor> def doubleEven1(): scala.collection.immutable.IndexedSeq = A.this.doubleEven1;
        def this(): object A = {
          A.super.this();
          A.this.doubleEven1 = scala.this.Predef.intWrapper(1).to(10).withFilter({
            (new A$$anonfun$1(): Function1)
          }).map({
            (new A$$anonfun$2(): Function1)
          }, immutable.this.IndexedSeq.canBuildFrom()).$asInstanceOf[scala.collection.immutable.IndexedSeq]();
          ()
        }
      };
      @SerialVersionUID(0) @serializable final <synthetic> class A$$anonfun$1 extends scala.runtime.AbstractFunction1$mcZI$sp {
        final def apply(i: Int): Boolean = A$$anonfun$1.this.apply$mcZI$sp(i);
        <specialized> def apply$mcZI$sp(v1: Int): Boolean = v1.%(2).==(0);
        final <bridge> def apply(v1: java.lang.Object): java.lang.Object = scala.Boolean.box(A$$anonfun$1.this.apply(scala.Int.unbox(v1)));
        def this(): A$$anonfun$1 = {
          A$$anonfun$1.super.this();
          ()
        }
      };
      @SerialVersionUID(0) @serializable final <synthetic> class A$$anonfun$2 extends scala.runtime.AbstractFunction1$mcII$sp {
        final def apply(i: Int): Int = A$$anonfun$2.this.apply$mcII$sp(i);
        <specialized> def apply$mcII$sp(v1: Int): Int = v1.*(2);
        final <bridge> def apply(v1: java.lang.Object): java.lang.Object = scala.Int.box(A$$anonfun$2.this.apply(scala.Int.unbox(v1)));
        def this(): A$$anonfun$2 = {
          A$$anonfun$2.super.this();
          ()
        }
      }
    }
    

    As you can see the compiler creates a Range from 1 to 10. Then it calls the withFilter on that Range to filter all even numbers. And the last step is the call of the map-method with the multiplication with 2.

    Would your second example work the semantic would be a little bit different. Because it would execute the body of the loop in every iteration without filtering.

    It is a matter of taste but I would prefer this:

    val doubleEven = 1 to 10 filter(_%2==0) map(_*2)
    
    • 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.