www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.algorithm.strip functionality

reply Pete Padil <padil gm.com> writes:
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
May 06
next sibling parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Tuesday, 6 May 2025 at 21:19:29 UTC, Pete Padil 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
Intended; if you want [1,2,3] .retro.find!(a=>a==15).drop(1)/*maybe*/.retro (I think finding needs an api rework)
May 06
parent Pete Padil <padil gm.com> writes:
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:
 ```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
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.
May 06
prev sibling next sibling parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
These both will print "[1, 2, 3]":
```d
    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.
May 06
prev sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
May 06
parent Pete Padil <padil gm.com> writes:
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.
May 06