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 8520383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:31:14+00:00 2026-06-11T06:31:14+00:00

I am currently learning scala. I am reading Scala for the impatient. 1.) Guards

  • 0

I am currently learning scala. I am reading Scala for the impatient.

1.)

Guards

Is there a difference?

for (i <- 0 to 10)if (i % 2 == 0) println(i)

for (i <- 0 to 10 if i % 2 == 0) println(i)

2.)

I always see the following symbol => but they never explain what it does.

Sometimes I think it is a cast but then it is something completely different, I hope you can clear things up.

  • 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-06-11T06:31:16+00:00Added an answer on June 11, 2026 at 6:31 am

    1.) Yes, there is a difference, the first if a normal if statement inside of the closure you pass to the for-comprehension. The second is an actual guard. It will actually call withFilter on the range, before calling foreach. So the translation of the two things will look like this:

    0.to(10).foreach(i => if(i % 2 == 0) println(i) )
    0.to(10).withFilter(x => x % 2 == 0).foreach(i => println(i))
    

    To add a little more context, calling withFilter or even just filter instead of using a normal if statement has some benefits. In a for comprehension, you can have nested calls to map, flatmap, filter, collect etc. so if you add guards, you can prevent a lot af calls from actually happening. For example:

    for {
      x <- 0 until 10
      y <- 10 until 20
    } {
      if(x % 2 == 0) println(x*y)
    }
    

    would call the actual closure 100 times

    for {
      x <- 0 until 10
      if x % 2 == 0
      y <- 10 until 20
    } println(x*y)
    

    this will only call it 50 times, while the result stays the same.

    2.)

    => separates the argument list of a function/closure from the body.

    This case e: NumberFormatException => None is a part of a partial function. Here the => separates the “argument” e from the body None.

    In a type signature like in someFunction(i: (A) => Int) it implies that i is of the type Function1[A,Int], read “function from A to Int“.

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

Sidebar

Related Questions

I had some experience in Haskell and currently learning Scala. Am wondering whether there
Learning Scala currently and needed to invert a Map to do some inverted value->key
I'm currently learning about actors in Scala. The book recommends using the react method
I don't have much experience with statically typed languages (currently learning Scala and loving
I'm currently learning Scala, and just discovered the way to create custom field getters/setters.
I'm currently learning Scala by working through the Programming in Scala book. So far,
I'm currently working on implementing my own Trie in Scala (for learning/hobby purposes), and
Currently learning Linq to Entity. I been successful, but came stumped with the orderby
I'm currently learning functional programming in my spare time with Scala, and I have
I am currently working on a logging mechanism to be used with Scala, but

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.