# modifying a Vector while iterating over it

8 messages
Open this post in threaded view
|

## modifying a Vector while iterating over it

 Suppose I am iterating over a Vector:var assigned = Vector[Trajectory]()for (trajectory <- trajectories.sortBy(_.freezeTime)) assigned :+= deconflict(trajectory)Here I am taking a list of aircraft trajectories and deconflicting them one at a time as they reach a time called the "freeze time." It works fine most of the time, but occasionally a conflict-free trajectory is not found. In that case, I wish to simulate a holding pattern (or other maneuver) to delay the flight, then I will try again later to deconflict it. I will simulate the delay with a simple time-shift for now and reinsert it into the list of trajectories (to be deconflicted later). It's freeze time will change. Is there a recommended way to do something like this? If so, what? Thanks. -- You received this message because you are subscribed to the Google Groups "scala-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.
Open this post in threaded view
|

## Re: modifying a Vector while iterating over it

 In case anyone is interested, I think I figured out a good way to handle this problem.I use a while loop rather than a for loop. Here is the simplified pattern:while (trajectories.nonEmpty) { // trajectories is a var with the original conflicted trajectories  val trajectory = trajectories.head  trajectories = trajectories.tail  val trial = deconflict(trajectory)  if (trial.success) assign(trajectory)  else {On Sunday, January 1, 2017 at 2:22:13 PM UTC-8, Russ P. wrote:Suppose I am iterating over a Vector:var assigned = Vector[Trajectory]()for (trajectory <- trajectories.sortBy(_.freezeTime)) assigned :+= deconflict(trajectory)Here I am taking a list of aircraft trajectories and deconflicting them one at a time as they reach a time called the "freeze time." It works fine most of the time, but occasionally a conflict-free trajectory is not found. In that case, I wish to simulate a holding pattern (or other maneuver) to delay the flight, then I will try again later to deconflict it. I will simulate the delay with a simple time-shift for now and reinsert it into the list of trajectories (to be deconflicted later). It's freeze time will change. Is there a recommended way to do something like this? If so, what? Thanks. -- You received this message because you are subscribed to the Google Groups "scala-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.
Open this post in threaded view
|

## Re: modifying a Vector while iterating over it

 Crap, I hit a tab and the darn thing posted! Oh well, you get the idea.On Tue, Jan 3, 2017 at 6:02 PM, Russ P. wrote:In case anyone is interested, I think I figured out a good way to handle this problem.I use a while loop rather than a for loop. Here is the simplified pattern:while (trajectories.nonEmpty) { // trajectories is a var with the original conflicted trajectories  val trajectory = trajectories.head  trajectories = trajectories.tail  val trial = deconflict(trajectory)  if (trial.success) assign(trajectory)  else {On Sunday, January 1, 2017 at 2:22:13 PM UTC-8, Russ P. wrote:Suppose I am iterating over a Vector:var assigned = Vector[Trajectory]()for (trajectory <- trajectories.sortBy(_.freezeTime)) assigned :+= deconflict(trajectory)Here I am taking a list of aircraft trajectories and deconflicting them one at a time as they reach a time called the "freeze time." It works fine most of the time, but occasionally a conflict-free trajectory is not found. In that case, I wish to simulate a holding pattern (or other maneuver) to delay the flight, then I will try again later to deconflict it. I will simulate the delay with a simple time-shift for now and reinsert it into the list of trajectories (to be deconflicted later). It's freeze time will change. Is there a recommended way to do something like this? If so, what? Thanks. -- You received this message because you are subscribed to a topic in the Google Groups "scala-user" group. To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-user/e98lIpgfRAs/unsubscribe. To unsubscribe from this group and all its topics, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. -- http://RussP.us -- You received this message because you are subscribed to the Google Groups "scala-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.
Open this post in threaded view
|

## Re: modifying a Vector while iterating over it

Open this post in threaded view
|

## Re: modifying a Vector while iterating over it

 In reply to this post by Russ P. I have a question about sorting efficiency. Suppose I have a sorted Vector around 2,000 long, and I wish to add an element in sorted order. The simple way is to just append the element and then resort (using trajectories.sortBy(_.freezeTime)). But is that efficient, or is there a better way to do it without "rolling your own"?On Tuesday, January 3, 2017 at 6:02:18 PM UTC-8, Russ P. wrote:In case anyone is interested, I think I figured out a good way to handle this problem.I use a while loop rather than a for loop. Here is the simplified pattern:while (trajectories.nonEmpty) { // trajectories is a var with the original conflicted trajectories  val trajectory = trajectories.head  trajectories = trajectories.tail  val trial = deconflict(trajectory)  if (trial.success) assign(trajectory)  else {On Sunday, January 1, 2017 at 2:22:13 PM UTC-8, Russ P. wrote:Suppose I am iterating over a Vector:var assigned = Vector[Trajectory]()for (trajectory <- trajectories.sortBy(_.freezeTime)) assigned :+= deconflict(trajectory)Here I am taking a list of aircraft trajectories and deconflicting them one at a time as they reach a time called the "freeze time." It works fine most of the time, but occasionally a conflict-free trajectory is not found. In that case, I wish to simulate a holding pattern (or other maneuver) to delay the flight, then I will try again later to deconflict it. I will simulate the delay with a simple time-shift for now and reinsert it into the list of trajectories (to be deconflicted later). It's freeze time will change. Is there a recommended way to do something like this? If so, what? Thanks. -- You received this message because you are subscribed to the Google Groups "scala-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.
Open this post in threaded view
|