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 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

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In terms of your first question, I struggled with this… May 16, 2026 at 4:32 pm
  • Editorial Team
    Editorial Team added an answer I think you want a ServletContextListener. Before that was available,… May 16, 2026 at 4:32 pm
  • Editorial Team
    Editorial Team added an answer The best way, of course, would be to not store… May 16, 2026 at 4:32 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I was given task to generate random 80 byte keys and i have decided
The task is this: generate k distinct positive numbers less than n without duplication.
Given an array of strings, array1 = [abcdwillbegoneabcccc,cdefwilbegokkkabcdc] and another array of strings which
At work I've been given the fun task of generating PDFs with XSL. The
I am given task to write a script (or better yet, a daemon), that
I am a junior software engineer who've been given a task to take over
My task is to find the actual Press release links of a given link.
I've been given the following task: An external source will write a name, datetime
I'm trying to find the positions of all occurrences of a string in another
This one has me puzzled. It seemed like an easy task, but the solution

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.