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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:13:14+00:00 2026-05-28T06:13:14+00:00

for (i <- Range(1,7); j <- Range(1,7)) yield (i,j) // want to yield only

  • 0
for (i <- Range(1,7); j <- Range(1,7))
  yield (i,j) // want to yield only if there is no such a pair

Is it possible to have access to the list, formed by yield, inside the loop?
For example, if I don’t want to add duplicates.

P.S. The main question is not how to do it in this particular case. But how to avoid duplicates in more complex case, where I want to check, what was already yielded.

  • 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-28T06:13:15+00:00Added an answer on May 28, 2026 at 6:13 am

    You cannot access members of an immutable collection being built from within the for comprehension.

    First of all, I’m going to modify your example so it actually produces duplicates:

    for (i <- Range(1,3); j <- Range(1,3)) yield (i min j, i max j)
    //scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((1,1), (1,2), (1,2), (2,2))
    

    For comprehensions are just syntactic sugar, so here’s the equivalent using map and flatMap that produces the exact same result

    Range(1,3).flatMap{i => Range(1,3).map{ j => (i min j, i max j)}}
    

    As you can see, you’re not actually building a single collection, but rather a collection of collections, and then merging them together as they’re created. The inner map is taking each j in the range and mapping it to a pair, then the outer flatMap is mapping each i to a sequence of pairs and merging them together. If I change the flatMap to just map, the result is this:

    Range(1,3).map{i => Range(1,3).map{ j => (i min j, i max j)}}
    //Vector(Vector((1,1), (1,2)), Vector((1,2), (2,2)))
    

    So only after the whole operation is finished can you access the result as a single collection. The result is a Vector which extends IndexedSeq[(Int, Int)], so you can use any of that trait’s methods on the result, one of them is distinct:

    (for (i <- Range(1,3); j <- Range(1,3)) yield (i min j, i max j)).distinct
    //Vector((1,1), (1,2), (2,2))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a Range object--for example, let's say it refers to cell A1
Let's say I have a function like this (it's only an example, so do
I have a function that returns a list via yield. I use this function
I know the range name of the start of a list - 1 column
I have a range of random numbers. The range is actually determined by the
So I have two ruby Date objects, and I want to iterate them every
Is filter/map equivalent to list comprehension? Suppose I have the following function def fib_gen():
I have multiple fieldset. And have Button inside each fieldset in Extjs 4. I
Is there a one-liner to take a generator and yield all elements in this
I have two def functions that I call in a nested fashion and want

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.