Re: Code Review
On Sunday, 10 December 2017 at 07:03:30 UTC, Venkat wrote: Can anybody take a look at this checkin ? The import files are a noise, please ignore them and take a look at the *.d files. Changes I made. - Tried to remove __va_argsave as it doesn't compile with it. - Tried to replace no arg opCall() with a factory function. https://bitbucket.org/vra5107/djni/pull-requests/1/code-now-compiles-removed-__va_argsave-and/diff Thanks in advance Venkat I should add that the source builds fine and I was able to write a couple of rudimentary programs using the library built from the sources.
Code Review
Can anybody take a look at this checkin ? The import files are a noise, please ignore them and take a look at the *.d files. Changes I made. - Tried to remove __va_argsave as it doesn't compile with it. - Tried to replace no arg opCall() with a factory function. https://bitbucket.org/vra5107/djni/pull-requests/1/code-now-compiles-removed-__va_argsave-and/diff Thanks in advance Venkat
Re: Seed Value for reduce function
On 12/09/2017 06:19 PM, Vino wrote: Tried with fold even then it is failing same as reduce with the same error when there is empty folder auto SdFiles = Array!ulong(dirEntries(d, SpanMode.depth).map!(a => a.size).fold!((a,b) => a + b))[].filter!(a => a > Size); When no seed value is specified, fold (and reduce) take the first element as the seed. When the range is empty, then there is a run-time failure: object.Exception@/usr/include/dmd/phobos/std/algorithm/iteration.d(2794): Cannot reduce an empty input range w/o an explicit seed value. The solution is to provide an explicit seed value: import std.container; import std.file; import std.algorithm; void main() { const d = "/tmp/empty_folder"; const Size = 42; auto SdFiles = Array!ulong(dirEntries(d, SpanMode.depth).map!(a => a.size).fold!((a,b) => a + b)(size_t(0)))[].filter!(a => a > Size); } I chose size_t(0) but you can use a variable, size_t.init, etc. Ali
Re: Seed Value for reduce function
On Sunday, 10 December 2017 at 01:42:46 UTC, Jonathan M Davis wrote: On Sunday, December 10, 2017 00:39:07 Vino via Digitalmars-d-learn wrote: [...] So, basically, you're trying to use reduce with UFCS, and that doesn't work in the case where you want to provide an explicit seed value? In that's the case, then use fold: https://dlang.org/phobos/std_algorithm_iteration.html#fold reduce predates UFCS in the language, so there wasn't really a reason to prefer having the range as the first argument at the time. fold has since been added to fix that problem. They're identical except for the order of arguments (in fact, fold just calls reduce internally). - Jonathan M Davis Hi Jonathan, Tried with fold even then it is failing same as reduce with the same error when there is empty folder auto SdFiles = Array!ulong(dirEntries(d, SpanMode.depth).map!(a => a.size).fold!((a,b) => a + b))[].filter!(a => a > Size); From, Vino.B
Re: Seed Value for reduce function
On Sunday, December 10, 2017 00:39:07 Vino via Digitalmars-d-learn wrote: > Hi All, > >Request you help on how to add the seed value for the reduce > function, below is the scnerio > > Program 1 : Works > > import std.algorithm; > void main () { > int[] ara = [1,2 ,3]; > auto sum1 = ara.reduce!((a,b) => a + b); > writeln(sum1); > } > > Program 2: Works > void main () { > int[] arrb = []; > auto sum1 = reduce!((a,b) => a + b)(0 , arb); > writeln(sum1); > } > > So how to add seed value for the below code as same as program 1 > without calling the seed value and array at the end rather than > calling it as arc.reduce!((a,b) => a + b); > > void main () { > int[] arc = []; > auto sum1 = arc.reduce!((a,b) => a + b); > writeln(sum1); > } So, basically, you're trying to use reduce with UFCS, and that doesn't work in the case where you want to provide an explicit seed value? In that's the case, then use fold: https://dlang.org/phobos/std_algorithm_iteration.html#fold reduce predates UFCS in the language, so there wasn't really a reason to prefer having the range as the first argument at the time. fold has since been added to fix that problem. They're identical except for the order of arguments (in fact, fold just calls reduce internally). - Jonathan M Davis
Re: Question for compiler gurus about compile-time strings
On Saturday, December 09, 2017 23:38:46 Stefan Koch via Digitalmars-d-learn wrote: > On Saturday, 9 December 2017 at 18:45:18 UTC, Steven > > Schveighoffer wrote: > > I was thinking that all strings generated at compile-time have > > a null-terminator added. But then I thought, wait, maybe that's > > only specifically for string literals. > > > > What is the true answer? If you generate a string, let's say > > via a CTFE call, does it have a null terminator? > > > > -Steve > > The results of CTFE calls are literals. Therefore they are > treated the same. > Thus the answer to your question is yes. > > However there might be rare corner-cases in which the null > termiantor is not there. > Should this happen please file a bug and sent me a mail > specifically. My initial reaction was that of course the null terminator wouldn't be there if it wasn't an actual string literal, but I tested it, and it was, but then when I thought it through some more, there really isn't any difference in the generated string between these options: enum str = "hello world"; enum str = "hello" ~ " world"; string foo() { string retval = "hello wor"; retval ~= 'l'; retval ~= 'd'; return retval; } enum str = foo(); In all cases, the compiler has to execute what's on the right-hand side to generate the value, and in all cases, it would go where string literals go in the binary, because there really isn't any difference from the compiler's perspective at that point (at least not from what I understand). Aside from the function call case, it probably even generates pretty much the same binary (whether the function call case would depend on whether the linker removed the function from the binary). I tried to think of a case where the result wouldn't be equivalent to a string literal, and I couldn't think of any. I didn't really want to make any statements about what was guaranteed though, since I'm not a compiler dev and not very familiar with the compiler internals. - Jonathan M Davis
Re: Seed Value for reduce function
On Sunday, 10 December 2017 at 00:39:07 UTC, Vino wrote: Hi All, Request you help on how to add the seed value for the reduce function, below is the scnerio Program 1 : Works import std.algorithm; void main () { int[] ara = [1,2 ,3]; auto sum1 = ara.reduce!((a,b) => a + b); writeln(sum1); } Program 2: Works void main () { int[] arrb = []; auto sum1 = reduce!((a,b) => a + b)(0 , arb); writeln(sum1); } So how to add seed value for the below code as same as program 1 without calling the seed value and array at the end rather than calling it as arc.reduce!((a,b) => a + b); void main () { int[] arc = []; auto sum1 = arc.reduce!((a,b) => a + b); writeln(sum1); } From, Vino.B Another example: The below code does errors out with the below error in there are any empty folders, else it works fine. import std.stdio; import std.file; import std.container; import std.algorithm; ulong Size = 10; auto SdFiles = Array!ulong(dirEntries("C:\\Temp\\BACKUP", SpanMode.depth).map!(a => a.size).reduce!((a,b) => a + b))[].filter!(a => a > Size); Error: object.Exception@C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(2794): Cannot reduce an empty input range w/o an explicit seed value. From, Vino.B
Seed Value for reduce function
Hi All, Request you help on how to add the seed value for the reduce function, below is the scnerio Program 1 : Works import std.algorithm; void main () { int[] ara = [1,2 ,3]; auto sum1 = ara.reduce!((a,b) => a + b); writeln(sum1); } Program 2: Works void main () { int[] arrb = []; auto sum1 = reduce!((a,b) => a + b)(0 , arb); writeln(sum1); } So how to add seed value for the below code as same as program 1 without calling the seed value and array at the end rather than calling it as arc.reduce!((a,b) => a + b); void main () { int[] arc = []; auto sum1 = arc.reduce!((a,b) => a + b); writeln(sum1); } From, Vino.B
Re: Question for compiler gurus about compile-time strings
On Saturday, 9 December 2017 at 18:45:18 UTC, Steven Schveighoffer wrote: I was thinking that all strings generated at compile-time have a null-terminator added. But then I thought, wait, maybe that's only specifically for string literals. What is the true answer? If you generate a string, let's say via a CTFE call, does it have a null terminator? -Steve The results of CTFE calls are literals. Therefore they are treated the same. Thus the answer to your question is yes. However there might be rare corner-cases in which the null termiantor is not there. Should this happen please file a bug and sent me a mail specifically.
Re: Get pointer or reference of an element in Array(struct)
On Saturday, 9 December 2017 at 06:46:27 UTC, Arun Chandrasekaran wrote: On Saturday, 9 December 2017 at 06:38:46 UTC, anonymous wrote: On Saturday, 9 December 2017 at 06:15:16 UTC, Arun Chandrasekaran wrote: Is there a way to get the pointer or reference of an element in Array(T)? [...] auto d2 = gallery[0]; auto d2 = &gallery[0]; Thanks. Just curious why reference can't be obtained here. Saves nasty null checks in most places. In D, structs are passed by value unless otherwise specified. Using a class will yield the expected result.
Re: Directory Size
On Friday, 8 December 2017 at 19:10:09 UTC, vino wrote: On Thursday, 7 December 2017 at 12:19:00 UTC, Vino wrote: On Thursday, 7 December 2017 at 09:04:19 UTC, Vino wrote: [...] Hi Andrea, Was able to find a solution to the above issue by adding the replace function as below, the the code is working as expected, is there any chance of using parallel function as the file system contains about 500+ folders and the size of each folder ranges from 5GB - 500 GB so the run time of the above code is about an 2 hours. .map!(a => tuple(a[0].replace(",\\?\", ""), ((a[1].to!real)/ SGb ; From, Vino.B Hi All, Any update on the above request. From, Vino.B Hi Andrea, Your code is good, when compared to the performance between your code and my earlier code , my code seem to be much faster, my code execution takes 40 mins where as your code takes 2+ hours(no change in the environment) as in my code i have used parallel and tired to add the parallelism to your code but was not able to succeed. Hence i need to switch back to my code, in case if you find any method to add parallelism you your code please do let me know. Thank you very much for you help. From, Vino.B
Re: Get pointer or reference of an element in Array(struct)
On Saturday, 9 December 2017 at 06:46:27 UTC, Arun Chandrasekaran wrote: Thanks. Just curious why reference can't be obtained here. Saves nasty null checks in most places. D simply doesn't have a (C++-style) concept of references as part of the type. Arguments can be passed by reference - hence the `ref` keyword -, but "free" references don't exist in the language. The ref in foreach loop variables can be conceptually thought of as a parameter to the loop body as well. (For opApply-based iteration, the loop body indeed gets turned into a function; for "plain" iteration the compiler AST internally has special ref variables, but they are not visible to the language.) -David
Question for compiler gurus about compile-time strings
I was thinking that all strings generated at compile-time have a null-terminator added. But then I thought, wait, maybe that's only specifically for string literals. What is the true answer? If you generate a string, let's say via a CTFE call, does it have a null terminator? -Steve
Re: Check whether a file is empty.
On Friday, 8 December 2017 at 19:13:20 UTC, vino wrote: Hi, The code is same just copy pasted the code form Windows 7 into Windows 2003 and executed, in Windows 7 the log file is of size 0 where as in windows 2003 the log file is of size 2 byte where the log file in both the server is empty. From, Vino.B What do you mean exactly by empty log file? If the file looks blank in notepad it does not mean it's empty. What are those 2 bytes? Which program does create this file? It may work differently depending on the system or different versions are installed.
Re: Sort in return statement
On Saturday, 9 December 2017 at 14:49:28 UTC, Seb wrote: randomSample doesn't sort its returned range Not by design, however, because it samples in the order that the elements appear, *if* those elements are already sorted (whether by design or explicately sorted), then the results of the randomsample are also implicitly already sorted ;-)
Re: Sort in return statement
On Saturday, 9 December 2017 at 14:42:44 UTC, codephantom wrote: After lots of reading, and testing, I managed to get a simple, one liner ;-) (doesn't seem like .release is needed though.) FYI .release is only possible on a SortedRange and then yields the underlying range. randomSample doesn't sort its returned range, but I am glad to hear this worked for you.
Re: Sort in return statement
On Saturday, 9 December 2017 at 14:18:00 UTC, Seb wrote: Yeah, you are very welcome. It's a bit hidden in the docs: Yes. Thanks for that. After lots of reading, and testing, I managed to get a simple, one liner ;-) (doesn't seem like .release is needed though.) // --- auto draw8Numbers() { import std.meta : aliasSeqOf; import std.range : iota; import std.random : randomSample; return randomSample([ aliasSeqOf!(iota(1,46)) ], 8); } // ---
Re: Sort in return statement
On Saturday, 9 December 2017 at 14:05:36 UTC, rjframe wrote: On Sat, 09 Dec 2017 07:32:42 +, Seb wrote: Use .release to obtain the underlying array. No need to do another allocation! ``` numbers.take(8).sort.release; ``` I did not realize that was there; thanks. Yeah, you are very welcome. It's a bit hidden in the docs: https://dlang.org/phobos/std_range.html#SortedRange
Re: Sort in return statement
On Sat, 09 Dec 2017 07:32:42 +, Seb wrote: > > Use .release to obtain the underlying array. No need to do another > allocation! > > ``` > numbers.take(8).sort.release; > ``` I did not realize that was there; thanks.
Re: std.conv.to!string refuses to convert a char* to string.
Thank you, core.runtime.Runtime.initialize() fixed the issue. I am now able to use to!string as well. I found your posts and Ali Çehreli's posts on this subject. I think I have some understanding now.
Re: Implicit nothrow with -betterC
Thanks Adam, that cleared it up for me.
Re: std.conv.to!string refuses to convert a char* to string.
On Saturday, 9 December 2017 at 06:14:36 UTC, Venkat wrote: Thanks for the quick response. std.string.fromStringz did the trick. I am not sure what was the deal with to!string. Be careful with fromStringz. It doesn't allocate a new string, so the returned string can easily become corrupted if the C string it's referencing falls off the stack. As for your problem with to!string, it isn't actually to!string that's the issue. It appears to have something to do with the memory allocated from the GC. I assume this is a shared library. Random thoughts -- Have you initialized DRuntime? Have you registered the calling thread with the GC?