Re: Idiomatic way of writing nested loops?
On Monday, 17 July 2017 at 11:07:35 UTC, Anton Fediushin wrote: Hello! What is the best way of rewriting this code in idiomatic D manner? -- foreach(a; ["foo", "bar"]) { foreach(b; ["baz", "foz", "bof"]) { foreach(c; ["FOO", "BAR"]) { // Some operations on a, b and c } } } -- Every array has at least 1 element, and adding/removing new "nested loops" should be as easy as possible. Also, I have a question about running this in parallel: if I want to use nested loops with `parallel` from `std.parallelism`, should I add `parallel` to every loop like this? -- foreach(a; ["foo", "bar"].parallel) { foreach(b; ["baz", "foz", "bof"].parallel) { foreach(c; ["FOO", "BAR"].parallel) { // Some operations on a, b and c } } } -- I am worried about running thousands of threads, because in this case first `parallel` runs 2 tasks, every task runs 3 tasks and every task runned inside a task runs 2 more tasks. So, how to write this in idiomatic D manner and run it _if possible_ in parallel? With regards to parallel, only use it on the outermost loop. Assuming you have more items in the outermost loop than you do threads parallelising more than one loop won't net you any speed.
Re: D doesn't read the first character of a file (reads everything but the first chararacter) with either read() or readText()
On Tuesday, 18 July 2017 at 02:21:59 UTC, Enjoys Math wrote: DMD32 D Compiler v2.074.1 import std.file; void main() { string bigInput = readText("input.txt"); } The file is 7 MB of ascii text, don't know if that matters... Should I upgrade versions? I wonder if it thinks there is a BOM and eats it?
D doesn't read the first character of a file (reads everything but the first chararacter) with either read() or readText()
DMD32 D Compiler v2.074.1 import std.file; void main() { string bigInput = readText("input.txt"); } The file is 7 MB of ascii text, don't know if that matters... Should I upgrade versions?
Re: Appending static arrays
On Mon, Jul 17, 2017 at 10:32:14PM +, Nordlöw via Digitalmars-d-learn wrote: > On Monday, 17 July 2017 at 20:01:41 UTC, H. S. Teoh wrote: [...] > > result[offset .. offset + a.length] = a[]; > > This slice assignment doesn't support conversion between different > element-types, for instance from `int[]` to `double[]`. > > But I'm not convinced that we should allow `CommonType` when operator > ~ doesn't. True. Maybe we should just leave out the `CommonType` thing for now. Not sure how to express that all element types should be equal in the sig constraints, though. (It shouldn't be overly hard to support `CommonType`; just replace the slice assignment with a manual loop. But yeah, probably not worth the effort.) T -- Do not reason with the unreasonable; you lose by definition.
Re: Appending static arrays
On Monday, 17 July 2017 at 20:28:12 UTC, Nordlöw wrote: Made some adjustments with working unittest and put it up on https://github.com/nordlow/phobos-next/blob/master/src/algorithm_ex.d#L2467 Moved here https://github.com/nordlow/phobos-next/blob/master/src/array_ex.d#L2765
Re: Appending static arrays
On Monday, 17 July 2017 at 20:01:41 UTC, H. S. Teoh wrote: OK, here's an actual, compilable, runnable version: import std.algorithm : sum; import std.meta : allSatisfy, staticMap; import std.range : only; import std.traits : CommonType, isStaticArray; alias Elem(A : E[n], E, size_t n) = E; enum Length(A) = A.length; enum sumLengths(A...) = sum(only(0, staticMap!(Length, A))); CommonType!(staticMap!(Elem, A))[sumLengths!A] append(A...)(A arrays) if (allSatisfy!(isStaticArray, A)) { typeof(return) result = void; foreach (i, a; arrays) { enum offset = sumLengths!(A[0 .. i]); result[offset .. offset + a.length] = a[]; This slice assignment doesn't support conversion between different element-types, for instance from `int[]` to `double[]`. But I'm not convinced that we should allow `CommonType` when operator ~ doesn't.
Re: Appending static arrays
On Monday, 17 July 2017 at 20:53:36 UTC, H. S. Teoh wrote: See the working implementation in my latest post on this thread. That version supports CommonType, and works correctly with empty tuples as well. T Thanks.
Re: Appending static arrays
On Mon, Jul 17, 2017 at 08:28:12PM +, Nordlöw via Digitalmars-d-learn wrote: [...] > I had to special-case foreach body for `i == 0` since `sumOfLengths` > couldn't instantiate with empty tuple `()`. > > Further, should `concat` support `CommonType`? That is, should > > int[2] x = [1, 2]; > const double[2] y = [3, 4]; > auto z = concat(x, y); > static assert(is(typeof(z) == double[4])); > > be allowed? See the working implementation in my latest post on this thread. That version supports CommonType, and works correctly with empty tuples as well. T -- Trying to define yourself is like trying to bite your own teeth. -- Alan Watts
Profiling Windows App and DLL
Is there a known limitation in profiling these or am I doing something wrong? When I try to run my application from VisualD (x64 build) with -profile switch I just get Access Violation reported on WinMain function (actual declaration, it doesn't enter its body). If I build it with dub build --build=profile and then try to run it nothing happens, like it doesn't run at all. If I only add -profile switch on DLL part of the application I get the same Access Violation on DllMain. I also tried "Very Sleepy" profiler but it only shows symbols for main application and not for the DLL that it loads which is also built with debug info.
Re: Appending static arrays
On Monday, 17 July 2017 at 20:10:31 UTC, H. S. Teoh wrote: On Mon, Jul 17, 2017 at 08:11:03PM +, Nordlöw via Digitalmars-d-learn wrote: [...] Does this have a place in Phobos? Never know till you try. :-D If so, - under what name: append, concat or cat? I'd vote for concat. - where: std.algorithm or std.array? std.array, IMO, since it's specific to static arrays. T Made some adjustments with working unittest and put it up on https://github.com/nordlow/phobos-next/blob/master/src/algorithm_ex.d#L2467 I had to special-case foreach body for `i == 0` since `sumOfLengths` couldn't instantiate with empty tuple `()`. Further, should `concat` support `CommonType`? That is, should int[2] x = [1, 2]; const double[2] y = [3, 4]; auto z = concat(x, y); static assert(is(typeof(z) == double[4])); be allowed?
Re: Appending static arrays
On Mon, Jul 17, 2017 at 08:11:03PM +, Nordlöw via Digitalmars-d-learn wrote: [...] > Does this have a place in Phobos? Never know till you try. :-D > If so, > > - under what name: append, concat or cat? I'd vote for concat. > - where: std.algorithm or std.array? std.array, IMO, since it's specific to static arrays. T -- Without outlines, life would be pointless.
Re: Appending static arrays
On Monday, 17 July 2017 at 19:11:26 UTC, H. S. Teoh wrote: Hmm, since we already have sumOfLengths available at compile-time, what about: T[sumOfLengths!StaticArrays] append(StaticArrays...)(StaticArrays arrays) if (allSatisfy!(isStaticArray, StaticArrays)) { typeof(return) result = void; foreach (i, a; 0 .. arrays.length) { enum offset = sumOfLengths!(arrays[0 .. i]); result[offset .. offset + a.length] = a[]; } return result; } T Thanks, I'll try this! Does this have a place in Phobos? If so, - under what name: append, concat or cat? - where: std.algorithm or std.array?
Re: Appending static arrays
OK, here's an actual, compilable, runnable version: import std.algorithm : sum; import std.meta : allSatisfy, staticMap; import std.range : only; import std.traits : CommonType, isStaticArray; alias Elem(A : E[n], E, size_t n) = E; enum Length(A) = A.length; enum sumLengths(A...) = sum(only(0, staticMap!(Length, A))); CommonType!(staticMap!(Elem, A))[sumLengths!A] append(A...)(A arrays) if (allSatisfy!(isStaticArray, A)) { typeof(return) result = void; foreach (i, a; arrays) { enum offset = sumLengths!(A[0 .. i]); result[offset .. offset + a.length] = a[]; } return result; } @nogc unittest { int[2] a = [ 1, 2 ]; int[3] b = [ 3, 4, 5 ]; int[4] c = [ 6, 7, 8, 9 ]; auto d = append(a, b, c); assert(is(typeof(d) == int[9])); assert(d == [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]); } T -- Be in denial for long enough, and one day you'll deny yourself of things you wish you hadn't.
Re: Appending static arrays
On Monday, 17 July 2017 at 18:54:31 UTC, Stefan Koch wrote: we have special code in the compiler to optimize a ~ b ~ c. Interesting, can you elaborate on what you mean with "optimize". In that case, is there a reason why int x[2]; int y[2]; int z[x.length + y.length] = x ~ y; isn't @nogc?
Re: Appending static arrays
On Monday, 17 July 2017 at 18:54:31 UTC, Stefan Koch wrote: So all you need to do make it so auto cat(T[]...)(T args) { T[] result; mixin(() { string mix = `result = `; foreach(i;args.length) { mix ~= `args[` ~ itos(i) ~ `] ~`; } mix[$-1] = ';'; return mix; }()); return result; } that should do it :) Thanks, but I wan't a @nogc-variant, which was posted below.
Re: Appending static arrays
On Mon, Jul 17, 2017 at 12:01:48PM -0700, H. S. Teoh via Digitalmars-d-learn wrote: [...] > template sumOfLengths(A...) > if (A.length > 0) > { > static if (A.length == 1) > enum sumOfLengths = A[0].length; > else > enum sumOfLengths = A[0].length + sumOfLengths!(A[1 .. > $]); > } > > T[sumOfLengths!StaticArrays] append(StaticArrays...)(StaticArrays > arrays) > if (/* insert static array constraints here */) > { > typeof(return) result = void; > size_t offset = 0; > foreach (a; arrays) > { > result[offset .. offset + a.length] = a[]; > } > return result; > } [...] Hmm, since we already have sumOfLengths available at compile-time, what about: T[sumOfLengths!StaticArrays] append(StaticArrays...)(StaticArrays arrays) if (allSatisfy!(isStaticArray, StaticArrays)) { typeof(return) result = void; foreach (i, a; 0 .. arrays.length) { enum offset = sumOfLengths!(arrays[0 .. i]); result[offset .. offset + a.length] = a[]; } return result; } T -- People say I'm indecisive, but I'm not sure about that. -- YHL, CONLANG
Re: Appending static arrays
On Mon, Jul 17, 2017 at 12:01:48PM -0700, H. S. Teoh via Digitalmars-d-learn wrote: [...] > T[sumOfLengths!StaticArrays] append(StaticArrays...)(StaticArrays > arrays) > if (/* insert static array constraints here */) > { > typeof(return) result = void; > size_t offset = 0; > foreach (a; arrays) > { > result[offset .. offset + a.length] = a[]; Argh, forgot this important line: offset += a.length; > } > return result; > } [...] T -- Marketing: the art of convincing people to pay for what they didn't need before which you fail to deliver after.
Re: Appending static arrays
On Mon, Jul 17, 2017 at 05:38:23PM +, Nordlöw via Digitalmars-d-learn wrote: > I'm want to define a specialization of `append()` that takes only > static arrays as inputs and returns a static array being the sum of > the lengths of the inputs. > > Have anybody already implemented this? > > If not, I'm specifically interested in how to most conveniently infer > the length (as an enum) of the returned static array from the > `.length`s of inputs (which of course must be enum-values too). Hmm. What about: template sumOfLengths(A...) if (A.length > 0) { static if (A.length == 1) enum sumOfLengths = A[0].length; else enum sumOfLengths = A[0].length + sumOfLengths!(A[1 .. $]); } T[sumOfLengths!StaticArrays] append(StaticArrays...)(StaticArrays arrays) if (/* insert static array constraints here */) { typeof(return) result = void; size_t offset = 0; foreach (a; arrays) { result[offset .. offset + a.length] = a[]; } return result; } T -- IBM = I'll Buy Microsoft!
Re: Appending static arrays
On 07/17/2017 08:35 PM, Nordlöw wrote: Thanks, but I'm talking about the variadic case where the number of input arguments are unknown (>= 2) where the function header looks something like import std.traits : allSatisfy, isStaticArray; auto append(R, Args...)(auto ref Args args) if (args.length >= 2 && allSatisfy!(isStaticArray, Args)) // TODO all ElementTypes have CommonType { // ... } I see. Here's what I could come up with: import std.traits : allSatisfy, isStaticArray; auto append(Args...)(auto ref Args args) if (args.length >= 2 && allSatisfy!(isStaticArray, Args)) { import std.algorithm : sum; import std.meta : staticMap; import std.range: ElementType; import std.traits : CommonType; enum staticArrayLength(A : E[n], E, size_t n) = n; alias E = ElementType!(Args[0]); static assert(is(CommonType!(staticMap!(ElementType, Args)) : E)); enum lengths = staticMap!(staticArrayLength, Args); E[sum([lengths])] result; foreach (i, length; lengths) { enum offset = sum!(size_t[])([lengths[0 .. i]]); result[offset .. offset + length] = args[i]; } return result; } @nogc unittest { int[3] a = [1, 2, 3]; const int[4] b = [4, 5, 6, 7]; immutable int[5] c = [8, 9, 10, 11, 12]; auto r = append(a, b, c); assert(r == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); static assert(is(typeof(r) == int[12])); }
Re: Appending static arrays
On Monday, 17 July 2017 at 18:38:16 UTC, Nordlöw wrote: On Monday, 17 July 2017 at 17:46:42 UTC, ag0aep6g wrote: int[n + m] result = a ~ b; Further, the expression `a ~ b` here will allocate and create a copy on the GC-heap before writing `result`. Maybe LDC optimizes away this now or in the future but DMD cannot. Yeah I know, kind of dumb but that's how it is currently. we have special code in the compiler to optimize a ~ b ~ c. So all you need to do make it so auto cat(T[]...)(T args) { T[] result; mixin(() { string mix = `result = `; foreach(i;args.length) { mix ~= `args[` ~ itos(i) ~ `] ~`; } mix[$-1] = ';'; return mix; }()); return result; } that should do it :)
Re: Appending static arrays
On Monday, 17 July 2017 at 17:38:23 UTC, Nordlöw wrote: I'm want to define a specialization of `append()` that takes only static arrays as inputs and returns a static array being the sum of the lengths of the inputs. Have anybody already implemented this? If not, I'm specifically interested in how to most conveniently infer the length (as an enum) of the returned static array from the `.length`s of inputs (which of course must be enum-values too). I just realized that I can use `std.meta.staticMap` to get the lengths but I still need to reduce them and there is no variant of `reduce` in `std.meta`. Why? Hasn't it been written yet? I know I can always write yet another recursive CT-function, say `SumOfLengths`, but I thought I'd try to reuse `std.meta` this time. :)
Re: Appending static arrays
On Monday, 17 July 2017 at 17:46:42 UTC, ag0aep6g wrote: int[n + m] result = a ~ b; Further, the expression `a ~ b` here will allocate and create a copy on the GC-heap before writing `result`. Maybe LDC optimizes away this now or in the future but DMD cannot. Yeah I know, kind of dumb but that's how it is currently.
Re: Appending static arrays
On Monday, 17 July 2017 at 17:46:42 UTC, ag0aep6g wrote: Like so? int[n + m] append(size_t n, size_t m)(int[n] a, int[m] b) { int[n + m] result = a ~ b; return result; } Thanks, but I'm talking about the variadic case where the number of input arguments are unknown (>= 2) where the function header looks something like import std.traits : allSatisfy, isStaticArray; auto append(R, Args...)(auto ref Args args) if (args.length >= 2 && allSatisfy!(isStaticArray, Args)) // TODO all ElementTypes have CommonType { // ... }
Re: Yesterday Visual D worked, today it does not!
On Monday, 17 July 2017 at 17:57:14 UTC, Enjoys Math wrote: I made a console app the other day and there were build options present. In the build options I had to specify the dmd2 executable directly. Then it worked (but that's another error). Today there are no build options! I tried creating a regular console app and a GDC/DMD console app. Okay, found them. You have to right-click on the project. Duh! :D
Yesterday Visual D worked, today it does not!
I made a console app the other day and there were build options present. In the build options I had to specify the dmd2 executable directly. Then it worked (but that's another error). Today there are no build options! I tried creating a regular console app and a GDC/DMD console app.
Re: Appending static arrays
On 07/17/2017 07:38 PM, Nordlöw wrote: I'm want to define a specialization of `append()` that takes only static arrays as inputs and returns a static array being the sum of the lengths of the inputs. Have anybody already implemented this? If not, I'm specifically interested in how to most conveniently infer the length (as an enum) of the returned static array from the `.length`s of inputs (which of course must be enum-values too). Like so? int[n + m] append(size_t n, size_t m)(int[n] a, int[m] b) { int[n + m] result = a ~ b; return result; }
Appending static arrays
I'm want to define a specialization of `append()` that takes only static arrays as inputs and returns a static array being the sum of the lengths of the inputs. Have anybody already implemented this? If not, I'm specifically interested in how to most conveniently infer the length (as an enum) of the returned static array from the `.length`s of inputs (which of course must be enum-values too).
Re: (char* str) is not callable using argument types (string)
On 17/07/2017 2:47 PM, Zaheer Ahmed wrote: I am Developing an Operating System in D Language and when want to Develop my writeln("Zaheer"); function, I pass String "Zaheer" and when receive, it says Error Error: function kernel.dwriteln (char* str) is not callable using argument types (string) I Tried to cast but still stuck. In C and C++ I built this 2 times but D is giving this. string == array array/slice = length + pointer [0] size_t length [1] [char|wchar|dchar] ptr char* pointer; string str = pointer[0 .. length];
(char* str) is not callable using argument types (string)
I am Developing an Operating System in D Language and when want to Develop my writeln("Zaheer"); function, I pass String "Zaheer" and when receive, it says Error Error: function kernel.dwriteln (char* str) is not callable using argument types (string) I Tried to cast but still stuck. In C and C++ I built this 2 times but D is giving this.
Re: Avoid if statements for checking neighboring indexes in a 2D array
As for the problem itself, it can be solved without finding connected components. I won't post the solution right away because it is potentially a spoiler. See http://codeforces.com/blog/entry/53268 for problem analysis (828B) and http://codeforces.com/contest/828/submission/28637184 for an example implementation in D. Ivan Kazmenko. Wow I understand the question wrongly than, B W WWWBB WWBWW I excepted there should be two B squares here. One 3*3 and another 1*1. And my answer would be 6 instead 12 here. Yes if there can be only one black square than my solution would be much more simple. One good thing about D community is when I ask a question here you guys are really nice that I get inspired. Thanks Ivan
Re: Idiomatic way of writing nested loops?
On Monday, 17 July 2017 at 11:55:47 UTC, Anton Fediushin wrote: Thank you! I knew it is in the library! So, `parallel` will work just fine with this function, isn't it? Yes
Re: Idiomatic way of writing nested loops?
On Monday, 17 July 2017 at 11:32:45 UTC, Sebastiaan Koppe wrote: On Monday, 17 July 2017 at 11:07:35 UTC, Anton Fediushin wrote: Hello! What is the best way of rewriting this code in idiomatic D manner? https://dlang.org/phobos/std_algorithm_setops.html#.cartesianProduct Thank you! I knew it is in the library! So, `parallel` will work just fine with this function, isn't it?
Re: Avoid if statements for checking neighboring indexes in a 2D array
On Monday, 17 July 2017 at 07:14:26 UTC, Andrea Fontana wrote: Probably using ndslice library could help you! Unfortunately, that's not possible on most online contest platforms like Codeforces. For each programming language and compiler available, only the most basic package is usually installed on the server. There is a mix of reasons involved (historical, maintenance, choice of libraries, more equality for different languages, etc.). That means, e.g., no numpy for Python, no Boost for C++, and no third-party libraries for D. Ivan Kazmenko.
Re: Avoid if statements for checking neighboring indexes in a 2D array
On Sunday, 16 July 2017 at 21:50:19 UTC, kerdemdemir wrote: Process(row-1,column-1, maxrow, maxcolumn); Process(row,column-1, maxrow, maxcolumn); Process(row+1,column-1, maxrow, maxcolumn); Process(row-1,column, maxrow, maxcolumn); Process(row+1,column, maxrow, maxcolumn); Process(row-1,column+1, maxrow, maxcolumn); Process(row,column+1, maxrow, maxcolumn); Process(row-1,column+1, maxrow, maxcolumn); One of "row-1,column+1" should actually be "row+1,column+1". That's where the mentioned ways help. As for the problem itself, it can be solved without finding connected components. I won't post the solution right away because it is potentially a spoiler. See http://codeforces.com/blog/entry/53268 for problem analysis (828B) and http://codeforces.com/contest/828/submission/28637184 for an example implementation in D. Ivan Kazmenko.
Re: Idiomatic way of writing nested loops?
On Monday, 17 July 2017 at 11:07:35 UTC, Anton Fediushin wrote: Hello! What is the best way of rewriting this code in idiomatic D manner? https://dlang.org/phobos/std_algorithm_setops.html#.cartesianProduct
Idiomatic way of writing nested loops?
Hello! What is the best way of rewriting this code in idiomatic D manner? -- foreach(a; ["foo", "bar"]) { foreach(b; ["baz", "foz", "bof"]) { foreach(c; ["FOO", "BAR"]) { // Some operations on a, b and c } } } -- Every array has at least 1 element, and adding/removing new "nested loops" should be as easy as possible. Also, I have a question about running this in parallel: if I want to use nested loops with `parallel` from `std.parallelism`, should I add `parallel` to every loop like this? -- foreach(a; ["foo", "bar"].parallel) { foreach(b; ["baz", "foz", "bof"].parallel) { foreach(c; ["FOO", "BAR"].parallel) { // Some operations on a, b and c } } } -- I am worried about running thousands of threads, because in this case first `parallel` runs 2 tasks, every task runs 3 tasks and every task runned inside a task runs 2 more tasks. So, how to write this in idiomatic D manner and run it _if possible_ in parallel?
How to debug in vscode with mago-mi?
Could anyone show me how to debug in vscode with mago-mi? I have installed vscode with Native Debug, SDLang. I have tried dlang-vscode and code-d. My tasks.json: { "version": "2.0.0", "command": "dub", "type": "shell", "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" }, "tasks" : [ { "taskName": "build", "group": { "kind": "build", "isDefault": true }, "args": [], //Pattern match DMD error messages "problemMatcher": { "owner": "d", "fileLocation": ["relative", "${workspaceRoot}"], "pattern": { "regexp": "^(.*)\\((\\d+),(\\d+)\\):\\s+(Warning|Error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } } } ] } And my launch.json: { "version": "0.2.0", "configurations": [ { "name": "Debug", "type": "mago-mi", "request": "launch", "target": "./bin/app.exe", "cwd": "${workspaceRoot}" } ] } I have placed mago-mi.exe in PATH. I can build app.exe successfully. But when I click the debug button, nothing happen.
Re: Avoid if statements for checking neighboring indexes in a 2D array
On Sunday, 16 July 2017 at 10:37:39 UTC, kerdemdemir wrote: My goal is to find connected components in a 2D array for example finding connected '*' chars below. You can also use a queue to avoid recursion that should improve performances and readibility. Probably using ndslice library could help you! Andrea