Thread overview
Are there some helpers in Phobos equivalent to std::set_difference of ugly c++
Feb 03, 2023
Ferhat Kurtulmuş
Feb 03, 2023
Ferhat Kurtulmuş
Feb 03, 2023
Ali Çehreli
Feb 03, 2023
Ali Çehreli
February 03, 2023

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:

    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 }
February 04, 2023
```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]
February 03, 2023
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.
February 04, 2023
All good, I'm glad it'll work for you :)

I used the word difference to search the phobos docs with.
February 03, 2023
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

February 03, 2023
On 2/3/23 09:11, Ali Çehreli wrote:

> 'fold' is

Ha ha! :) Make that 'reduce' of course.

Ali