Re: A little help with Ranges

2021-08-27 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Re: A little help with Ranges

2021-08-26 Thread Merlin Diavova via Digitalmars-d-learn

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!


Re: A little help with Ranges

2021-08-26 Thread Ali Çehreli via Digitalmars-d-learn

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)
  .filter!(line => line.front != '#')
  .array;

  if (significantLines.empty) {
writeln("There were no significant lines.");

  } else {
writefln!"The lines: %-(\n%s%)"(significantLines);
  }
}

Ali


Re: A little help with Ranges

2021-08-26 Thread Stefan Koch via Digitalmars-d-learn

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.


Re: A little help with Ranges

2021-08-26 Thread Merlin Diavova via Digitalmars-d-learn

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?


Re: A little help with Ranges

2021-08-26 Thread Stefan Koch via Digitalmars-d-learn

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


A little help with Ranges

2021-08-26 Thread Merlin Diavova via Digitalmars-d-learn

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