[your code here]
import std.conv, std.traits, std.ascii; S rot13(S)(S s) if (isSomeString!S) { return rot(s, 13); } S rot(S)(S s, int key) if (isSomeString!S) { dchar[] r = new dchar[s.length]; foreach (i, dchar c; s) { if (isLower(c)) c = ((c - 'a' + key) % 26 + 'a'); else if (isUpper(c)) c = ((c - 'A' + key) % 26 + 'A'); r[i] = c; } return to!S(r); } --- still learning this stuff, feel free to correct or improve Jos
Re: [your code here]
On 15-1-2012 17:26, Timon Gehr wrote: On 01/15/2012 03:23 PM, Jos van Uden wrote: import std.conv, std.traits, std.ascii; S rot13(S)(S s) if (isSomeString!S) { return rot(s, 13); } S rot(S)(S s, int key) if (isSomeString!S) { dchar[] r = new dchar[s.length]; foreach (i, dchar c; s) { if (isLower(c)) c = ((c - 'a' + key) % 26 + 'a'); else if (isUpper(c)) c = ((c - 'A' + key) % 26 + 'A'); r[i] = c; } return to!S(r); } --- still learning this stuff, feel free to correct or improve Jos Your code will fail for narrow strings containing Unicode. dchar[] r = new dchar[s.length]; // creates a new dchar[] with as many elements as there are code units in the input string You can use std.range.walkLength to get the number of code points. Furthermore, that line mentions the type twice. auto r = new dchar[s.walkLength()]; Thank you. --- S rot13(S)(S s) if (isSomeString!S) { return rot(s, 13); } S rot(S)(S s, int key) if (isSomeString!S) { auto r = new dchar[s.walkLength()]; foreach (i, dchar c; s) { if (isLower(c)) r[i] = ((c - 'a' + key) % 26 + 'a'); else if (isUpper(c)) r[i] = ((c - 'A' + key) % 26 + 'A'); } return to!S(r); }
[your code here]
import std.stdio, std.stream, std.string, std.range; void main() { int countPalindromes; auto infile = new BufferedFile("unixdict.txt"); foreach (char[] line; infile) { if (line.walkLength > 1) { line.toLowerInPlace; if (line == line.dup.reverse) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }
Re: [your code here]
On 28-1-2012 14:57, Mantis wrote: 28.01.2012 15:31, Jos van Uden пишет: import std.stdio, std.stream, std.string, std.range; void main() { int countPalindromes; auto infile = new BufferedFile("unixdict.txt"); foreach (char[] line; infile) { if (line.walkLength > 1) { line.toLowerInPlace; if (line == line.dup.reverse) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); } The same may be done without memory duplication by changing comparison to this (equal function is in std.algorithm): if (equal(line, retro(line))) Hard to say without profiling if it will have actual impact on performance, but at least it should be more GC-friendly. Good idea. It's also faster. I tried with a larger file (ukacd17.txt) which has 240,000 entries. --- import std.stdio, std.stream, std.string, std.range, std.algorithm; void main() { int countPalindromes; auto infile = new BufferedFile("unixdict.txt"); foreach (char[] line; infile) { if (line.walkLength > 1) { line.toLowerInPlace; if (equal(line, retro(line))) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }
Re: [your code here]
On 28-1-2012 16:07, Andrei Alexandrescu wrote: import std.stdio, std.stream, std.string, std.range, std.algorithm; void main() { int countPalindromes; auto infile = new BufferedFile("unixdict.txt"); foreach (char[] line; infile) { if (line.walkLength > 1) { line.toLowerInPlace; if (equal(line, retro(line))) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); } Could you please also compare with code that uses foreach with stdin.byLine()? After having added the upTo param, which is an improvement on both methods, the infile is (marginally) faster on my system than the byLine. win7 64-bits i7 2600, dmd 2.057, optimizations enabled: -O -inline -release -- import std.stdio, std.stream, std.string, std.range, std.algorithm; void main() { int countPalindromes; auto infile = new BufferedFile("ukacd17.txt"); foreach (char[] line; infile) { if (line.walkLength(2) > 1) { line.toLowerInPlace; if (equal(line, retro(line))) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }
Re: [your code here]
On 29-1-2012 7:55, Manfred Nowak wrote: Jos van Uden wrote: BufferedFile("ukacd17.txt"); Me got an unicode-error on that file on line 100. -manfred The original file is encoded ISO 8859-1. You'll have to convert it to utf-8. My earlier code sample used the unixdict.txt that I found here http://www.puzzlers.org/pub/wordlists/unixdict.txt I only changed to ukacd17.txt to test performance. Jos
[your code here]
import std.string, std.traits, std.uni; enum Alphabet : dstring { DE = "abcdefghijklmnopqrstuvwxyzßäöü", EN = "abcdefghijklmnopqrstuvwxyz", SV = "abcdefghijklmnopqrstuvwxyzåäö" } bool isPangram(S)(S s, dstring alpha = Alphabet.EN) if (isSomeString!S) { foreach (c; alpha) if (indexOf(s, c) == -1 && indexOf(s, toUpper(c)) == -1) return false; return true; } --- A pangram is a sentence that contains every letter of a given alphabet at least once. A classic example is "The quick brown fox jumps over the lazy dog".
Re: [your code here]
On 4-2-2012 14:14, bearophile wrote: Jos van Uden: See: http://rosettacode.org/wiki/Pangram_checker#D Yeah, it's mine. And I bet the other one is yours :-)
Re: [your code here]
On 5-2-2012 5:05, Manfred Nowak wrote: 2) The optional third parameter of `indexOf' can be called with `CaseSensitive.no'. But that parameter is left untouched. Instead a check with `toUpper( c)' is used, thereby risking a further visitation of the whole string . Using CaseSensitive.no is a lot slower
Re: [your code here]
S rot13(S)(S s) if (isSomeString!S) { return rot(s, 13); } S rot(S)(S s, int key) if (isSomeString!S) { auto r = new dchar[s.walkLength()]; foreach (i, dchar c; s) { if (isLower(c)) r[i] = ((c - 'a' + key) % 26 + 'a'); else if (isUpper(c)) r[i] = ((c - 'A' + key) % 26 + 'A'); } return to!S(r); } this is better S rot13(S)(in S s) if (isSomeString!S) { return rot(s, 13); } S rot(S)(in S s, in int key) if (isSomeString!S) { auto r = new dchar[s.walkLength]; foreach (i, dchar c; s) { if (std.ascii.isLower(c)) c = ((c - 'a' + key) % 26 + 'a'); else if (std.ascii.isUpper(c)) c = ((c - 'A' + key) % 26 + 'A'); r[i] = c; } return to!S(r); }
[your code here]
bool isKaprekar(in long n) pure nothrow in { assert(n > 0, "isKaprekar(n): n must be > 0"); assert(n <= uint.max, "isKaprekar(n): n must be <= uint.max"); } body { ulong powr = n ^^ 2UL; ulong tens = 10, r, l; while (r < n) { r = powr % tens; l = powr / tens; if (r && l + r == n) return true; tens *= 10; } return false; } -- A positive integer is a Kaprekar number if: -It is 1 -The decimal representation of its square may be split once into two parts consisting of positive integers which sum to the original number. Note that a split resulting in a part consisting purely of 0s is not valid, as 0 is not considered positive. Example: 2223 is a Kaprekar number, as 2223 * 2223 = 4941729, 4941729 may be split to 494 and 1729, and 494 + 1729 = 2223. See also http://rosettacode.org/wiki/Kaprekar_numbers
Re: [your code here]
On 11-2-2012 16:30, H. S. Teoh wrote: On Sat, Feb 11, 2012 at 12:20:22PM +0100, Jos van Uden wrote: bool isKaprekar(in long n) pure nothrow in { assert(n> 0, "isKaprekar(n): n must be> 0"); assert(n<= uint.max, "isKaprekar(n): n must be<= uint.max"); } body { [...] Shouldn't you just use "in ulong n" as parameter instead of long with a contract? Good question. I'm not sure which is better. My personal preference goes to (in uint n) and rely on the (self) documentation. But this is demo code and I wanted to show Ds support for contracts. Also, the contract version will at least give you a warning in debug mode. By the way, if you use (in ulong n) you could still get in trouble because the code only handles upto uint.max correctly due to the pow. bool isKaprekar(in uint n) pure nothrow { ulong powr = n ^^ 2UL; ulong tens = 10, r, l; while (r < n) { r = powr % tens; l = powr / tens; if (r && l + r == n) return true; tens *= 10; } return false; }
Re: Arbitrary abbreviations in phobos considered ridiculous
On 6-3-2012 19:35, H. S. Teoh wrote: On Tue, Mar 06, 2012 at 07:13:47PM +0100, Alex Rønne Petersen wrote: (Also, seriously, I think you're over-dramatizing the Java variable naming thing; I rarely see names that are as bad as you claim...) [...] OK, I exaggerated a little. :-) But my point stands, that I find many of these names just way too long for my tastes. Like BufferedOutputStream. Or ByteArrayOutputStream. Ugh. I mean, we're writing programs here, not essays. Why spell things out in full if we don't need to? long time = System.currentTimeMillis(); I use a macro for that one :-) Jos
Re: The One-Letter Nested Function - a sample article for some kind of
On 13-2-2012 15:14, bearophile wrote: Zach the Mystic: void setRandomColorPair( ref ColorPair cp ) { import std.random; ubyte u(int a, int b) { return cast(ubyte) uniform(a,b); } Where possible it's good to add "static" to nested functions: Why?
Re: Understanding Templates: why can't anybody do it?
On 17-3-2012 21:20, Nick Sabalausky wrote: Here's a little templates primer, I hope it helps: [...] Part of this primer would serve well as introduction to Phillipe's tutorial, because it skips the basics. (I knew the basics, but not everybody does.) Jos
Re: What library functionality would you most like to see in D?
On 2-8-2011 4:48, Adam D. Ruppe wrote: I've been thinking about a minimalist drawing library for phobos - just basic 2d stuff, but with easy enough hooks to add more (native api events and handles. +1 I'd very much like to see this implemented.
Re: Had another 48hr game jam this weekend...
On 1-9-2013 20:00, bearophile wrote: Manu: Seriously, how do you quickly read and understand the API through the noise? The noise increases if you have to repeat the class name for each method :-) +1
Re: Had another 48hr game jam this weekend...
On 2-9-2013 15:46, Manu wrote: On 2 September 2013 21:37, Jos van Uden mailto:use...@fwend.com>> wrote: On 1-9-2013 20:00, bearophile wrote: Manu: Seriously, how do you quickly read and understand the API through the noise? The noise increases if you have to repeat the class name for each method :-) +1 Really? You both think seeing the function signature a second time at the definition is 'noisy' when compared to massive blocks of arbitrarily indented function body code consuming the entire class definition, and completely breaking up your code window? A few function bodies and you can't see anything anymore. You have to scroll miles to get an overview of the class, and try and remember each function header along the way as you scroll by; you can never digest it cleanly in one place. My memory's not that good... So you end up scrolling up and down and up and down and up and down, and then inevitably, get off your arse, walk over, and interrupt the guy that wrote it. That's a waste of my time, it's a waste of their time, and in an office environment, it's a waste of money. So, I find it extremely useful being able to see the members and functions available listed in a row all together. I can quickly gather a fairly complete mental picture. Everyone on the weekend agreed with me, none of us could immediately understand the classes we were working with. Productivity being the key element in our exercise, and it demonstrably impacted our productivity. Most editors support a navigation view. Then you can easily see the structure of the class. And then there's the documentation.
Re: Had another 48hr game jam this weekend...
On 2-9-2013 22:34, Walter Bright wrote: On 9/2/2013 7:15 AM, Manu wrote: On 2 September 2013 23:50, Andrej Mitrovic mailto:andrej.mitrov...@gmail.com>> wrote: On 9/2/13, Manu mailto:turkey...@gmail.com>> wrote: > But I still barely see this as an inconvenience when compared to not being > able to read a class definition. How about not being able to read the include paths in VS? I'm talking about this: http://i.stack.imgur.com/0cTZG.png You can view 2 lines at a time. After almost 20 years they still haven't fixed this. Where is their core dev team that should make the IDE experience great? Classic. It is classic. Scott Meyers did a presentation on that, calling it the "keyhole" interface, because it shows the view as if you looked at the data through a keyhole. :) It's like the environment variables. You have to copy the path to the clipboard and then to a plain text editor to have a proper look at it.
Re: Had another 48hr game jam this weekend...
On 3-9-2013 0:09, Andrej Mitrovic wrote: On 9/2/13, Jos van Uden wrote: It's like the environment variables. You have to copy the path to the clipboard and then to a plain text editor to have a proper look at it. It's why I use: http://www.rapidee.com/en/screenshots Brilliant! I gave it a special place on my taskbar.
Re: new DIP47: Outlining member functions of aggregates
On 7-9-2013 19:00, Walter Bright wrote: Outlining of member functions is the practice of placing the declaration of a member function in the struct/class/union, and placing the definition of it at global scope in the module or even in another module. http://wiki.dlang.org/DIP47 The problem is that it is optional, so when you're reading other people's code you'll still have to deal with inline definitions, and you'll need a decent editor (doesn't have to be an IDE, even basic editors like notepad++ and editpad pro support code folding and function lists) to easily read it.
Re: new DIP47: Outlining member functions of aggregates
On 7-9-2013 20:39, Paolo Invernizzi wrote: On Saturday, 7 September 2013 at 17:28:42 UTC, Jos van Uden wrote: On 7-9-2013 19:00, Walter Bright wrote: Outlining of member functions is the practice of placing the declaration of a member function in the struct/class/union, and placing the definition of it at global scope in the module or even in another module. http://wiki.dlang.org/DIP47 The problem is that it is optional, so when you're reading other people's code you'll still have to deal with inline definitions, and you'll need a decent editor (doesn't have to be an IDE, even basic editors like notepad++ and editpad pro support code folding and function lists) to easily read it. No pun intended, but having it as _mandatory_ would be a little to much for people like me that don't like this proposal at all. - Paolo Invernizzi Exactly, so we're adding a lot of code noise, while you still have to use an editor for codes that don't outline, so you might as well just use an smart editor in the first place.
Re: [OT] Which IDE / Editor do you use?
On 13-9-2013 21:48, Namespace wrote: Just out of interest. I use Sublime 2, Notepad++ and as IDE currently Mono-D. But I will try this evening VisualD. Notepad++. I've installed Visual Studio Shell + Visual-D but haven't used it much yet.
Re: Will Java go native?
On 19-9-2013 2:25, Walter Bright wrote: On 9/18/2013 2:33 PM, Chris wrote: Seeing that more and more developers and companies look for or actively develop native languages, I wonder will Java go native one day? (Cf. http://docs.oracle.com/cd/A97336_01/buslog.102/a83727/jtools5.htm) I wrote a native Java compiler in the 90's that was released by Symantec. There wasn't much interest in it. I remember that. It was part of Visual Cafe. I did use it to create a command line app for a client. It was very convenient to be able to ship a regular executable that needed no further instructions.
Re: std.d.lexer: pre-voting review / discussion
On 26-9-2013 17:41, Dominikus Dittes Scherkl wrote: Hello. I'm not sure if this belongs here, but I think there is bug at the very start of the Lexer chapter: Is U+001A really meant to end the source file? According to the Unicode specification this is a "replacement character", like the newer U+FFFC. Or is it simply a spelling error and U+0019 was intended to end the source (this would fit, as it means "end of media"). I don't know if anybody ever has ended his source in that way or if it was tested. More important to me is, that all the Space-Characters beyond ASCII are not considered whitespace (starting with U+00A0 NBSP, the different wide spaces U+2000 to U+200B up to the exotic stuff U+202F, U+205F, U+2060, U+3000 and the famous U+FEFF). Why? Ok, the set is much larger, but for the end-of-line also the unicode versions (U+2028 and U+2029) are added. This seems inconsequent to me. I imagine the lexer follows the language specification: http://dlang.org/lex.html#EndOfFile
Re: goto a no-go?
On 1-10-2013 13:22, Chris wrote: Is it ok or even necessary to use goto in D? I doubt it's necessary but it can be useful sometimes. In the Markov algorithm at Rosetta code I used it to restart a loop, including reinitializing a variable. I'm sure it could have been done without goto but it was convenient. http://rosettacode.org/wiki/Markov_Algorithm#D
Re: reddit.com: first Chapter of TDPL available for free
Andrei Alexandrescu wrote: http://www.reddit.com/r/programming/comments/975ng/diving_into_the_d_programming_language_tdpl/ (Don't tell anyone, but I plan to rewrite it.) Andrei This doesn't compile: string[] words = split(strip(line)); it has to be string[] words = split(strip(line.idup)); I like the way you write, it's amusing. And most of the time, you explain things well. But many of the code examples you provide don't compile or don't give correct results (I also had this problem with the Dr. Dobbs article). That makes me wonder if you actually test them or just write them off the top of your head? Jos
Re: reddit.com: first Chapter of TDPL available for free
Jos van Uden wrote: Andrei Alexandrescu wrote: http://www.reddit.com/r/programming/comments/975ng/diving_into_the_d_programming_language_tdpl/ (Don't tell anyone, but I plan to rewrite it.) Andrei This doesn't compile: string[] words = split(strip(line)); it has to be string[] words = split(strip(line.idup)); I like the way you write, it's amusing. And most of the time, you explain things well. But many of the code examples you provide don't compile or don't give correct results (I also had this problem with the Dr. Dobbs article). That makes me wonder if you actually test them or just write them off the top of your head? Right. It's explained later in the chapter. I hadn't gotten that far yet, got stuck trying to compile code that wouldn't. I guess I'm not supposed to do that. Jos