digitalmars.D.learn - std.algorithm.strip functionality
- Pete Padil (19/19) May 06 I compiled and ran the following test program:
- monkyyy (4/23) May 06 Intended; if you want [1,2,3]
- Pete Padil (2/28) May 06 Ah, got it. Misread the docs. Thanks.
- Bastiaan Veelo (9/9) May 06 These both will print "[1, 2, 3]":
- Jonathan M Davis (18/37) May 06 strip removes the requested elements from the ends (whereas stripLeft do...
- Pete Padil (3/12) May 06 Thank you for the replies. They help me discover library features
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
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) thanksIntended; if you want [1,2,3] .retro.find!(a=>a==15).drop(1)/*maybe*/.retro (I think finding needs an api rework)
May 06
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:Ah, got it. Misread the docs. Thanks.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) thanksIntended; if you want [1,2,3] .retro.find!(a=>a==15).drop(1)/*maybe*/.retro (I think finding needs an api rework)
May 06
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
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) thanksstrip 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
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:Thank you for the replies. They help me discover library features I haven't used before.[...]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). [...]
May 06