Thread overview | ||||||
---|---|---|---|---|---|---|
|
September 05, 2016 Removing array element in foreach, safe? | ||||
---|---|---|---|---|
| ||||
is this code safe? if not how do i do it correctly? static AsyncHttpGet[] openRequests; static void updateRequests() { foreach(idx, req; openRequests) { if(req.state != Fiber.State.TERM) req.call(); else openRequests.remove(idx); } } thx :) |
September 05, 2016 Re: Removing array element in foreach, safe? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dom | On Monday, 5 September 2016 at 15:53:39 UTC, dom wrote: > is this code safe? if not how do i do it correctly? > > static AsyncHttpGet[] openRequests; > static void updateRequests() > { > foreach(idx, req; openRequests) > { > if(req.state != Fiber.State.TERM) > req.call(); > else > openRequests.remove(idx); > } > } > > thx :) openRequests = openRequests.filter!(a=>a.state != Fiber.State.TERM).array; openRequests.each!((a){ a.call(); }); |
September 05, 2016 Re: Removing array element in foreach, safe? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On Monday, 5 September 2016 at 17:38:10 UTC, Daniel Kozak wrote: > On Monday, 5 September 2016 at 15:53:39 UTC, dom wrote: >> is this code safe? if not how do i do it correctly? >> >> static AsyncHttpGet[] openRequests; >> static void updateRequests() >> { >> foreach(idx, req; openRequests) >> { >> if(req.state != Fiber.State.TERM) >> req.call(); >> else >> openRequests.remove(idx); >> } >> } >> >> thx :) > > > openRequests = openRequests.filter!(a=>a.state != Fiber.State.TERM).array; > openRequests.each!((a){ a.call(); }); or openRequests = openRequests.filter!(a=>a.state != 0).map!((a) {a.call(); return a;}).array; |
September 05, 2016 Re: Removing array element in foreach, safe? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On Monday, 5 September 2016 at 17:38:10 UTC, Daniel Kozak wrote:
> On Monday, 5 September 2016 at 15:53:39 UTC, dom wrote:
>> is this code safe? if not how do i do it correctly?
>>
>> static AsyncHttpGet[] openRequests;
>> static void updateRequests()
>> {
>> foreach(idx, req; openRequests)
>> {
>> if(req.state != Fiber.State.TERM)
>> req.call();
>> else
>> openRequests.remove(idx);
>> }
>> }
>>
>> thx :)
>
>
> openRequests = openRequests.filter!(a=>a.state != Fiber.State.TERM).array;
> openRequests.each!((a){ a.call(); });
thx mate!
|
Copyright © 1999-2021 by the D Language Foundation