Re: std.process.execute without capturing stderr?
On Thursday, 20 September 2018 at 14:10:44 UTC, Steven Schveighoffer wrote: Hm... 2.079.0 had it: Sorry, I made a mistake while testing and after I found out, that it was not available in the documentation at dpldocs.info I concluded, that it must be a really new feature. But now it seems to me, that dpldocs is outdated a little bit, isn't it? Meanwhile I've got the latest version of dmd and made it working.
Re: Simple parallel foreach and summation/reduction
On Thursday, 20 September 2018 at 05:51:17 UTC, Neia Neutuladh wrote: On Thursday, 20 September 2018 at 05:34:42 UTC, Chris Katko wrote: All I want to do is loop from 0 to [constant] with a for or foreach, and have it split up across however many cores I have. You're looking at std.parallelism.TaskPool, especially the amap and reduce functions. Should do pretty much exactly what you're asking. auto taskpool = new TaskPool(); taskpool.reduce!((a, b) => a + b)(iota(1_000_000_000_000L)); I get "Error: template instance `reduce!((a, b) => a + b)` cannot use local __lambda1 as parameter to non-global template reduce(functions...)" when trying to compile that using the online D editor with DMD and LDC. Any ideas?
Re: Simple parallel foreach and summation/reduction
On Friday, 21 September 2018 at 07:25:17 UTC, Chris Katko wrote: I get "Error: template instance `reduce!((a, b) => a + b)` cannot use local __lambda1 as parameter to non-global template reduce(functions...)" when trying to compile that using the online D editor with DMD and LDC. Any ideas? That's a long standing issue: https://issues.dlang.org/show_bug.cgi?id=5710 Using a string for the expression does work though: ``` import std.stdio, std.parallelism, std.range; void main() { taskPool.reduce!"a + b"(iota(1_000L)).writeln; } ```
Re: Simple parallel foreach and summation/reduction
On 09/21/2018 12:25 AM, Chris Katko wrote: On Thursday, 20 September 2018 at 05:51:17 UTC, Neia Neutuladh wrote: On Thursday, 20 September 2018 at 05:34:42 UTC, Chris Katko wrote: All I want to do is loop from 0 to [constant] with a for or foreach, and have it split up across however many cores I have. You're looking at std.parallelism.TaskPool, especially the amap and reduce functions. Should do pretty much exactly what you're asking. auto taskpool = new TaskPool(); taskpool.reduce!((a, b) => a + b)(iota(1_000_000_000_000L)); I get "Error: template instance `reduce!((a, b) => a + b)` cannot use local __lambda1 as parameter to non-global template reduce(functions...)" when trying to compile that using the online D editor with DMD and LDC. Any ideas? You can use a free-standing function as a workaround, which is included in the following chapter that explains most of std.parallelism: http://ddili.org/ders/d.en/parallelism.html That chapter is missing e.g. the newly-added fold(): https://dlang.org/phobos/std_parallelism.html#.TaskPool.fold Ali
Converting a character to upper case in string
How can I properly convert a character, say, first one to upper case in a unicode correct manner? In which code level I should be working on? Grapheme? Or maybe code point is sufficient? There are few phobos functions like asCapitalized() none of which are what I want.
Re: Converting a character to upper case in string
On Friday, 21 September 2018 at 12:15:52 UTC, NX wrote: How can I properly convert a character, say, first one to upper case in a unicode correct manner? In which code level I should be working on? Grapheme? Or maybe code point is sufficient? There are few phobos functions like asCapitalized() none of which are what I want. I would probably go for std.utf.decode [1] to get the character and its length in code units, capitalize it, and concatenate the result with the rest of the string. [1] https://dlang.org/phobos/std_utf.html#.decode
Re: Converting a character to upper case in string
On Friday, 21 September 2018 at 12:15:52 UTC, NX wrote: How can I properly convert a character, say, first one to upper case in a unicode correct manner? In which code level I should be working on? Grapheme? Or maybe code point is sufficient? There are few phobos functions like asCapitalized() none of which are what I want. -- import std.conv : to; import std.stdio : writeln; import std.string : capitalize; import std.utf : decode; size_t index = 1; size_t oldIndex = index; auto theString = "hëllo, world"; auto firstLetter = theString.decode(index); auto result = theString[0 .. oldIndex] ~ capitalize(firstLetter.to!string) ~ theString[index .. $]; writeln(result); -- (This could be a lot prettier, but this seems to basically work)
Re: Converting a character to upper case in string
On Friday, 21 September 2018 at 13:32:54 UTC, NX wrote: On Friday, 21 September 2018 at 12:34:12 UTC, Laurent Tréguier wrote: I would probably go for std.utf.decode [1] to get the character and its length in code units, capitalize it, and concatenate the result with the rest of the string. [1] https://dlang.org/phobos/std_utf.html#.decode So by this I assume it is sufficient to work with dchars rather than graphemes? -- import std.stdio; import std.conv; import std.string; import std.uni; size_t index = 1; auto theString = "he\u0308llo, world"; auto theStringPart = theString[index .. $]; auto firstLetter = theStringPart.decodeGrapheme; auto result = theString[0 .. index] ~ capitalize(firstLetter[].text) ~ theString[index + graphemeStride(theString, index) .. $]; writeln(result); -- This will capitalize graphemes as a whole, and might be better than what I previously wrote.
Re: Converting a character to upper case in string
On Friday, 21 September 2018 at 12:34:12 UTC, Laurent Tréguier wrote: I would probably go for std.utf.decode [1] to get the character and its length in code units, capitalize it, and concatenate the result with the rest of the string. [1] https://dlang.org/phobos/std_utf.html#.decode So by this I assume it is sufficient to work with dchars rather than graphemes?
Re: Converting a character to upper case in string
On Friday, 21 September 2018 at 13:32:54 UTC, NX wrote: On Friday, 21 September 2018 at 12:34:12 UTC, Laurent Tréguier wrote: I would probably go for std.utf.decode [1] to get the character and its length in code units, capitalize it, and concatenate the result with the rest of the string. [1] https://dlang.org/phobos/std_utf.html#.decode So by this I assume it is sufficient to work with dchars rather than graphemes? From what I've tested; it seems sufficient. I might be wrong though, I'm no unicode expert. It might still be a good idea to have a look at grapheme related functions.
Re: std.process.execute without capturing stderr?
On Friday, 21 September 2018 at 06:08:39 UTC, berni wrote: Sorry, I made a mistake while testing and after I found out, that it was not available in the documentation at dpldocs.info I concluded, that it must be a really new feature. But now it seems to me, that dpldocs is outdated a little bit, isn't it? Oh yeah, I haven't updated Phobos on it for a while, my attention most this year has been the dub thingy. Just did though.
Get variables with call stack
Hello! I need to make a some sort of error report system for an application. I want to catch base Exception class instance and report call stack and with the call stack I want to report all variables with their values. There are a couple of services that make report using call stack and provides variables' values. Sentry.io, New Relic etc. I see how to get call stack, the book Adam Ruppe writes helps me. How to get all variables from every layer of call stack?
Re: "Error: function expected before (), not module *module* of type void
On Monday, 24 March 2008 at 17:41:11 UTC, Steven Schveighoffer wrote: I know you fixed the problem, but just an FYI, the reason is because when you import rollDice, you bring both rollDice the module and rollDice the function into the global namespace (which confuses the compiler 'cause it doesn't know what symbol you want to use). This is normally avoided in libraries by having a package tree. So for example, if you created everything in the subdirectory foo, and had your modules be: module foo.diceroller; import foo.rollDice; Then the import would import the module foo.rollDice, and the function rollDice, and the compiler would no longer be confused about what you are trying to call. IMO, this makes it difficult to write multi-file applications that live in one directory. It would be nice if this was changed... -Steve I know this thread is quite old but I still seem to be getting a similar error and don't understand how to resolve it. I currently have a program isPrime.d that I would like to reuse in other programs: isPrime.d: bool isPrime(int n) { // logic to check if n is prime } main.d: import isPrime; void main() { isPrime(x); } Both files are in the same directory. When compiling main.d, I get: Error: function expected before (), not module isPrime of type void I've tried changing the name of the function isPrime in isPrime.d to something else (as well as changing the name in the main program) but then I get an error similar to: In function `_Dmain': main.d:(.text._Dmain[_Dmain]+0x83): undefined reference to `_D7isPrime3isPFiZb' collect2: error: ld returned 1 exit status Error: linker exited with status 1 Thanks in advance.
Re: "Error: function expected before (), not module *module* of type void
On Saturday, 22 September 2018 at 01:51:33 UTC, Samir wrote: main.d: import isPrime; void main() { isPrime(x); } You probably shouldn't name a module the same as a member anyway, and it should also have two names, like "module myproject.isprime;" But the fix here is to just use the full name. import isPrime; void main() { isPrime.isPrime(x); // module_name.member_name } or change the import: import isPrime : isPrime; // specify you want the same-named member Both files are in the same directory. When compiling main.d, When compiling, be sure to pass both modules to it, or use the dmd -i if on a new version. dmd -i main.d or dmd main.d isPrime.d main.d:(.text._Dmain[_Dmain]+0x83): undefined reference to `_D7isPrime3isPFiZb' this likely means you forgot to compile in the isPrime module, so use the above dmd lines
Re: "Error: function expected before (), not module *module* of type void
On Saturday, 22 September 2018 at 01:58:57 UTC, Adam D. Ruppe wrote: You probably shouldn't name a module the same as a member anyway, and it should also have two names, like "module myproject.isprime;" But the fix here is to just use the full name. import isPrime; void main() { isPrime.isPrime(x); // module_name.member_name } or change the import: import isPrime : isPrime; // specify you want the same-named member Both files are in the same directory. When compiling main.d, When compiling, be sure to pass both modules to it, or use the dmd -i if on a new version. dmd -i main.d or dmd main.d isPrime.d main.d:(.text._Dmain[_Dmain]+0x83): undefined reference to `_D7isPrime3isPFiZb' this likely means you forgot to compile in the isPrime module, so use the above dmd lines Thanks for your help, Adam! Right after posting my question, I started reading this site: https://www.tutorialspoint.com/d_programming/d_programming_modules.htm Based on that and your recommendation, here is what I ended up doing: I changed the filename of isPrime.d to isprime.d and put that in the subdirectory func/: func/isprime.d: module func.isprime; bool isPrime(int n) { // check to see if n is prime } I then changed main.d to: import func.isprime; void main() { isPrime(x); } Finally, per your suggestion, I compiled it using: dmd -i main.d Thanks again!
Re: Get variables with call stack
On Friday, 21 September 2018 at 19:08:36 UTC, ANtlord wrote: Hello! I need to make a some sort of error report system for an application. I want to catch base Exception class instance and report call stack and with the call stack I want to report all variables with their values. There are a couple of services that make report using call stack and provides variables' values. Sentry.io, New Relic etc. I see how to get call stack, the book Adam Ruppe writes helps me. How to get all variables from every layer of call stack? The only way to do that would be using a debugger. The specifics of the solution would thus depend on the platform. On POSIX, it would probably mean getting gdb to print a detailed backtrace for your project. On Windows, you might be able to achieve this by spawning a thread which then uses dbgeng.dll to get a detailed stack trace.
Re: Simple parallel foreach and summation/reduction
On Saturday, 22 September 2018 at 02:13:58 UTC, Chris Katko wrote: On Friday, 21 September 2018 at 12:15:59 UTC, Ali Çehreli wrote: On 09/21/2018 12:25 AM, Chris Katko wrote: [...] You can use a free-standing function as a workaround, which is included in the following chapter that explains most of std.parallelism: http://ddili.org/ders/d.en/parallelism.html That chapter is missing e.g. the newly-added fold(): https://dlang.org/phobos/std_parallelism.html#.TaskPool.fold Ali Okay... so I've got it running. The problem is, it uses tons of RAM. In fact, proportional to the working set. T test(T)(T x, T y) { return x + y; } double monte(T)(T x) { double v = uniform(-1F, 1F); double u = uniform(-1F, 1F); if(sqrt(v*v + u*u) < 1.0) { return 1; }else{ return 0; } } auto taskpool = new TaskPool(); sum = taskpool.reduce!(test)( taskpool.amap!monte( iota(num) ) ); taskpool.finish(true); 100 becomes ~8MB 1000 becomes 80MB 1, I can't even run because it says "Exception: Memory Allocation failed" Also, when I don't call .finish(true) at the end, it just sits there forever (after running) like one of the threads won't terminate. Requiring a control-C. But the docs and examples don't seem to indicate I should need that...
Re: Get variables with call stack
On Saturday, 22 September 2018 at 05:43:53 UTC, Vladimir Panteleev wrote: The only way to do that would be using a debugger. The specifics of the solution would thus depend on the platform. On POSIX, it would probably mean getting gdb to print a detailed backtrace for your project. On Windows, you might be able to achieve this by spawning a thread which then uses dbgeng.dll to get a detailed stack trace. One thing to note: only variables in stack frames since the most top-level exception block will be visible (so, you'll also need to disable D runtime's standard exception handler). The reason for this is that exceptions do not capture the entire stack, but extract only a stack trace during instantiation, so to get the entire stack, you'd need to breakpoint _d_throw or such, but at that point you don't know if you're within an exception frame ready to catch the thrown exception. In short: there is no easy way, in the general sense.
Re: Get variables with call stack
On Saturday, 22 September 2018 at 05:49:05 UTC, Vladimir Panteleev wrote: In short: there is no easy way, in the general sense. If you can find something that achieves what you need in C++, there's a good chance that it would work to some extent (or could be adapted with reasonable effort) for D, too. D debug information has much in common with C++, however exceptions vary from platform to platform.
Re: Simple parallel foreach and summation/reduction
On Friday, 21 September 2018 at 12:15:59 UTC, Ali Çehreli wrote: On 09/21/2018 12:25 AM, Chris Katko wrote: On Thursday, 20 September 2018 at 05:51:17 UTC, Neia Neutuladh wrote: On Thursday, 20 September 2018 at 05:34:42 UTC, Chris Katko wrote: All I want to do is loop from 0 to [constant] with a for or foreach, and have it split up across however many cores I have. You're looking at std.parallelism.TaskPool, especially the amap and reduce functions. Should do pretty much exactly what you're asking. auto taskpool = new TaskPool(); taskpool.reduce!((a, b) => a + b)(iota(1_000_000_000_000L)); I get "Error: template instance `reduce!((a, b) => a + b)` cannot use local __lambda1 as parameter to non-global template reduce(functions...)" when trying to compile that using the online D editor with DMD and LDC. Any ideas? You can use a free-standing function as a workaround, which is included in the following chapter that explains most of std.parallelism: http://ddili.org/ders/d.en/parallelism.html That chapter is missing e.g. the newly-added fold(): https://dlang.org/phobos/std_parallelism.html#.TaskPool.fold Ali Okay... so I've got it running. The problem is, it uses tons of RAM. In fact, proportional to the working set. T test(T)(T x, T y) { return x + y; } double monte(T)(T x) { double v = uniform(-1F, 1F); double u = uniform(-1F, 1F); if(sqrt(v*v + u*u) < 1.0) { return 1; }else{ return 0; } } auto taskpool = new TaskPool(); sum = taskpool.reduce!(test)( taskpool.amap!monte( iota(num) ) ); taskpool.finish(true); 100 becomes ~8MB 1000 becomes 80MB 1, I can't even run because it says "Exception: Memory Allocation failed"
Re: Converting a character to upper case in string
On Friday, 21 September 2018 at 12:15:52 UTC, NX wrote: How can I properly convert a character, say, first one to upper case in a unicode correct manner? In which code level I should be working on? Grapheme? Or maybe code point is sufficient? There are few phobos functions like asCapitalized() none of which are what I want. Use `asCapitalized` to capitalize the first letter or use something like this: import std.conv; import std.range; import std.stdio; import std.uni; void main(string[] args) { string input = "noe\u0308l"; int index= 2; auto graphemes= input.byGrapheme.array; string upperCased = [graphemes[index]].byCodePoint.text.toUpper; graphemes[index] = upperCased.decodeGrapheme; string output= graphemes.byCodePoint.text; writeln(output); }