Thread overview
std.algorithm.strip functionality
2 days ago
Pete Padil
2 days ago
monkyyy
2 days ago
Pete Padil
2 days ago
Bastiaan Veelo
2 days ago
Pete Padil
2 days ago

I compiled and ran the following test program:

import std.stdio, std.algorithm;

void main()
{
   long[] a = [1, 2, 3, 15, 4];
   auto b = a[].strip(15);
   writeln(a);
   writeln(b);
}

I get:
[1, 2, 3, 15, 4]
[1, 2, 3, 15, 4]
It did not remove 15, it does work if 15 is at the beginning or end.
Is this a bug or am I misunderstading the docs?

version: 1.41.0-beta1 (DMD v2.111.0, LLVM 19.1.7)

thanks

2 days ago

On Tuesday, 6 May 2025 at 21:19:29 UTC, Pete Padil wrote:

>

I compiled and ran the following test program:

import std.stdio, std.algorithm;

void main()
{
   long[] a = [1, 2, 3, 15, 4];
   auto b = a[].strip(15);
   writeln(a);
   writeln(b);
}

I get:
[1, 2, 3, 15, 4]
[1, 2, 3, 15, 4]
It did not remove 15, it does work if 15 is at the beginning or end.
Is this a bug or am I misunderstading the docs?

version: 1.41.0-beta1 (DMD v2.111.0, LLVM 19.1.7)

thanks

Intended; if you want [1,2,3] .retro.find!(a=>a==15).drop(1)/maybe/.retro

(I think finding needs an api rework)

2 days ago

On Tuesday, 6 May 2025 at 21:54:31 UTC, monkyyy wrote:

>

On Tuesday, 6 May 2025 at 21:19:29 UTC, Pete Padil wrote:

>

I compiled and ran the following test program:

import std.stdio, std.algorithm;

void main()
{
   long[] a = [1, 2, 3, 15, 4];
   auto b = a[].strip(15);
   writeln(a);
   writeln(b);
}

I get:
[1, 2, 3, 15, 4]
[1, 2, 3, 15, 4]
It did not remove 15, it does work if 15 is at the beginning or end.
Is this a bug or am I misunderstading the docs?

version: 1.41.0-beta1 (DMD v2.111.0, LLVM 19.1.7)

thanks

Intended; if you want [1,2,3] .retro.find!(a=>a==15).drop(1)/maybe/.retro

(I think finding needs an api rework)

Ah, got it. Misread the docs. Thanks.

2 days ago

These both will print "[1, 2, 3]":

   writeln([1, 2, 3, 15, 4].until([15]));
   writeln([1, 2, 3, 15, 4].findSplit([15])[0]);

You can press "improve this page" on the top of https://dlang.org/phobos/std_algorithm_mutation.html#strip if you like to add more clarity to the documentation.

-- Bastiaan.

2 days ago
On Tuesday, May 6, 2025 3:19:29 PM Mountain Daylight Time Pete Padil via Digitalmars-d-learn wrote:
> I compiled and ran the following test program:
> ```d
> import std.stdio, std.algorithm;
>
> void main()
> {
>     long[] a = [1, 2, 3, 15, 4];
>     auto b = a[].strip(15);
>     writeln(a);
>     writeln(b);
> }
> ```
> I get:
> [1, 2, 3, 15, 4]
> [1, 2, 3, 15, 4]
> It did not remove 15, it does work if 15 is at the beginning or
> end.
> Is this a bug or am I misunderstading the docs?
>
> version:   1.41.0-beta1 (DMD v2.111.0, LLVM 19.1.7)
>
> thanks

strip removes the requested elements from the ends (whereas stripLeft does it from the front of the range, and stripRight does it from the back of the range). It's a generalization of the strip function which strips whitespace from both ends (what some languages call trim).

If you want to remove an element from the middle, then std.algorithm.iteration.filter will return a lazy range which filters elements based on the predicate - e.g.

auto result = range.filter!(a=> a != 15)();

and if you want an array instead of a lazy range, you can allocate a new one with std.array.array, e.g.

auto result = range.filter!(a=> a != 15)().array();

Alternatively, std.algorithm.mutation.remove can be used to remove an element at a specific index (shifting all of the other elements after it in the array), but then you'll need to use a function such as std.algorithm.searching.find or std.algorithm.countUntil to get the element's offset to pass to remove.

- Jonathan M Davis




2 days ago
On Wednesday, 7 May 2025 at 01:40:33 UTC, Jonathan M Davis wrote:
> On Tuesday, May 6, 2025 3:19:29 PM Mountain Daylight Time Pete Padil via Digitalmars-d-learn wrote:
>> [...]
>
> strip removes the requested elements from the ends (whereas stripLeft does it from the front of the range, and stripRight does it from the back of the range). It's a generalization of the strip function which strips whitespace from both ends (what some languages call trim).
>
> [...]

Thank you for the replies. They help me discover library features I haven't used before.