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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:24:21+00:00 2026-05-16T12:24:21+00:00

Task: For a given position in 2D array generate list of surrounding positions located

  • 0

Task:
For a given position in 2D array generate list of surrounding positions located in radius.

For example:

input: (1, 1)
radius: 1
output: ( (0, 0), (1, 0), (2, 0), 
          (0, 1),         (2, 1),
          (0, 2), (1, 2), (2, 2) ).

I wrote something like

def getPositions(x:Int, y:Int, r:Int) = {
  for(radius <- 1 to r) yield {
    List(
      for (dx <- -radius to radius) yield Pair(x + dx, y - radius),
      for (dx <- -radius to radius) yield Pair(x + dx, y + radius),
      for (dy <- -radius to radius) yield Pair(x + radius, y + dy),
      for (dy <- -radius to radius) yield Pair(x - radius, y + dy)
    )
  }
}

In this code getPositions returns not a sequance of points, but a sequance of Tuple4 of sequances of points.
How can I “concatenate” 4 generators listed in code? Or is there more concise solution for my task? (I’m pretty new to scala).

P.S.
It’s actually for my starcraft bot.

  • 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-16T12:24:22+00:00Added an answer on May 16, 2026 at 12:24 pm

    You need to flatten the List (twice), so this would do:

    def getPositions(x:Int, y:Int, r:Int) = {
      for(radius <- 1 to r) yield {
        List(
          for (dx <- -radius to radius) yield Pair(x + dx, y - radius),
          for (dx <- -radius to radius) yield Pair(x + dx, y + radius),
          for (dy <- -radius to radius) yield Pair(x + radius, y + dy),
          for (dy <- -radius to radius) yield Pair(x - radius, y + dy)
        ).flatten
      }
    }.flatten
    

    It’s not a ‘lazy’ spiral, though.

    Edit

    That one is lazy:

    def P(i:Int, j:Int) = { print("eval"); Pair(i,j) }
    
    def lazyPositions(x:Int, y:Int, r:Int) = {
      (1 to r).toStream.flatMap{ radius =>
    
        (-radius to radius).toStream.map(dx => P(x + dx, y - radius)) #:::
        (-radius to radius).toStream.map(dx => P(x + dx, y + radius)) #:::
        (-radius to radius).toStream.map(dy => P(x + radius, y + dy)) #:::
        (-radius to radius).toStream.map(dy => P(x - radius, y + dy))
      }
    }
    
    
    print(lazyPositions(1,1,1).take(3).toList) # prints exactly three times ‘eval’.
    

    I’ve used the def P method to show the real laziness. Everytime, you’d create a Pair, it gets called. In a lazy solution, you’d only want this on demand.

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

Sidebar

Related Questions

I was given task to generate random 80 byte keys and i have decided
I am given task to write a script (or better yet, a daemon), that
I've been given the task of coming up to speed, at least at a
I am a junior software engineer who've been given a task to take over
I have a question for a c++ lab assignment I have. The task is
So I have an iPhone app which should aid the user to find a
Some student asked this on another site, but got no answers. I had a
I would like to apply some function on each row of a dataframe in
I'm working on a tasklist program to brush up on my C before I

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.