Re: Proper way to accept either static or dynamic array as a parameter
On Sunday, 12 September 2021 at 02:44:36 UTC, Alex Bryan wrote: `T[] dynArr` can be passed (by reference) to a function that takes `ref T[] data` but `T[10] data` cannot? Why not? ```d void add1(ref int[] nums) { nums ~= 1; } unittest { int[] nums; nums.add1; nums.add1; nums.add1; assert(nums == [1, 1, 1]); } ``` the `ref` allows `add1` to extend the length of the slice passed to it, with potential reallocation. This doesn't make sense with a static array whose length can't be modified.
Re: Proper way to accept either static or dynamic array as a parameter
On Sunday, 12 September 2021 at 01:48:07 UTC, H. S. Teoh wrote: On Sun, Sep 12, 2021 at 01:08:17AM +, Alex Bryan via Digitalmars-d-learn wrote: I am having trouble discovering what the proper (or at least a proper) way is to write a function that can take either a static or dynamic array as a parameter. My current implementation consists of 2 overloaded functions (one takes a dynamic array, the other takes a static array) with 99% copy/pasted code. My intuition tells me this is dirty, and there's a better way to do this with templates, but for the life of me I just can't figure it out. Would someone be so kind as to please help me out? [...] Just make the function take an array parameter. Static arrays will decay into a slice (though my recommendation is to explicitly slice it with the [] operator): auto myFunction(T[] data) { ... } T[10] staticArr; T[] dynArr = [ ... ]; myFunction(staticArr); // implicit slice, not recommended myFunction(staticArr[]);// explicit slice, better myFunction(dynArr); T So it turns out my issue was that my function had an unnecessary ref: auto myFunction(ref T[] data) { ... } T[10] staticArr; T[] dynArr = [ ... ]; myFunction(staticArr); // does not compile myFunction(staticArr[]); // does not compile myFunction(dynArr); // compiles Since I was modifying the contents of data, I thought I needed the ref, but this is not the case. Removing the ref and the code works as you described. Let me see if I can summarize what I've learned (and ask additional questions): `T[] data` is essentially already a reference since it contains a pointer and a length and allows you to modify the contents it points to, but if I wanted to modify either the pointer or length in `T[] data` in that function (for some reason) I'd need to pass it in as a `ref`? `T[] dynArr` can be passed (by reference) to a function that takes `ref T[] data` but `T[10] data` cannot? Why not?
Re: Proper way to accept either static or dynamic array as a parameter
On Sun, Sep 12, 2021 at 01:08:17AM +, Alex Bryan via Digitalmars-d-learn wrote: > I am having trouble discovering what the proper (or at least a proper) > way is to write a function that can take either a static or dynamic > array as a parameter. My current implementation consists of 2 > overloaded functions (one takes a dynamic array, the other takes a > static array) with 99% copy/pasted code. My intuition tells me this is > dirty, and there's a better way to do this with templates, but for the > life of me I just can't figure it out. Would someone be so kind as to > please help me out? [...] Just make the function take an array parameter. Static arrays will decay into a slice (though my recommendation is to explicitly slice it with the [] operator): auto myFunction(T[] data) { ... } T[10] staticArr; T[] dynArr = [ ... ]; myFunction(staticArr); // implicit slice, not recommended myFunction(staticArr[]);// explicit slice, better myFunction(dynArr); T -- Life is too short to run proprietary software. -- Bdale Garbee
Proper way to accept either static or dynamic array as a parameter
I am having trouble discovering what the proper (or at least a proper) way is to write a function that can take either a static or dynamic array as a parameter. My current implementation consists of 2 overloaded functions (one takes a dynamic array, the other takes a static array) with 99% copy/pasted code. My intuition tells me this is dirty, and there's a better way to do this with templates, but for the life of me I just can't figure it out. Would someone be so kind as to please help me out? Other than this frustration I am enjoying programming in D!
Re: File difference module similar to the Linux command "comm"
On Saturday, 11 September 2021 at 23:17:31 UTC, Vino wrote: Hi All, May i know whether there is any module similar to the Linux command "comm" (finding difference between 2 files), if present can you please guide me through link to the page nor do let me know if there is any other solution. Eg File1 list1 list2 list3 lsit5 File2 list1 list3 lsit6 File1 : difference: list6 File2 : difference: list2,list5 From, Vino Take a look at https://dlang.org/phobos/std_algorithm_setops.html With these inputs: ```d import std.string : strip, splitLines; import std.algorithm : equal, setDifference; string[] File1 = q"EOF list1 list2 list3 list5 EOF".strip.splitLines; string[] File2 = q"EOF list1 list3 list6 EOF".strip.splitLines; unittest { assert(["list2", "list5"].equal(File1.setDifference(File2))); assert(["list6"].equal(File2.setDifference(File1))); } ```
File difference module similar to the Linux command "comm"
Hi All, May i know whether there is any module similar to the Linux command "comm" (finding difference between 2 files), if present can you please guide me through link to the page nor do let me know if there is any other solution. Eg File1 list1 list2 list3 lsit5 File2 list1 list3 lsit6 File1 : difference: list6 File2 : difference: list2,list5 From, Vino
Re: Array permutations
On Saturday, 11 September 2021 at 23:04:29 UTC, Vino wrote: On Saturday, 11 September 2021 at 19:57:26 UTC, jfondren wrote: [...] Hi, Thank you very much, let me try to explain the actual requirement, the actual requirement is to find all the permutations of a given array, I modified your code and it gives me the exact output , but i need help to remove the array which has the same value as below [...] Hi, Was able to resolve the issue by adding the filter ".filter!"a[0] != a[1]" From, Vino
Re: Array permutations
On Saturday, 11 September 2021 at 19:57:26 UTC, jfondren wrote: On Saturday, 11 September 2021 at 19:37:42 UTC, Vino wrote: Hi All, Request your help on the below to print the below array as "Required output", Was able to get these values "[1,2],[2,3],[3,4],[4,5]" by using list.slide(2), need your help to get values "1,3],[1,4],[1,5],[2,4],[2,5],[3,5]" auto list[] = [1,2,3,4,5] Required output [1,2],[2,3],[3,4],[4,5],[1,3],[1,4],[1,5],[2,4],[2,5],[3,5] From, Vino I don't see the pattern just from the required output. If you have an English description of what you want, it might be easier. Anyway, take a look at ```d unittest { import std.algorithm : cartesianProduct, filter, map, sort; import std.range : drop; import std.array : array; auto list = [1,2,3,4,5]; auto required = [[1,2],[2,3],[3,4],[4,5],[1,3],[1,4],[1,5],[2,4],[2,5],[3,5]]; auto pairs = list.cartesianProduct(list.drop(1)) .filter!"a[0] < a[1]" .map!array .array; assert(pairs == [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]]); assert(required.sort.array == pairs); } ``` Hi, Thank you very much, let me try to explain the actual requirement, the actual requirement is to find all the permutations of a given array, I modified your code and it gives me the exact output , but i need help to remove the array which has the same value as below Example: void main() { import std.algorithm : cartesianProduct, filter, map, sort; import std.range : drop; import std.array : array; import std.stdio : writeln; auto list = [1,2,3,4,5]; auto pairs = list.cartesianProduct(list) .map!array .array; writeln(pairs); } Output [ [1, 1], /* shodule not print this array */ [1, 2], [1, 3], [1, 4], [1, 5], [2, 1], [2, 2], /* should not print this array */ [2, 3], [2, 4], [2, 5], [3, 1], [3, 2], [3, 3], /* should not print this array */ [3, 4], [3, 5], [4, 1], [4, 2], [4, 3], [4, 4], /* should not print this array */ [4, 5], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5] /* should not print this array */ ] From, Vino
Re: Array permutations
On Saturday, 11 September 2021 at 19:57:26 UTC, jfondren wrote: auto pairs = list.cartesianProduct(list.drop(1)) This `drop` isn't necessary.
Re: Array permutations
On Saturday, 11 September 2021 at 19:37:42 UTC, Vino wrote: Request your help on the below to print the below array as "Required output", Was able to get these values "[1,2],[2,3],[3,4],[4,5]" by using list.slide(2), need your help to get values "1,3],[1,4],[1,5],[2,4],[2,5],[3,5]" ```d import std; auto slideEnds(List)(List list, size_t slideLen) { return list.slide(slideLen).map!(window => [window.front, window.back]); } void main() { auto list = [1,2,3,4,5]; iota(2, list.length) .map!(slideLen => list.slideEnds(slideLen)) .joiner .writeln; } ``` This almost does the trick. It leaves out `[1, 5]` for some reason I didn't bother to check though.
Re: Array permutations
On Saturday, 11 September 2021 at 19:37:42 UTC, Vino wrote: Hi All, Request your help on the below to print the below array as "Required output", Was able to get these values "[1,2],[2,3],[3,4],[4,5]" by using list.slide(2), need your help to get values "1,3],[1,4],[1,5],[2,4],[2,5],[3,5]" auto list[] = [1,2,3,4,5] Required output [1,2],[2,3],[3,4],[4,5],[1,3],[1,4],[1,5],[2,4],[2,5],[3,5] From, Vino I don't see the pattern just from the required output. If you have an English description of what you want, it might be easier. Anyway, take a look at ```d unittest { import std.algorithm : cartesianProduct, filter, map, sort; import std.range : drop; import std.array : array; auto list = [1,2,3,4,5]; auto required = [[1,2],[2,3],[3,4],[4,5],[1,3],[1,4],[1,5],[2,4],[2,5],[3,5]]; auto pairs = list.cartesianProduct(list.drop(1)) .filter!"a[0] < a[1]" .map!array .array; assert(pairs == [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]]); assert(required.sort.array == pairs); } ```
Array permutations
Hi All, Request your help on the below to print the below array as "Required output", Was able to get these values "[1,2],[2,3],[3,4],[4,5]" by using list.slide(2), need your help to get values "1,3],[1,4],[1,5],[2,4],[2,5],[3,5]" auto list[] = [1,2,3,4,5] Required output [1,2],[2,3],[3,4],[4,5],[1,3],[1,4],[1,5],[2,4],[2,5],[3,5] From, Vino
Re: Ali's Book - Programming in D
On Friday, 10 September 2021 at 22:26:34 UTC, Ali Çehreli wrote: https://bitbucket.org/acehreli/ddili/commits/ Ah it's there - gotta remember that place if I want to make corrections. Good book - It's the one that got me up to speed with D before I was commited enough to pay for anything. Now while I'm talking, I'll tell the one error I have already spotted, in the `ranges` chapter. It claims that `std.range.generate` calls the underlying function on `front`, not `popFront`. This used to be true long ago, but nowadays the call is done on `popFront` and the call result is cached for `front`.
Re: Ali's Book - Programming in D
On Friday, 10 September 2021 at 22:26:34 UTC, Ali Çehreli wrote: Not off the top of my head but by actual commits. :) https://bitbucket.org/acehreli/ddili/commits/ One major change that I can see is @property being discouraged. Good to know. Thanks, Ali.