Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
June 10, 2019 How to walk over two arrays by ref in beautyfull way? | ||||
---|---|---|---|---|
| ||||
How to? I plan scan First array with checkers: [checker, checker, checker] ...and store result to Second array: [0, 0, 1] In next code 'r' not reference. But expected ref. import std.range : zip; foreach(checker, ref r; zip(checkers, result.set)) { r = checker(); } What is the best way for speed and beautyfull D code? |
June 10, 2019 Re: How to walk over two arrays by ref in beautyfull way? | ||||
---|---|---|---|---|
| ||||
Posted in reply to vitalfadeev | On Monday, 10 June 2019 at 07:41:40 UTC, vitalfadeev wrote:
> How to?
>
> I plan scan First array with checkers:
> [checker, checker, checker]
>
> ...and store result to Second array:
> [0, 0, 1]
>
> In next code 'r' not reference. But expected ref.
>
> import std.range : zip;
>
> foreach(checker, ref r; zip(checkers, result.set)) {
> r = checker();
> }
>
> What is the best way for speed and beautyfull D code?
Detailed:
import std.range : zip;
struct Result {
byte[] set;
}
Result result;
alias Callback = bool delegate();
Callback[] checkers;
foreach(checker, ref r; zip(checkers, result.set)) {
r = checker();
}
|
June 10, 2019 Re: How to walk over two arrays by ref in beautyfull way? | ||||
---|---|---|---|---|
| ||||
Posted in reply to vitalfadeev | On Monday, 10 June 2019 at 07:45:42 UTC, vitalfadeev wrote: > On Monday, 10 June 2019 at 07:41:40 UTC, vitalfadeev wrote: >> How to? >> >> I plan scan First array with checkers: >> [checker, checker, checker] >> >> ...and store result to Second array: >> [0, 0, 1] >> >> In next code 'r' not reference. But expected ref. >> >> import std.range : zip; >> >> foreach(checker, ref r; zip(checkers, result.set)) { >> r = checker(); >> } >> >> What is the best way for speed and beautyfull D code? > Detailed: import std.range : zip; import std.stdio : writeln, write; struct Result { byte[] set; } alias Callback = bool function(); Callback[] checkers; Result result; bool false_cb() { return 0; } bool true_cb() { return 1; } void init() { checkers ~= &false_cb; checkers ~= &false_cb; checkers ~= &true_cb; result.set.length = checkers.length; } void check() { foreach(checker, ref r; zip(checkers, result.set)) { r = checker(); } } void print() { foreach(r; result.set) { write(r, " "); } writeln(); } void main() { init(); check(); print(); } https://run.dlang.io/is/Plqcq5 |
June 10, 2019 Re: How to walk over two arrays by ref in beautyfull way? | ||||
---|---|---|---|---|
| ||||
Posted in reply to vitalfadeev | On Monday, 10 June 2019 at 08:02:18 UTC, vitalfadeev wrote: > On Monday, 10 June 2019 at 07:45:42 UTC, vitalfadeev wrote: >> On Monday, 10 June 2019 at 07:41:40 UTC, vitalfadeev wrote: >>> How to? >>> >>> I plan scan First array with checkers: >>> [checker, checker, checker] >>> >>> ...and store result to Second array: >>> [0, 0, 1] >>> >>> In next code 'r' not reference. But expected ref. >>> >>> import std.range : zip; >>> >>> foreach(checker, ref r; zip(checkers, result.set)) { >>> r = checker(); >>> } Replace zip with lockstep and your example works. "zip is similar to lockstep, but lockstep doesn't bundle its elements and uses the opApply protocol. lockstep allows reference access to the elements in foreach iterations." https://dlang.org/library/std/range/zip.html "Function lockstep Iterate multiple ranges in lockstep using a foreach loop. In contrast to zip it allows reference access to its elements." https://dlang.org/library/std/range/lockstep.html |
June 10, 2019 Re: How to walk over two arrays by ref in beautyfull way? | ||||
---|---|---|---|---|
| ||||
Posted in reply to vitalfadeev Attachments:
| On Mon, 2019-06-10 at 08:02 +0000, vitalfadeev via Digitalmars-d-learn wrote:
[…]
Perhaps I am missing something that is critical to the example, but I rewrote the code as:
import std.algorithm: map;
import std.stdio: writeln;
bool false_cb() { return false; }
bool true_cb() { return true; }
void main() {
auto checkers = [&false_cb, &false_cb, &true_cb];
auto result = map!(a => a())(checkers);
writeln(result);
}
This avoids the assumption that 0 is false and 1 is true. Also it makes use of D map which seems to me what is happening at the core of the code – using foreach and zip or lockstep just seems to be implementing map. But as I say, I may be missing something.
--
Russel.
===========================================
Dr Russel Winder t: +44 20 7585 2200
41 Buckmaster Road m: +44 7770 465 077
London SW11 1EN, UK w: www.russel.org.uk
|
June 10, 2019 Re: How to walk over two arrays by ref in beautyfull way? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | Thanks! I happy! You are really powerfull! |
Copyright © 1999-2021 by the D Language Foundation