www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A little help with Ranges

reply Merlin Diavova <md mdiavov.com> writes:
Hi all,

I'm Merlin, I'm just starting out in D and super excited.

My questions are:-

1. In a range pipeline how does one handle the event of a filter 
range returning empty?

2. How does one unwrap a single result from a range operation?


Look forward to your assistance!

Merlin
Aug 26 2021
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Friday, 27 August 2021 at 01:51:42 UTC, Merlin Diavova wrote:
 Hi all,

 I'm Merlin, I'm just starting out in D and super excited.

 My questions are:-

 1. In a range pipeline how does one handle the event of a 
 filter range returning empty?

 2. How does one unwrap a single result from a range operation?


 Look forward to your assistance!

 Merlin
1. you don't have to handle it. it just won't go. 2. `takeOne` put it into the search on the dlang site
Aug 26 2021
parent reply Merlin Diavova <md mdiavov.com> writes:
On Friday, 27 August 2021 at 02:10:48 UTC, Stefan Koch wrote:
 On Friday, 27 August 2021 at 01:51:42 UTC, Merlin Diavova wrote:
 Hi all,

 I'm Merlin, I'm just starting out in D and super excited.

 My questions are:-

 1. In a range pipeline how does one handle the event of a 
 filter range returning empty?

 2. How does one unwrap a single result from a range operation?


 Look forward to your assistance!

 Merlin
1. you don't have to handle it. it just won't go. 2. `takeOne` put it into the search on the dlang site
Thanks for the quick response! Took a look at takeOne and yep that's the ticket. What I meant about the handling an empty filter is, what if I want to take an alternative route if the filter returns empty?
Aug 26 2021
next sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
On Friday, 27 August 2021 at 02:17:21 UTC, Merlin Diavova wrote:
 On Friday, 27 August 2021 at 02:10:48 UTC, Stefan Koch wrote:
 On Friday, 27 August 2021 at 01:51:42 UTC, Merlin Diavova 
 wrote:
 Hi all,

 I'm Merlin, I'm just starting out in D and super excited.

 My questions are:-

 1. In a range pipeline how does one handle the event of a 
 filter range returning empty?

 2. How does one unwrap a single result from a range operation?


 Look forward to your assistance!

 Merlin
1. you don't have to handle it. it just won't go. 2. `takeOne` put it into the search on the dlang site
Thanks for the quick response! Took a look at takeOne and yep that's the ticket. What I meant about the handling an empty filter is, what if I want to take an alternative route if the filter returns empty?
You check if it's by calling empty. or do you want a default value? I am sure there is a convince function for that somewhere in phobos.
Aug 26 2021
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 8/26/21 7:17 PM, Merlin Diavova wrote:

 What I meant about the handling an empty filter is, what if I want to 
 take an alternative route if the filter returns empty?
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) .array; if (significantLines.empty) { writeln("There were no significant lines."); } else { writefln!"The lines: %-(\n%s%)"(significantLines); } } Ali
Aug 26 2021
parent reply Merlin Diavova <md mdiavov.com> writes:
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) .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!
Aug 26 2021
parent Steven Schveighoffer <schveiguy gmail.com> writes:
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
Aug 27 2021