How to walk over two arrays by ref in beautyfull way?
vitalfadeev
vital.fadeev at gmail.com
Mon Jun 10 08:02:18 UTC 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:
>> 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
More information about the Digitalmars-d-learn
mailing list