Thread overview
How about a function called contains() for the module std.array?
Oct 19, 2019
Murilo
Oct 19, 2019
Adam D. Ruppe
Oct 20, 2019
Murilo
Oct 20, 2019
Murilo
Oct 20, 2019
Murilo
Oct 20, 2019
Les De Ridder
Oct 20, 2019
Murilo
October 19, 2019
I needed a function that tells if an array contains the elements of another, as in `[1, 2, 3, 4, 5, 6].contains([6, 4])` by returning a `bool`. I did not find such function so I made my own version but I'm unable to submit a PR on GitHub since I don't know how to write a function using that style that I saw on GitHub. I imagine that maybe you guys could take my function and refactor it so it will be according to your rules and then add it to std.array. What do you think?
Here is my function:
//This function returns a bool telling if an array totally contains the elements of a smaller array
import std.algorithm.searching : countUntil;
import std.algorithm.sorting : sort;

bool contains(long[] a, long[] b)
{
    //first we sort both of them
    sort(a);
    sort(b);
    //now we check them using slices and return the result
    return b == a[a.countUntil(b[0]) .. a.countUntil(b[$ - 1]) + 1];
}
/*
Example:
long[] a = [3, 9, 1, 4, 7, 6, 5, 8, 2];
long[] b = [5, 4, 6];
writeln(contains(a, b));
*/
October 19, 2019
On Saturday, 19 October 2019 at 21:47:57 UTC, Murilo wrote:
> I did not find such function

It is called `canFind`

http://dpldocs.info/experimental-docs/std.algorithm.searching.canFind.canFind.3.html


import std.algorithm.searching;
import std.stdio;

void main() {
        bool f = [1,2,3,4,5,6].canFind([3,5,6]);
        writeln(f);
}
October 20, 2019
On Saturday, 19 October 2019 at 21:58:55 UTC, Adam D. Ruppe wrote:
> On Saturday, 19 October 2019 at 21:47:57 UTC, Murilo wrote:
>> I did not find such function
>
> It is called `canFind`
>
> http://dpldocs.info/experimental-docs/std.algorithm.searching.canFind.canFind.3.html
>
>
> import std.algorithm.searching;
> import std.stdio;
>
> void main() {
>         bool f = [1,2,3,4,5,6].canFind([3,5,6]);
>         writeln(f);
> }

Unfortunately I tested it here and it didn't work. The documentation also shows that it is not really what I am looking for. What I want is a function that checks if all the elements of one set are contained in another set, regardless of the order in which they are written.
October 20, 2019
On Saturday, 19 October 2019 at 21:58:55 UTC, Adam D. Ruppe wrote:
> On Saturday, 19 October 2019 at 21:47:57 UTC, Murilo wrote:
>> I did not find such function
>
> It is called `canFind`
>
> http://dpldocs.info/experimental-docs/std.algorithm.searching.canFind.canFind.3.html
>
>
> import std.algorithm.searching;
> import std.stdio;
>
> void main() {
>         bool f = [1,2,3,4,5,6].canFind([3,5,6]);
>         writeln(f);
> }

That unfortunately returned false.
October 20, 2019
On Sunday, 20 October 2019 at 02:19:06 UTC, Murilo wrote:
> On Saturday, 19 October 2019 at 21:58:55 UTC, Adam D. Ruppe wrote:
>> On Saturday, 19 October 2019 at 21:47:57 UTC, Murilo wrote:
>>> I did not find such function
>>
>> It is called `canFind`
>>
>> http://dpldocs.info/experimental-docs/std.algorithm.searching.canFind.canFind.3.html
>>
>>
>> import std.algorithm.searching;
>> import std.stdio;
>>
>> void main() {
>>         bool f = [1,2,3,4,5,6].canFind([3,5,6]);
>>         writeln(f);
>> }
>
> That unfortunately returned false.

bool f = [1,2,3,4,5,6].canFind!(e => e.canFind([3,5,6]));

