questions around mutating range algorithms, const, and return ref
aliak
something at something.com
Sun Jan 28 22:52:36 UTC 2018
Hello, I'm trying to write a function called "pull" that, given 2
ranges, "pull"s the values from range 2 out of range 1. I'm not
sure if I'm doing it correctly though, and I have some questions
so any help is appreciated. This is what I have:
ref pull(R1, R2)(return ref R1 r1, R2 r2) {
import std.algorithm, std.array, std.range;
int numFound = 0;
auto r = r1.save;
ElementType!R1[] unfoundElements;
foreach (e; r) {
if (!r2.canFind(e)) {
unfoundElements ~= e;
} else {
numFound++;
}
}
moveEmplaceAll(unfoundElements, r1);
r1.popBackN(numFound);
return r1;
}
So my questions are:
1) So first of all, is there a function like this in phobos that
I can use? From what I understand phobos is supposed to be nogc
so mutating functions like these are usually in place ones?
2) Is there a way to avoid the extra "moveEmplaceAll" call
towards the end and still get the same results?
3) Is it necessary to always import std.array (or std.range:
empty, front) when doing work like this with range algorithms?
(otherwise you get no property save when an array is used as a
range)
4) is the .save necessary? My (initial) tests seem to show that
it works without, but from my understanding of what save does, it
feels like it's necessary.
5) return ref and -dip25 ... is that something that's going to
become default at some time? Is there a timeline? (also curious
about dip1000 for that matter).
6) It will not work when I pass in a range that has const
elements.
ie:
const(int)[] arr = [1, 2, 3, 4, 5];
arr.pull([2, 3]);
This I guess makes sense because moveEmplaceAll depends on
isInputRange and from my understanding, a const range cannot be
one (is that correct?). So am I correct in thinking there really
is no way around this, or is there some move/cast trickery that I
can use maybe?
Cheers,
- Ali
More information about the Digitalmars-d-learn
mailing list