digitalmars.D - How about a function called contains() for the module std.array?
- Murilo (28/28) Oct 19 2019 I needed a function that tells if an array contains the elements
- Adam D. Ruppe (9/10) Oct 19 2019 It is called `canFind`
- Murilo (6/16) Oct 19 2019 Unfortunately I tested it here and it didn't work. The
- Murilo (2/12) Oct 19 2019 That unfortunately returned false.
- Arun Chandrasekaran (2/20) Oct 19 2019 bool f = [1,2,3,4,5,6].canFind!(e => e.canFind([3,5,6]));
- Murilo (3/24) Oct 19 2019 That triggered an error.
- Les De Ridder (8/36) Oct 19 2019 You could do something like this:
- Murilo (3/40) Oct 19 2019 That worked, thanks! But I find that too complicated, just a
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)); */
Oct 19 2019
On Saturday, 19 October 2019 at 21:47:57 UTC, Murilo wrote:I did not find such functionIt 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); }
Oct 19 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: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.I did not find such functionIt 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); }
Oct 19 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:That unfortunately returned false.I did not find such functionIt 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); }
Oct 19 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:bool f = [1,2,3,4,5,6].canFind!(e => e.canFind([3,5,6]));On Saturday, 19 October 2019 at 21:47:57 UTC, Murilo wrote:That unfortunately returned false.I did not find such functionIt 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); }
Oct 19 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:That triggered an error.On Saturday, 19 October 2019 at 21:58:55 UTC, Adam D. Ruppe wrote:bool f = [1,2,3,4,5,6].canFind!(e => e.canFind([3,5,6]));On Saturday, 19 October 2019 at 21:47:57 UTC, Murilo wrote:That unfortunately returned false.I did not find such functionIt 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); }
Oct 19 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.
Oct 19 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:That worked, thanks! But I find that too complicated, just a function would be easier.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.
Oct 19 2019