October 20, 2019
On Saturday, 19 October 2019 at 21:47:57 UTC, Murilo wrote:
> I needed a function that tells if an array contains the elements of another, as in `[1, 2, 3, 4, 5, 6].contains([6, 4])` by returning a `bool`. I did not find such function so I made my own version but I'm unable to submit a PR on GitHub since I don't know how to write a function using that style that I saw on GitHub. I imagine that maybe you guys could take my function and refactor it so it will be according to your rules and then add it to std.array. What do you think?
> Here is my function:
> //This function returns a bool telling if an array totally contains the elements of a smaller array
> import std.algorithm.searching : countUntil;
> import std.algorithm.sorting : sort;
>
> bool contains(long[] a, long[] b)
> {
>     //first we sort both of them
>     sort(a);
>     sort(b);
>     //now we check them using slices and return the result
>     return b == a[a.countUntil(b[0]) .. a.countUntil(b[$ - 1]) + 1];
> }
> /*
> Example:
> long[] a = [3, 9, 1, 4, 7, 6, 5, 8, 2];
> long[] b = [5, 4, 6];
> writeln(contains(a, b));
> */

You could do something like this:

    import std.stdio;
    import std.algorithm : all, canFind;

    auto a = [1, 2, 3, 4, 5, 6];
    auto f = [3, 5, 6].all!(n => a.canFind(n));
    writeln(f);

PS: this should probably have been posted to the Learn list.
October 20, 2019
On Sunday, 20 October 2019 at 02:42:12 UTC, Arun Chandrasekaran wrote:
> On Sunday, 20 October 2019 at 02:19:06 UTC, Murilo wrote:
>> On Saturday, 19 October 2019 at 21:58:55 UTC, Adam D. Ruppe wrote:
>>> On Saturday, 19 October 2019 at 21:47:57 UTC, Murilo wrote:
>>>> I did not find such function
>>>
>>> It is called `canFind`
>>>
>>> http://dpldocs.info/experimental-docs/std.algorithm.searching.canFind.canFind.3.html
>>>
>>>
>>> import std.algorithm.searching;
>>> import std.stdio;
>>>
>>> void main() {
>>>         bool f = [1,2,3,4,5,6].canFind([3,5,6]);
>>>         writeln(f);
>>> }
>>
>> That unfortunately returned false.
>
> bool f = [1,2,3,4,5,6].canFind!(e => e.canFind([3,5,6]));

That triggered an error.
October 20, 2019
On Sunday, 20 October 2019 at 02:46:24 UTC, Les De Ridder wrote:
> On Saturday, 19 October 2019 at 21:47:57 UTC, Murilo wrote:
>> I needed a function that tells if an array contains the elements of another, as in `[1, 2, 3, 4, 5, 6].contains([6, 4])` by returning a `bool`. I did not find such function so I made my own version but I'm unable to submit a PR on GitHub since I don't know how to write a function using that style that I saw on GitHub. I imagine that maybe you guys could take my function and refactor it so it will be according to your rules and then add it to std.array. What do you think?
>> Here is my function:
>> //This function returns a bool telling if an array totally contains the elements of a smaller array
>> import std.algorithm.searching : countUntil;
>> import std.algorithm.sorting : sort;
>>
>> bool contains(long[] a, long[] b)
>> {
>>     //first we sort both of them
>>     sort(a);
>>     sort(b);
>>     //now we check them using slices and return the result
>>     return b == a[a.countUntil(b[0]) .. a.countUntil(b[$ - 1]) + 1];
>> }
>> /*
>> Example:
>> long[] a = [3, 9, 1, 4, 7, 6, 5, 8, 2];
>> long[] b = [5, 4, 6];
>> writeln(contains(a, b));
>> */
>
> You could do something like this:
>
>     import std.stdio;
>     import std.algorithm : all, canFind;
>
>     auto a = [1, 2, 3, 4, 5, 6];
>     auto f = [3, 5, 6].all!(n => a.canFind(n));
>     writeln(f);
>
> PS: this should probably have been posted to the Learn list.

That worked, thanks! But I find that too complicated, just a function would be easier.