digitalmars.D.learn - How to walk over two arrays by ref in beautyfull way?
- vitalfadeev (11/11) Jun 10 2019 How to?
- vitalfadeev (12/23) Jun 10 2019 Detailed:
- vitalfadeev (34/53) Jun 10 2019 import std.range : zip;
- Anonymouse (10/27) Jun 10 2019 Replace zip with lockstep and your example works.
- vitalfadeev (3/3) Jun 10 2019 Thanks!
- Russel Winder (26/26) Jun 10 2019 On Mon, 2019-06-10 at 08:02 +0000, vitalfadeev via Digitalmars-d-learn
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?
Jun 10 2019
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(); }
Jun 10 2019
On Monday, 10 June 2019 at 07:45:42 UTC, vitalfadeev wrote:On Monday, 10 June 2019 at 07:41:40 UTC, vitalfadeev wrote: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/Plqcq5How 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:
Jun 10 2019
On Monday, 10 June 2019 at 08:02:18 UTC, vitalfadeev wrote:On Monday, 10 June 2019 at 07:45:42 UTC, vitalfadeev wrote: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.htmlOn 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(); }
Jun 10 2019
On Mon, 2019-06-10 at 08:02 +0000, vitalfadeev via Digitalmars-d-learn wrote: [=E2=80=A6] 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 =3D [&false_cb, &false_cb, &true_cb]; auto result =3D map!(a =3D> 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 =E2=80=93 using foreach and zip or lockstep just seems to be implement= ing map. But as I say, I may be missing something. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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
Jun 10 2019