Thread overview
A little help with Ranges
Aug 27, 2021
Merlin Diavova
Aug 27, 2021
Stefan Koch
Aug 27, 2021
Merlin Diavova
Aug 27, 2021
Stefan Koch
Aug 27, 2021
Ali Çehreli
Aug 27, 2021
Merlin Diavova
August 27, 2021

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

August 27, 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!

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

August 27, 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:

>

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?

August 27, 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:

>

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.

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

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

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

Ali
August 27, 2021

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

.filter!(not!empty)

My code now works exactly how I wanted. Thanks!

August 27, 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

.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):

.filter!(r => !r.empty)

-Steve