www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Are there some helpers in Phobos equivalent to std::set_difference of

reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
I could not find a thing doing this. And I don't want to write it 
from scratch. Getting a range as output is ok too. What I need is 
the following:

```c++
     const std::vector<int> v1{1, 2, 5, 5, 5, 9};
     const std::vector<int> v2{2, 5, 7};
     std::vector<int> diff;

     std::set_difference(v1.begin(), v1.end(), v2.begin(), 
v2.end(),
                         std::inserter(diff, diff.begin()));
     print(v1, "∖ ");
     print(v2, "= ");
     print(diff, "\n");

     // yielding { 1 2 5 5 5 9 } ∖ { 2 5 7 } = { 1 5 5 9 }
```
Feb 03 2023
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
```d
import std.stdio, std.algorithm.setops, std.array;

void main() {
     int[] v1 = [1, 2, 5, 5, 5, 9];
     int[] v2 = [2, 5, 7];
     int[] v3 = setDifference(v1, v2).array;

     writefln!"%s \\ %s = %s"(v1, v2, v3);
}
```

[1, 2, 5, 5, 5, 9] \ [2, 5, 7] = [1, 5, 5, 9]
Feb 03 2023
parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Friday, 3 February 2023 at 15:53:35 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 ```d
 import std.stdio, std.algorithm.setops, std.array;

 void main() {
     int[] v1 = [1, 2, 5, 5, 5, 9];
     int[] v2 = [2, 5, 7];
     int[] v3 = setDifference(v1, v2).array;

     writefln!"%s \\ %s = %s"(v1, v2, v3);
 }
 ```

 [1, 2, 5, 5, 5, 9] \ [2, 5, 7] = [1, 5, 5, 9]
Thank you very much. I am surprised that I couldn't find it using many keywords on the site and google.
Feb 03 2023
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
All good, I'm glad it'll work for you :)

I used the word difference to search the phobos docs with.
Feb 03 2023
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/3/23 08:01, Richard (Rikki) Andrew Cattermole wrote:
 All good, I'm glad it'll work for you :)
 
 I used the word difference to search the phobos docs with.
'fold' is doing much better in that department because it mentions other names: "Implements the homonym function (also known as accumulate, compress, inject, or foldl) present in various programming languages of functional flavor." https://dlang.org/library/std/algorithm/iteration/reduce.html Ali
Feb 03 2023
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/3/23 09:11, Ali Çehreli wrote:

 'fold' is
Ha ha! :) Make that 'reduce' of course. Ali
Feb 03 2023