For N=1, I would use std.array : empty
to check for length being at least N, and avoid having to go through the whole input.
For N>1 (or all N), what is the idiomatic way in D language?
I've tried to use the std.range : take
which "Lazily takes only up to n elements of a range.". It works for arrays but not for ranges (unless of course I make a sub-range into an array):
#!/usr/bin/env rdmd
module test_ranges;
void main()
{
import std.container.dlist : DList;
assert(lengthAtLeast([1, 2, 3], 2) == true);
// assert(lengthAtLeast(DList!int(1, 2, 3)[], 2) == true);
/*
test_ranges.d(64): Error: no property length for type Take!(Range)
test_ranges.d(10): Error: template instance `test_ranges.lengthAtLeast!(Range)` error instantiating
Failed: ["/usr/bin/dmd", "-v", "-o-", "test_ranges.d", "-I."]
*/
}
bool lengthAtLeast(R)(R input, size_t n)
{
import std.range : take;
return input.take(n).length == n;
// this makes it work for arrays and ranges alike, but is not nice, is it?
// import std.array : array;
// return input.take(n).array.length == n;
}
walkLength
does what you want:
bool lengthAtLeast(R)(R input, size_t n)
{
import std.range.primitives : walkLength;
return input.walkLength(n) >= n; // walks upTo n or returns the length if known
}