digitalmars.D.learn - A little help with Ranges
- Merlin Diavova (8/8) Aug 26 2021 Hi all,
- Stefan Koch (4/12) Aug 26 2021 1. you don't have to handle it.
- Merlin Diavova (5/24) Aug 26 2021 Thanks for the quick response!
- Stefan Koch (4/31) Aug 26 2021 You check if it's by calling empty.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (22/24) Aug 26 2021 Then the operations downstream will not produce any results. For
- Merlin Diavova (7/30) Aug 26 2021 And there it is!
- Steven Schveighoffer (9/50) Aug 27 2021 Be careful with this! `not!empty` is *only* working because you are
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
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! Merlin1. 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
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: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?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! Merlin1. 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
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: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.On Friday, 27 August 2021 at 01:51:42 UTC, Merlin Diavova wrote: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?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! Merlin1. 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
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
On Friday, 27 August 2021 at 04:01:19 UTC, Ali Çehreli wrote:On 8/26/21 7:17 PM, Merlin Diavova wrote:And there it is! I was missing ```d .filter!(not!empty) ``` My code now works exactly how I wanted. Thanks![...]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
On 8/27/21 12:41 AM, Merlin Diavova wrote:On Friday, 27 August 2021 at 04:01:19 UTC, Ali Çehreli wrote: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) ``` -SteveOn 8/26/21 7:17 PM, Merlin Diavova wrote:And there it is! I was missing ```d .filter!(not!empty) ``` My code now works exactly how I wanted. Thanks![...]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
Aug 27 2021