A little help with Ranges
Steven Schveighoffer
schveiguy at gmail.com
Fri Aug 27 13:32:59 UTC 2021
On 8/27/21 12:41 AM, Merlin Diavova wrote:
> On Friday, 27 August 2021 at 04:01:19 UTC, Ali Çehreli wrote:
>> On 8/26/21 7:17 PM, Merlin Diavova wrote:
>>
>>> [...]
>>
>> Then the operations downstream will not produce any results. For
>> example, the array will be empty below:
>>
>> import std.stdio;
>> import std.range;
>> import std.algorithm;
>> import std.string;
>> import std.functional;
>>
>> void main() {
>> auto significantLines = stdin
>> .byLineCopy
>> .map!strip
>> .filter!(not!empty)
>> .filter!(line => line.front != '#')
>> .array;
>>
>> if (significantLines.empty) {
>> writeln("There were no significant lines.");
>>
>> } else {
>> writefln!"The lines: %-(\n%s%)"(significantLines);
>> }
>> }
>>
>> Ali
>
> And there it is!
>
> I was missing
>
> ```d
> .filter!(not!empty)
> ```
>
> My code now works exactly how I wanted. Thanks!
Be careful with this! `not!empty` is *only* working because you are
using arrays (where `empty` is a UFCS function defined in std.range).
Other ranges this will not work on. Instead, I would recommend a lambda
(which will work with arrays too):
```d
.filter!(r => !r.empty)
```
-Steve
More information about the Digitalmars-d-learn
mailing list