Re: regex with literal (ie automatically replace '(' with '\(', etc) )
ok, here it is: https://github.com/timotheecour/dtools/blob/master/dtools/util/util.d#L78 simplified implementation and added missing escape symbols. Any symbol missing? I was basing myself based on http://dlang.org/phobos/std_regex.html, table entry '\c where c is one of', but that was incomplete. I'm also noting that table entry 'any character except' is also incomplete. Technically any working escapeRegex would also function as a valid escapeRegexReplace, although it might be slightly faster to have a specialised one. not sure, because they escape differently (\$ vs $$). shall i do a pull request for std.regex? On Wed, May 29, 2013 at 8:32 PM, Diggory digg...@googlemail.com wrote: On Wednesday, 29 May 2013 at 23:33:30 UTC, timotheecour wrote: something like this, which we should have in std.regex: string escapeRegex(string a){ import std.string; enum transTable = ['[' : `\[`, '|' : `\|`, '*': `\*`, '+': `\+`, '?': `\?`, '(': `\(`, ')': `\)`]; return translate(a, transTable); } string escapeRegexReplace(string a){ import std.string; // enum transTable = ['$' : `$$`, '\\' : `\\`]; enum transTable = ['$' : `$$`]; return translate(a, transTable); } unittest{ string a=`asdf(def[ghi]+*|)`; assert(match(a,regex(**escapeRegex(a))).hit==a); string b=`$aa\/$ $$#@$\0$1#$@%#@%=+_`; auto s=replace(a,regex(escapeRegex(**a)),escapeRegexReplace(b)); assert(s==b); } That would be good (although you missed a few :P) Technically any working escapeRegex would also function as a valid escapeRegexReplace, although it might be slightly faster to have a specialised one.
Re: Duplicating multidimensional array
On 05/29/2013 10:22 AM, Joseph Rushton Wakeling wrote: I've been having some trouble trying to work out how to effectively duplicate a multidimensional array in a way that preserves type qualifiers. Templates preserve type qualifiers. So, as long as the return type is the same as the parameter type, then the type qualifiers should be preserved: T foo(T)(T x) { // ... } immutable int[][] x = [[10, 0, 0, 0], [0, 10, 0, 0], [0, 0, 10, 0], [0, 0, 0, 10]]; int[][] y = multidup(x); As long as it is a conversion, std.conv.to handles that case for you: import std.conv; // ... int[][] y = x.to!(int[][]); Done! :) Unfortunately, std.conv.to is not a general copy tool. It does not do anything when converting to the same type. In other words, when the target type is the same as the source type it is a no-op, not a copy. What do you think about the following recursive template solution? I have tested it only with arrays of int. :/ import std.stdio; import std.traits; import std.conv; // This is for one-dimensional arrays T multidup(T : E[], E)(T arr) if (!isArray!E) { T result; static if (is (E == immutable)) { // No need to copy immutable elements result = arr; } else { // Otherwise, we must make a copy result = arr.dup; } return result; } // This is for array of array types T multidup(T : E[], E)(T arr) if(isArray!E) { T result; static if (is (E == immutable)) { // No need to go deeper than an immutable layer result == arr; } else { foreach(row; arr) { result ~= multidup(row); } } return result; } unittest { { int[] a = [ 1, 2, 3 ]; auto b = multidup(a); assert(typeid(b) is typeid(a)); assert(a == b); } { immutable(int[]) a = [ 1, 2 ]; auto b = multidup(a); assert(typeid(b) == typeid(immutable(int)[])); assert(a == b); assert(a.ptr == b.ptr); } { immutable(int)[] a = [ 1, 2, 3 ]; auto b = multidup(a); assert(typeid(b) == typeid(a)); assert(a == b); assert(a.ptr == b.ptr); // assert(a[0].ptr == b[0].ptr); } { immutable(int)[][] a = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]; auto b = multidup(a); assert(typeid(b) == typeid(a)); assert(a == b); assert(a.ptr != b.ptr); foreach (i; 0 .. a.length) { assert(a[i].ptr == b[i].ptr); } } { alias Elem = const(int[])[]; const(int[])[][]a = [ [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ], [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ]; auto b = multidup(a); assert(typeid(b) == typeid(a)); assert(a == b); assert(a.ptr != b.ptr); foreach (i; 0 .. a.length) { assert(a[i].ptr != b[i].ptr); } } } void main() {} Is it usable in your situation? Ali
Put/watch/play/run dvd movie video to iPad 3
When you want to play the DVD movie on your beloved iPad 3, there will be a problem that there is no DVD drive in iPad 3. So where is a solution to help us playing DVD movie on iPad 3? iPad 3 supports 1080p mp4, so Aunsoft DVD to iPad 3 ripper can help you out on this matter to convert DVD to iPad 3 supported format. It is very simple to complete the whole conversion with this good DVD to iPad 3 Converter. Firstly you need sure that your PC could load the DVD disk. Download and install Aunsoft DVD Ripper software. Install and launch it, click the Load button to load the DVD into Aunsoft DVD movie to iPad 3 Converter, and then go to Format to select the iPad 3 option. For example: if you want keep the full HD movie from DVD to iPad 3, you can choose this iPad 3(New iPad) Full HD Video (*.mp4). If you do not want to have a very big output size, you can just check the main movie for the DVD to iPad 3 conversion. The last step is start to converting dvd to iPad 3 h.264 mp4 and a few minutes later, get the mp4 file on your iPad 3. Is it very easy, correct? http://camcorder-editor.com/converting-dvd-to-ipad-3-h-264-mp4-and-play-dvd-movies-on-new-ipad/
Re: regex with literal (ie automatically replace '(' with '\(', etc) )
On Thursday, 30 May 2013 at 06:50:06 UTC, Timothee Cour wrote: ok, here it is: https://github.com/timotheecour/dtools/blob/master/dtools/util/util.d#L78 simplified implementation and added missing escape symbols. Any symbol missing? I was basing myself based on http://dlang.org/phobos/std_regex.html, table entry '\c where c is one of', but that was incomplete. I'm also noting that table entry 'any character except' is also incomplete. Technically any working escapeRegex would also function as a valid escapeRegexReplace, although it might be slightly faster to have a specialised one. not sure, because they escape differently (\$ vs $$). According to this: http://dlang.org/phobos/std_regex.html#.replace you can use the same escape sequences for both (\c - c in the replacement string).
AnalyzeD
hi Someone know if AnalyzeD could to be used from command line ? i.e http://dconf.org/talks/rohe.html I failed to find AnalyzeD source code thanks
Re: Put/watch/play/run dvd movie video to iPad 3
Is it written in D ? Where is the Linux version ? 35$ for a stripped down dvd::rip ? Are you kidding ? Keep your ads for you please, you'll lose at this game here. camcorder like camelote (junk in French ?) Le 30/05/2013 10:12, daisy520 a écrit : When you want to play the DVD movie on your beloved iPad 3, there will be a problem that there is no DVD drive in iPad 3. So where is a solution to help us playing DVD movie on iPad 3? iPad 3 supports 1080p mp4, so Aunsoft DVD to iPad 3 ripper can help you out on this matter to convert DVD to iPad 3 supported format. It is very simple to complete the whole conversion with this good DVD to iPad 3 Converter. Firstly you need sure that your PC could load the DVD disk. Download and install Aunsoft DVD Ripper software. Install and launch it, click the Load button to load the DVD into Aunsoft DVD movie to iPad 3 Converter, and then go to Format to select the iPad 3 option. For example: if you want keep the full HD movie from DVD to iPad 3, you can choose this iPad 3(New iPad) Full HD Video (*.mp4). If you do not want to have a very big output size, you can just check the main movie for the DVD to iPad 3 conversion. The last step is start to converting dvd to iPad 3 h.264 mp4 and a few minutes later, get the mp4 file on your iPad 3. Is it very easy, correct? http://camcorder-editor.com/converting-dvd-to-ipad-3-h-264-mp4-and-play-dvd-movies-on-new-ipad/
Re: irrelevant compiler error messages: should stop semantic3 passes after 1st error is encountered
ping. I've posted a bugzilla entry http://d.puremagic.com/issues/show_bug.cgi?id=10177 and finally managed to reduce to a small test case. So will the proposed method improve error reporting? Is there any case where it might hurt? On Mon, May 20, 2013 at 3:24 PM, Timothee Cour thelastmamm...@gmail.comwrote: In a number of cases I get very large compile error messages after running rdmd main_module.d Upon inspection, running rdmd -v fun.d reveals that the irrelevant error messages are in different semantic3 passes: semantic3 module_with_relevant_error relevant error (eg: Error: no property 'x' for type 'y') semantic3 module_with_irrelevant_error irrelevant error (only coming from the fact that there was an error in a prior semantic pass) ... and the list goes on for large number of lines. Why not just start semantic3 passes at 1st error occurence ? or at least only show those by default? in all cases i've encountered all that was needed for me to see was the 1st faulty semantic3 pass, all other was irrelevant artifacts stemming from that.
Re: regex with literal (ie automatically replace '(' with '\(', etc) )
According to this: http://dlang.org/phobos/std_**regex.html#.replacehttp://dlang.org/phobos/std_regex.html#.replace you can use the same escape sequences for both (\c - c in the replacement string). Your suggestion does not work; try for yourself by replacing the $$ by \$ in my code. Is that a bug in std.regex' doc? eg: replace(,regex(``),`\$`); = invalid format string in regex replace However everything works fine with $$, see my code above. On Thu, May 30, 2013 at 1:14 AM, Diggory digg...@googlemail.com wrote: On Thursday, 30 May 2013 at 06:50:06 UTC, Timothee Cour wrote: ok, here it is: https://github.com/**timotheecour/dtools/blob/** master/dtools/util/util.d#L78https://github.com/timotheecour/dtools/blob/master/dtools/util/util.d#L78 simplified implementation and added missing escape symbols. Any symbol missing? I was basing myself based on http://dlang.org/phobos/std_**regex.htmlhttp://dlang.org/phobos/std_regex.html, table entry '\c where c is one of', but that was incomplete. I'm also noting that table entry 'any character except' is also incomplete. Technically any working escapeRegex would also function as a valid escapeRegexReplace, although it might be slightly faster to have a specialised one. not sure, because they escape differently (\$ vs $$). According to this: http://dlang.org/phobos/std_**regex.html#.replacehttp://dlang.org/phobos/std_regex.html#.replaceyou can use the same escape sequences for both (\c - c in the replacement string).
Re: Consume an entire range
Brad Anderson: import std.stdio, std.algorithm, std.array; void eat(R)(R r) { while(!r.empty) { r.front; r.popFront; } } void main() { size_t[dstring] dic; stdin.byLine .joiner( ) .array .splitter(' ') .filter!(w = !w.empty w !in dic) .map!(w = writeln(dic[w.idup] = dic.length, '\t', w)) .eat; } I would have prefered to not use joiner() but working with ranges of ranges of ranges (splitter() on each line) got a bit weird and confusing. Maybe here it's better to work on lines. Alternatively I don't know if you can read the whole input there. It's usually better to give only pure functions to filter/map, because in Bugzilla I've shown those higher order functions don't work well otherwise. So I prefer a terminal function that takes an impure function and returns nothing, something like: ... .filter!(w = !w.empty w !in dic) .forEach!((w) { writeln(dic[w.idup] = dic.length, '\t', w); }); Bye, bearophile
double vs real
Hello. I like that D exposes to me the real type to maximally utilize the machine's numerical precision. Since I am writing a program (currently in C++ but I am thinking of moving to D) that has to handle lots of fractional numbers (calculating offset curves and such) I am wondering whether/when I should real instead of double or even any arguments in favour of staying with double (compatibility with C/C++?). Any pointers appreciated please? I checked the FAQ but it doesn't seem to mention this. Thanks! -- Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा
and/or/not/xor operators
Hello. I have always loved the readability of C++'s and/or/not/xor word-like logical operators but It doesn't seem to be available in D. Isn't this possible in D? I tried doing: alias and ; import std.stdio ; void main () { writeln ( true and true ) ; } but I get errors: $ dmd foo.d foo.d(1): Error: basic type expected, not foo.d(1): Error: no identifier for declarator int foo.d(1): Error: semicolon expected to close alias declaration foo.d(1): Error: Declaration expected, not '' foo.d(7): Error: found 'and' when expecting ', Thanks. -- Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा
My first D program
Hello. I am new to D and come from some intermediate C/C++ plus some Python programming background. I currently have DMD 2.062 installed on my Kubuntu Raring 64-bit system. 1. Too big binary output? OK so I wrote my first Hello World program: #! /usr/bin/rdmd import std.stdio ; void main() { writeln ( Namaste Prapancha! ) ; } (so I'm a bit of a Sanskrit geek...) and when I save it as namaste.d, do chmod +x and run ./namaste.d, all is fine and I get the output. However I am somewhat taken aback to see the file size -- 335KiB for a simple Hello World? The equivalent C/C++ programs compiled with Clang without any -O options produce binaries of less than 10K! 2. No filename freedom? Next I wanted to go to another example but I like to keep my practice files in order, so I rename namaste.d to 01-namaste.d but I get the error: $ dmd 01-namaste.d 01-namaste.d: Error: module 01-namaste has non-identifier characters in filename, use module declaration instead Huh? Now my program *name* has to be a valid identifier in the language? So I can't have my filename contain a hyphen-minus or start with a digit, and only something like e01_namaste.d is permitted. Why is this? -- Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा
Re: regex with literal (ie automatically replace '(' with '\(', etc) )
30-May-2013 14:24, Timothee Cour пишет: According to this: http://dlang.org/phobos/std___regex.html#.replace http://dlang.org/phobos/std_regex.html#.replace you can use the same escape sequences for both (\c - c in the replacement string). Your suggestion does not work; try for yourself by replacing the $$ by \$ in my code. Is that a bug in std.regex' doc? eg: replace(,regex(``),`\$`); = invalid format string in regex replace Indeed replace format string is a different beast. I can't recall if I stolen the original std.regex or devised this $$ myself. By any rate replace(fmt, `\$`, $$) would work or the same with replace from std.string. So I feel it's a bit of stretch to include a function for such a narrow case. However everything works fine with $$, see my code above. On Thu, May 30, 2013 at 1:14 AM, Diggory digg...@googlemail.com mailto:digg...@googlemail.com wrote: On Thursday, 30 May 2013 at 06:50:06 UTC, Timothee Cour wrote: ok, here it is: https://github.com/__timotheecour/dtools/blob/__master/dtools/util/util.d#L78 https://github.com/timotheecour/dtools/blob/master/dtools/util/util.d#L78 simplified implementation and added missing escape symbols. Any symbol missing? I was basing myself based on http://dlang.org/phobos/std___regex.html http://dlang.org/phobos/std_regex.html, table entry '\c where c is one of', but that was incomplete. I'm also noting that table entry 'any character except' is also incomplete. Technically any working escapeRegex would also function as a valid escapeRegexReplace, although it might be slightly faster to have a specialised one. not sure, because they escape differently (\$ vs $$). According to this: http://dlang.org/phobos/std___regex.html#.replace http://dlang.org/phobos/std_regex.html#.replace you can use the same escape sequences for both (\c - c in the replacement string). -- Dmitry Olshansky
Re: My first D program
On Thu, 30 May 2013 12:13:19 +0100, Shriramana Sharma samj...@gmail.com wrote: Hello. I am new to D and come from some intermediate C/C++ plus some Python programming background. I currently have DMD 2.062 installed on my Kubuntu Raring 64-bit system. 1. Too big binary output? OK so I wrote my first Hello World program: #! /usr/bin/rdmd import std.stdio ; void main() { writeln ( Namaste Prapancha! ) ; } (so I'm a bit of a Sanskrit geek...) and when I save it as namaste.d, do chmod +x and run ./namaste.d, all is fine and I get the output. However I am somewhat taken aback to see the file size -- 335KiB for a simple Hello World? The equivalent C/C++ programs compiled with Clang without any -O options produce binaries of less than 10K! The D standard library is currently statically linked. This will change shortly/eventually. 2. No filename freedom? Next I wanted to go to another example but I like to keep my practice files in order, so I rename namaste.d to 01-namaste.d but I get the error: $ dmd 01-namaste.d 01-namaste.d: Error: module 01-namaste has non-identifier characters in filename, use module declaration instead Huh? Now my program *name* has to be a valid identifier in the language? So I can't have my filename contain a hyphen-minus or start with a digit, and only something like e01_namaste.d is permitted. Why is this? As the error says use module declaration instead. You need to add a module namaste; statement to the top of the file 01-namaste.d. D defaults the module name to the filename, but you can specify it when they differ. R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Re: and/or/not/xor operators
On Thursday, 30 May 2013 at 16:18:44 UTC, Shriramana Sharma wrote: Hello. I have always loved the readability of C++'s and/or/not/xor word-like logical operators but It doesn't seem to be available in D. Isn't this possible in D? I tried doing: No, it isn't available... Thank goodness! :)
Re: regex with literal (ie automatically replace '(' with '\(', etc) )
Your suggestion does not work; try for yourself by replacing the $$ by \$ in my code. Is that a bug in std.regex' doc? eg: replace(,regex(``),`\$`); = invalid format string in regex replace However everything works fine with $$, see my code above. Either the doc or the code should probably be changed then so they are consistent.
Re: regex with literal (ie automatically replace '(' with '\(', etc) )
30-May-2013 10:49, Timothee Cour пишет: ok, here it is: https://github.com/timotheecour/dtools/blob/master/dtools/util/util.d#L78 simplified implementation and added missing escape symbols. Any symbol missing? I was basing myself based on http://dlang.org/phobos/std_regex.html, table entry '\c where c is one of', but that was incomplete. I'm also noting that table entry 'any character except' is also incomplete. One thing missing that '.' that should become '\.'. Technically any working escapeRegex would also function as a valid escapeRegexReplace, although it might be slightly faster to have a specialised one. not sure, because they escape differently (\$ vs $$). shall i do a pull request for std.regex? Yes, please. It's was a blind spot for long time. Strictly speaking I think that a generic escaping routine would work: auto escape(S1, S2, C)(S1 src, S2 escapables, C escape='\\') if(isSomeString!S1 isSomeString!S2 isSomeChar!C) { } Do we have something like this in std.string? Then all we need is a convenience wrapper in std.regex? BTW unescape is as important. On Wed, May 29, 2013 at 8:32 PM, Diggory digg...@googlemail.com mailto:digg...@googlemail.com wrote: On Wednesday, 29 May 2013 at 23:33:30 UTC, timotheecour wrote: something like this, which we should have in std.regex: string escapeRegex(string a){ import std.string; enum transTable = ['[' : `\[`, '|' : `\|`, '*': `\*`, '+': `\+`, '?': `\?`, '(': `\(`, ')': `\)`]; return translate(a, transTable); } string escapeRegexReplace(string a){ import std.string; // enum transTable = ['$' : `$$`, '\\' : `\\`]; enum transTable = ['$' : `$$`]; return translate(a, transTable); } unittest{ string a=`asdf(def[ghi]+*|)`; assert(match(a,regex(__escapeRegex(a))).hit==a); string b=`$aa\/$ $$#@$\0$1#$@%#@%=+_`; auto s=replace(a,regex(escapeRegex(__a)),escapeRegexReplace(b)); assert(s==b); } That would be good (although you missed a few :P) Technically any working escapeRegex would also function as a valid escapeRegexReplace, although it might be slightly faster to have a specialised one. -- Dmitry Olshansky
Re: double vs real
On Thursday, 30 May 2013 at 16:18:44 UTC, Shriramana Sharma wrote: Hello. I like that D exposes to me the real type to maximally utilize the machine's numerical precision. Since I am writing a program (currently in C++ but I am thinking of moving to D) that has to handle lots of fractional numbers (calculating offset curves and such) I am wondering whether/when I should real instead of double or even any arguments in favour of staying with double (compatibility with C/C++?). Any pointers appreciated please? I checked the FAQ but it doesn't seem to mention this. Thanks! Since D does all operations at highest possible precision anyway (even for double or float) it only makes a difference when the value is being stored to memory and then read back again. I would recommend aliasing double or real to a custom name and then using that. Then when you're done you can easily switch between them and see how it affects precision in your particular case and decide what's best.
Re: and/or/not/xor operators
On 2013-05-30, 13:56, Shriramana Sharma wrote: Hello. I have always loved the readability of C++'s and/or/not/xor word-like logical operators but It doesn't seem to be available in D. Isn't this possible in D? I tried doing: alias and ; import std.stdio ; void main () { writeln ( true and true ) ; } but I get errors: $ dmd foo.d foo.d(1): Error: basic type expected, not foo.d(1): Error: no identifier for declarator int foo.d(1): Error: semicolon expected to close alias declaration foo.d(1): Error: Declaration expected, not '' foo.d(7): Error: found 'and' when expecting ', Thanks. While that's not possible, D's operator overloading allows you to implement it yourself: struct And { struct AndImpl { private bool payload; this( bool value ) { payload = value; } bool opBinary(string op : /)(bool value) const { return payload value; } } AndImpl opBinaryRight(string op : /)(bool value) { return AndImpl( value ); } } struct Or { struct OrImpl { private bool payload; this( bool value ) { payload = value; } bool opBinary(string op : /)(bool value) const { return payload || value; } } OrImpl opBinaryRight(string op : /)(bool value) { return OrImpl( value ); } } And and; Or or; void main( ) { assert( true /and/ true ); assert( true /or/ false ); } Of course, if you ever use this, watch out for the flood of WTFs and curse words. -- Simen
Re: double vs real
Shriramana Sharma: Hello. I like that D exposes to me the real type to maximally utilize the machine's numerical precision. Since I am writing a program (currently in C++ but I am thinking of moving to D) that has to handle lots of fractional numbers (calculating offset curves and such) I am wondering whether/when I should real instead of double or even any arguments in favour of staying with double (compatibility with C/C++?). Any pointers appreciated please? I checked the FAQ but it doesn't seem to mention this. If you have to store many reals, they require more memory than doubles (how much is relative to the operating system). Regarding speed in theory double and real should give the same, but in practice theory and practice often differ. As suggested, use an alias like: alias FP = real; And switch from double and real, and take a look at the differences. But don't perform such switch at the end, do it now and then to be sure everything keeps working correctly. Bye, barophile
Re: and/or/not/xor operators
Shriramana Sharma: Hello. I have always loved the readability of C++'s and/or/not/xor word-like logical operators but It doesn't seem to be available in D. and/or/not are less visually noisy, they look better than the ugly /||/! of C/C++/D, and it's much less easy to write when you need or vice versa, so they are also less bug-prone, as Python shows. On this Python syntax got it better than D. But Walter refused them time ago on the basis that no one uses them in C++. So you can ask for them in the main D newsgroup, but I don't think you will see them in D... Bye, bearophile
Re: My first D program
Shriramana Sharma: However I am somewhat taken aback to see the file size -- 335KiB for a simple Hello World? The equivalent C/C++ programs compiled with Clang without any -O options produce binaries of less than 10K! On Windows32 DMD produces binaries for small programs that are often half the size of binaries generated by similar small C++ programs compiled with G++ (about 300+ against 700+). D has a garbage collector, runtime type introspection (module info, type info, etc), run-time built-in operations on dynamic arrays (concat, append), associative arrays and some of their operations, a sort (but probably the built-in sort and reverse will be deprecated and later removed), exceptions, and more. All that needs space that's absent in the C++ binary. --- Regan Heath: The D standard library is currently statically linked. This will change shortly/eventually. And then you will need the GC somewhere to run it :-) Both static and dynamic linking have their advantages and disadvantages. I think Go has a storng preference for static linking. Bye, bearophile
Re: Consume an entire range
On Thursday, 30 May 2013 at 11:38:58 UTC, bearophile wrote: Brad Anderson: import std.stdio, std.algorithm, std.array; void eat(R)(R r) { while(!r.empty) { r.front; r.popFront; } } void main() { size_t[dstring] dic; stdin.byLine .joiner( ) .array .splitter(' ') .filter!(w = !w.empty w !in dic) .map!(w = writeln(dic[w.idup] = dic.length, '\t', w)) .eat; } I would have prefered to not use joiner() but working with ranges of ranges of ranges (splitter() on each line) got a bit weird and confusing. Maybe here it's better to work on lines. Alternatively I don't know if you can read the whole input there. I posted a version that worked on lines instead. I was having trouble but the trouble was actually the same problem I was having with the joined version (my map wasn't being consumed). It's usually better to give only pure functions to filter/map, because in Bugzilla I've shown those higher order functions don't work well otherwise. Have any links? I considered using sort and uniq to avoid the closure around dic but then the order would be different and I wanted to keep it using a similar technique to the original. So I prefer a terminal function that takes an impure function and returns nothing, something like: ... .filter!(w = !w.empty w !in dic) .forEach!((w) { writeln(dic[w.idup] = dic.length, '\t', w); }); Is forEach real? I sought it out because it'd be a better fit but came up empty (it's not in std.range or std.algorithm). Bye, bearophile
Re: and/or/not/xor operators
On Thursday, 30 May 2013 at 16:30:12 UTC, Iain Buclaw wrote: On Thursday, 30 May 2013 at 16:18:44 UTC, Shriramana Sharma wrote: Hello. I have always loved the readability of C++'s and/or/not/xor word-like logical operators but It doesn't seem to be available in D. Isn't this possible in D? I tried doing: No, it isn't available... Thank goodness! :) Why thank goodness? This would seem to significantly aid readability and comprehension.
Re: Consume an entire range
On Thursday, May 30, 2013 19:58:45 Brad Anderson wrote: Is forEach real? I sought it out because it'd be a better fit but came up empty (it's not in std.range or std.algorithm). No. The normal thing would be to just use an actual foreach loop. - Jonathan M Davis
how to pipe contents of a D string to a std.process.Pipe (without resorting to any escaping)
I want to pipe the contents of a D string to the stdin of a pipe created with one of the std.process functions. I want to avoiding resorting to escaping the string. How would i do that? Thanks
Re: and/or/not/xor operators
On Thursday, 30 May 2013 at 16:18:44 UTC, Shriramana Sharma wrote: Hello. I have always loved the readability of C++'s and/or/not/xor word-like logical operators but It doesn't seem to be available in D. Isn't this possible in D? I tried doing: alias and ; import std.stdio ; void main () { writeln ( true and true ) ; } but I get errors: $ dmd foo.d foo.d(1): Error: basic type expected, not foo.d(1): Error: no identifier for declarator int foo.d(1): Error: semicolon expected to close alias declaration foo.d(1): Error: Declaration expected, not '' foo.d(7): Error: found 'and' when expecting ', Thanks. For better or worse, we don't have those keywords in D and are unlikely to get them. Also, alias is not the same as the C preprocessor, you can't redefine any arbitrary string you want. http://dlang.org/declaration.html#alias
Re: Consume an entire range
Brad Anderson: Have any links? http://d.puremagic.com/issues/show_bug.cgi?id=9674 Is forEach real? It's just an idea. Bye, bearophile
Re: how to pipe contents of a D string to a std.process.Pipe (without resorting to any escaping)
On Thu, 30 May 2013 14:40:21 -0400, Timothee Cour thelastmamm...@gmail.com wrote: I want to pipe the contents of a D string to the stdin of a pipe created with one of the std.process functions. I want to avoiding resorting to escaping the string. How would i do that? Thanks use rawWrite -Steve
Automatic attribute inference of functions
Hi, I wonder if there are any plans to allow automatic inference of function attributes. I think it's a big hassle having to pollute function declarations with things like @safe, nothrow, pure, etc. let alone rembering them all. I know this is done for anonymous functions on a best effort basis, so what is holding back that feature for regular functions? Is it 1. Incompleteness, e.g. hard to implement correctly for all cases, thus we better leave it out entirely 2. The fact that every new keyword (thinking of 'public auto infer') bloats the language 3. In practice it isn't as annoying as I might think, YAGNI or a mixture of all 3?
Re: Automatic attribute inference of functions
On Thu, 30 May 2013 16:24:35 -0400, Sebastian Graf sebastiang...@t-online.de wrote: Hi, I wonder if there are any plans to allow automatic inference of function attributes. I think it's a big hassle having to pollute function declarations with things like @safe, nothrow, pure, etc. let alone rembering them all. I know this is done for anonymous functions on a best effort basis, so what is holding back that feature for regular functions? Is it 1. Incompleteness, e.g. hard to implement correctly for all cases, thus we better leave it out entirely 2. The fact that every new keyword (thinking of 'public auto infer') bloats the language 3. In practice it isn't as annoying as I might think, YAGNI It's actually 4. separate compilation model. If you are building a module, and it imports a d interface file which has no function body, there is no conceivable way to tell what the attributes should be. For anonymous functions, and template functions, where the entire function body MUST be present in order to use them, the compiler can and does infer attributes. We would need to change the compilation model, and potentially invent a new object file format/linker that tracks the function attributes in order to have this work. -Steve
Re: Duplicating multidimensional array
On 05/30/2013 09:36 AM, Ali Çehreli wrote: What do you think about the following recursive template solution? I have tested it only with arrays of int. :/ Very impressed -- I would never have thought of that trick with the second template parameter. It's really nice to see these kinds of example, as it's starting to bring to life the concepts of generic programming _as programming_, not just simple templating of functions or classes. I tried out the code with double-arrays of tuples, which is my use-case and which works. I guess it might fall over with complex structs or classes, though. :-\ What do you reckon the impact of this will be performance-wise? I'll try it out of course, but what with all the array concatenations etc. I'm worried about whether it might leak memory if repeated many times, and whether there might be some other better way to copy all the values from one multidimensional array to another.
Re: double vs real
Am Thu, 30 May 2013 17:47:14 +0530 schrieb Shriramana Sharma samj...@gmail.com: Hello. I like that D exposes to me the real type to maximally utilize the machine's numerical precision. Since I am writing a program (currently in C++ but I am thinking of moving to D) that has to handle lots of fractional numbers (calculating offset curves and such) I am wondering whether/when I should real instead of double or even any arguments in favour of staying with double (compatibility with C/C++?). Any pointers appreciated please? I checked the FAQ but it doesn't seem to mention this. Thanks! If you have large amounts of values and want to work on them fast use floats for storage since currently computers have more processing power than memory bandwidth. If you return numbers from functions use real. This allows the compiler to return them as-is from the FPU, whereas double and float require an additional rounding step. The same may apply to temporary variables. Where the amount of numbers is smaller and they fit into CPU cache, the memory bandwidth doesn't matter. 32-bit CPUs work the fastest with floats, whereas 64-bit CPUs work efficiently with doubles. My tip is to use real everywhere in calculations until it comes to storing the results for later use where doubles are more compact. -- Marco
Re: double vs real
On 05/30/2013 06:45 PM, bearophile wrote: Regarding speed in theory double and real should give the same, but in practice theory and practice often differ. In my experience, using real slows things down, though the degree depends on use-case (mine involves lots of iterations and calculations over different arrays full of floating-point numbers).
Re: Duplicating multidimensional array
On 05/30/2013 03:02 PM, Joseph Rushton Wakeling wrote: I would never have thought of that trick with the second template parameter. Phobos is a source of ideas. ;) I guess it might fall over with complex structs or classes, though. :-\ Copying structs is trivial because they already have copy semantics: T b = a; // b is a copy of a However, that depends on correctly implemented copy semantics on T. For classes, there is no syntax for copying. The type may have annotated a member function that it is the duplication function or we may know by convention that dup() is the equivalent of array .dup. What do you reckon the impact of this will be performance-wise? As long there is no extra copy generated during the process, it should be fine. However, the usual issues around the current conservative GC applies. :/ Ali
Re: My first D program
Thanks to all those who kindly replied. On Thu, May 30, 2013 at 9:57 PM, Regan Heath re...@netmail.co.nz wrote: The D standard library is currently statically linked. This will change shortly/eventually. Ah OK -- should have thought of that. So whatever is in libc.so or libstdc++.so doesn't get counted to the size of the C/C++ executables. Likewise we should have a libd.so I suppose. I am all for having shared runtime/stdlib. -- Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा
Re: Duplicating multidimensional array
On 05/31/2013 01:48 AM, Ali Çehreli wrote: Copying structs is trivial because they already have copy semantics: T b = a; // b is a copy of a However, that depends on correctly implemented copy semantics on T. By complex structs I meant things like structs containing arrays -- a regular copy would just copy the reference and not duplicate the arrays. As long there is no extra copy generated during the process, it should be fine. However, the usual issues around the current conservative GC applies. :/ I'll get back to you on that one when I have results ... :-)
Re: double vs real
Thanks to all who replied. On Thu, May 30, 2013 at 10:07 PM, Diggory digg...@googlemail.com wrote: Since D does all operations at highest possible precision anyway (even for double or float) it only makes a difference when the value is being stored to memory and then read back again. But isn't this true for even C/C++ i.e. that the actual FP calculation is done at a higher precision than what is exposed? And this is so that rounding errors may be minimized? (I mean, I can see how repeated multiplications and square roots and such would totally devalue the LSBs of a double if calculations were done only in double precision.) So IIUC the only thing D does newly is to actually *expose* the full machine precision for those who want it? But really how much use is that? Because a friend of mine was warning (in general, not particularly about D) against falling into the illusion of higher precision == higher accuracy. If I use 80-bit FP as my data storage type, then only if an even higher precision were actually used inside the processor for calculations would the LSBs retain their significance, right? So in the end what is real useful for? -- Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा
Re: and/or/not/xor operators
Thanks to all who replied. On Thu, May 30, 2013 at 10:18 PM, bearophile bearophileh...@lycos.com wrote: But Walter refused them time ago on the basis that no one uses them in C++. So you can ask for them in the main D newsgroup, but I don't think you will see them in D... Um why really? No one uses them in C++? How come? I agree that Walter has succesfully produced/marketed a C++ compiler for a long time, but really, no one uses?!!! I use, for one. And it does tremendously increase the readability of a program. Otherwise why would there be a separate C header ever since 1990 for that: http://en.wikipedia.org/wiki/Iso646.h I agree that we should retain the old operators for C compatibility but really at least the basic and/or/not/xor keywords should be provided. -- Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा
Re: and/or/not/xor operators
On 05/30/2013 06:41 PM, Shriramana Sharma wrote: no one uses?!!! I use, for one. Are there others in the team? Without getting into the argument of whether they are useful, you are the first person that I know who uses them. :) I have been programming in C since 1988 and in C++ since 1997. I have read a lot of books (mostly on C++), frequented the C++ newsgroups for years and years, and did work at many C and C++ companies. I have never seen those keywords used. I agree that we should retain the old operators for C compatibility but really at least the basic and/or/not/xor keywords should be provided. I don't have a preference on the topic but it would still be incomplete, right? There are many other operators that don't have keywords. Perhaps that's why not many people use them. Ali
Exception isn't thrown as expected
Hello I have code like this: class SomeClass { ubyte[] data_; ... auto getObjectType() const { if(data_ is null) { writeln(throwing); throw new Exception(Here the exception should be thrown!); } KeyHeaderHelper value_header; value_header.ptr_ = cast(ubyte*) data_.ptr; return value_header.object_type; } } When data_ is null the application just prints throwing and hangs up without an exception throwing. I don't know how to handle this. May be I did something wrong? If so then what?
Re: and/or/not/xor operators
Shriramana Sharma: Um why really? No one uses them in C++? How come? I agree that Walter has succesfully produced/marketed a C++ compiler for a long time, but really, no one uses?!!! I use, for one. And it does tremendously increase the readability of a program. Otherwise why would there be a separate C header ever since 1990 for that: http://en.wikipedia.org/wiki/Iso646.h I agree that we should retain the old operators for C compatibility but really at least the basic and/or/not/xor keywords should be provided. I prefer the Python design here, it uses | ^ for bitwise operators and and or not for the boolean operations. But I asked to Walter and that's the official answer I received. Python doesn't support || ! at all, so you are asking to introduce duplicated syntax in D. In D learn we help newbies, discuss bugs in user code, library code or in the compiler, sometimes we even propose problems to solve or small functions to add to Phobos, but this is not the right newsgroup to ask for new D syntax. If you are passionate about your request, then you should ask about it in the main D newsgroup. But don't expect to receive a yes sure! as answer :-) If you think many C++ programmers are using or and not, then it's much better for you to show evidence. Just saying I use it! is (rightfully) not enough for Walter. I read enough C++ code, and I don't see them used often. Bye, bearophile
Re: and/or/not/xor operators
Ali Çehreli: I don't have a preference on the topic but it would still be incomplete, right? There are many other operators that don't have keywords. Beside the || ! I think it's much better to not replace the other operators with keywords. Perhaps that's why not many people use them. I don't know why people don't use them in C++. Python programmers are usually happy to not use || !. Maybe they prefer to stick to one single common way to do something. Bye, bearophile
Re: Exception isn't thrown as expected
three questions come to mind: 1) what operating system and 32 bit or 64 bit? 2) what D compiler? 3) are you sure you didn't catch the exception somewhere up the chain and silence the message that way?
Re: Exception isn't thrown as expected
31.05.2013 8:59, Adam D. Ruppe пишет: three questions come to mind: 1) what operating system and 32 bit or 64 bit? 2) what D compiler? 3) are you sure you didn't catch the exception somewhere up the chain and silence the message that way? 1) win7 64 2) dmd 32 3) I don't catch explicitly. To ensure it isn't catch some way implicitly I just throw Exception without condition and it is thrown as expected auto getObjectType() const { throw new Exception(Here the exception should be thrown!); // works as expected KeyHeaderHelper value_header; value_header.ptr_ = cast(ubyte*) data_.ptr; return value_header.object_type; // if data isn't null it hangs up here } moreover, when I provide data_ always have some length, the application starts hangin at return operator. I've met something like this behaviour (I mean silent hanging up) earlier working with std.concurrency, but I found some workaround. Now I dont know what to do.
Re: Duplicating multidimensional array
On 05/30/2013 04:48 PM, Ali Çehreli wrote: For classes, there is no syntax for copying. The type may have annotated a member function that it is the duplication function or we may know by convention that dup() is the equivalent of array .dup. Kenji Hara responded to another thread on the main D newsgroup. Apparently, such a convention-based functionality is already being used by std.conv.to. Here is the excerpt: On 05/30/2013 07:13 PM, Kenji Hara wrote: Current D does not provide generic way for deep copy of class object. But, you can use std.conv.to with adding a kind of copy constructor. class C { int x; this(int n) { x = n; } // Do deep copy, this is used by to!(array-type)(array) this(const C c) { this.x = c.x; } } void main() { const(C)[] carr = [new C(1), new C(2)]; // C[] marr = carr.dup; // -- Error: cannot implicitly convert element type const(C) to mutable in carr.dup import std.conv; C[] marr = carr.to!(C[]); // For class arrays which need copy elements, // std.conv.to returns [new C(carr[0]), new C(carr[1]), ...] // modify element of returned array marr[0].x = 5; // Right now carr[0] and marr[0] are completely unrelated objects assert(carr[0].x == 1); assert(marr[0].x == 5); } Kenji Hara Ali
Re: double vs real
Am Fri, 31 May 2013 07:02:11 +0530 schrieb Shriramana Sharma samj...@gmail.com: Thanks to all who replied. On Thu, May 30, 2013 at 10:07 PM, Diggory digg...@googlemail.com wrote: Since D does all operations at highest possible precision anyway (even for double or float) it only makes a difference when the value is being stored to memory and then read back again. But isn't this true for even C/C++ i.e. that the actual FP calculation is done at a higher precision than what is exposed? And this is so that rounding errors may be minimized? (I mean, I can see how repeated multiplications and square roots and such would totally devalue the LSBs of a double if calculations were done only in double precision.) Well more or less. When C was designed PCs didn't have 80-bit floating point (real). So the specification read like this: » All floating arithmetic in C is carried out in double-precision; whenever a float appears in an expression it is lengthened to double by zero-padding its fraction. « The C runtime achieves this by switching a FPU setting for internal precision from real - the default - to double. So in C, double * double will in deed NOT use the full FPU precision. Later specifications (~1998) allowed for greater precision in C just like D does it. But I imagine few C compilers actually do this since it breaks code and is platform specific. E.g. PowerPC has 128-bit as the maximum precision, so the same program running on x86 and PowerPC using the full precision might give different results! So to summarize, C typically gives you the same results across platforms, D encourages compiler implementers to use a higher precision. (http://dlang.org/float.html) So IIUC the only thing D does newly is to actually *expose* the full machine precision for those who want it? Well there is the switch for the internal FPU precision and there is data types (like real). Most C compilers *do* offer 'real' data types under the name 'long double' and you can also change the FPU precision with intrinsics or compiler switches. So if you really want it you can get the same precision in C as in D. But you have to specialize for different compilers. But really how much use is that? Because a friend of mine was warning (in general, not particularly about D) against falling into the illusion of higher precision == higher accuracy. If I use 80-bit FP as my data storage type, then only if an even higher precision were actually used inside the processor for calculations would the LSBs retain their significance, right? It totally depends on the values and operations you apply on them. If you add two values which have the same exponent and their mantissas can be added without changing this exponent, you have 100% accuracy. If you do more complex arithmetics, accuracy is lost and your friend is right. But you seem to care so much about precision and not at all about speed or memory consumption that you will probably sleep better knowing that you might have saved one or two bits of precision from some calculations. :p So in the end what is real useful for? For everything that's not stored. ;) Function returns and variables that the compiler will likely keep in a FPU register: real foo(real x, real y) { real z = x + y; return z; } (If you use float or double for variable z, the compiler is forced to insert instructions that will round down and store the FP value in memory just to load it back up into an FPU register for the return.) -- Marco
Re: Exception isn't thrown as expected
Am Fri, 31 May 2013 10:26:49 +0700 schrieb Alexandr Druzhinin drug2...@bk.ru: 31.05.2013 8:59, Adam D. Ruppe пишет: three questions come to mind: 1) what operating system and 32 bit or 64 bit? 2) what D compiler? 3) are you sure you didn't catch the exception somewhere up the chain and silence the message that way? 1) win7 64 2) dmd 32 3) I don't catch explicitly. To ensure it isn't catch some way implicitly I just throw Exception without condition and it is thrown as expected auto getObjectType() const { throw new Exception(Here the exception should be thrown!); // works as expected KeyHeaderHelper value_header; value_header.ptr_ = cast(ubyte*) data_.ptr; return value_header.object_type; // if data isn't null it hangs up here } moreover, when I provide data_ always have some length, the application starts hangin at return operator. I've met something like this behaviour (I mean silent hanging up) earlier working with std.concurrency, but I found some workaround. Now I dont know what to do. The code that you haven't shown reads: int main(string[] args) { SomeClass c; auto t = c.getObjectType(); return 0; } You have to fix that! -- Marco
Re: Exception isn't thrown as expected
31.05.2013 10:49, Marco Leise пишет: The code that you haven't shown reads: int main(string[] args) { SomeClass c; auto t = c.getObjectType(); return 0; } You have to fix that! You mean I didn't initialize c? If even so, it should throw an exception, no hanging, I think. Nevertheless, a class instance is correct. I'll try to reduce code.
Why do i have to add to the counter when using for loop to perfectly get result?
/* licenced under the wtfpl license :D... as if i even understand how to license*/ import std.stdio; void main(){ //yeah... im making a diamond pattern out off asterisks writeln(Pick a number that is odd); int chosen; bool oddnoteven = false; while(oddnoteven == false){ write(Enter number here: ); readf( %s, chosen); int numbercheck = chosen % 2; if(numbercheck == 0){ writeln(I really don't like even numbers mofo :/... Just odd ones.. Try again? :D); } else{ oddnoteven = true; } } int peak = (chosen + 1)/2; for(int counter = 0; counter chosen + 1; ++counter){ // my fro block //Ok its not broken DO NOT KILL THE BLOCK! if(counter peak){ int spacesneeded = (peak - counter) - 1; for(int spacesmade = 0 ; spacesmade spacesneeded; ++spacesmade ){ write( ); } int shotsneeded = (2*counter) + 1;//(countxXXXerx + 1) +x (counterx + 1)/x2; uhh nope for(int shotsmade = 0; shotsmade shotsneeded; ++shotsmade){ write(*); } writeln(); } //hmmm bastard block... :/ if(counter peak){ int spacesneeded = (counter - peak); for(int spacesmade = 0; spacesmade spacesneeded; ++spacesmade){ write( ); } int shotsneeded = (2*(chosen - counter))+ 1; for(int shotsfired = 0; shotsfired shotsneeded; ++shotsfired){ write(*); } writeln(); } } } ok i know its bad math, hope its not too much of a waste of serv space. so can someone shoot me an explain about why in my fro block i hav to add +1 to chosen to perfectly make the diamond shape.. but it i take out the + 1 it cuts of the last print... help please :(
Using in as a parameter qualifier
Hello people. I have a pair type defined as : struct pair { double x,y ; this () {} this (double x, double y) { this.x = x ; this.y = y ; } } Consider a function that operates on a pair: double abs2 ( pair a ) { return a.x * a.x + a.y * a.y ; } In C++ the function signature would be: double abs2 ( const pair a ) So I thought const ref pair a would be appropriate in D -- is that right? How about the in qualifier? Does it only replace C++'s const in this case or does it also guarantee that the object will not be copied (/ref) ? Also: does D not require member initializer lists like in C++? In C++ I would write the this(double,double) above as: pair(double x, double y):x(x),y(y){} -- since that language is supposed to guarantee that once we enter the { of the constructor, all sub-objects are initialized. Thanks! -- Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा
Re: Why do i have to add to the counter when using for loop to perfectly get result?
Well, after a quick glance at the code you're iterating N times but only printing N-1 times. When counter == peak the loop does nothing so you'd get something like: chosen=5 for(int counter = 0; counter chosen ; ++counter){ // note +1 removed } counter = 0, 1, 2, [3]NO_PRINT, 4 *0 ***1 *2 ***4 (note: my ascii art, I assume this is what the code would produce...) Now add the +1 for(int counter = 0; counter chosen +1 ; ++counter){ 0, 1, 2, [3]skip, 4, 5 *0 ***1 *2 ***4 *5 With your implementation as it stands you need to skip iteration N/2+1 to get the correct output. But you still need to write N lines. Hope that makes sense...!! :D Stewart