Re: SO rotate question

2010-09-03 Thread Pelle
On 09/02/2010 10:24 PM, bearophile wrote: simendsjo: Suggestions for D-ifying the code is welcome. Your unit tests are not good enough, they miss some important corner cases. This my first version in D2: import std.string: indexOf; /// return True if s1 is a rotated version of s2 bool

Re: SO rotate question

2010-09-03 Thread simendsjo
Pelle wrote: On 09/02/2010 10:24 PM, bearophile wrote: simendsjo: Suggestions for D-ifying the code is welcome. Your unit tests are not good enough, they miss some important corner cases. This my first version in D2: import std.string: indexOf; /// return True if s1 is a rotated version

Re: SO rotate question

2010-09-03 Thread bearophile
simendsjo: I agree that's much simpler, but s1 ~ s1 doesn't perform too well I think. Always creates a new heap allocation and copies the array.. The version written by Pelle is better. On the other hand performance is not an absolute thing, it's good enough or not good enough, and in many

Re: SO rotate question

2010-09-03 Thread Pelle
On 09/03/2010 01:35 PM, bearophile wrote: Pelle: bool isRotated(T)(T[] a, T[] b) { return a.length == b.length (a.length == 0 || canFind(chain(a,a), b)); } Nice clean solution. I suggest to add pure and two const in the signature. canFind isn't really the best possible name, is it?

Re: SO rotate question

2010-09-03 Thread bearophile
Pelle: bool isRotated(T)(T[] a, T[] b) { return a.length == b.length (a.length == 0 || canFind(chain(a,a), b)); } Nice clean solution. I suggest to add pure and two const in the signature. canFind isn't really the best possible name, is it? Some name/APIs of Phobos are not the

Re: SO rotate question

2010-09-03 Thread Jonathan M Davis
On Friday 03 September 2010 04:35:30 bearophile wrote: canFind isn't really the best possible name, is it? Some name/APIs of Phobos are not the best possible, they were often invented by a single person. I don't know if contains() or isIn() are better. It used to be that all you had was

Understanding isInfinite(Range)

2010-09-03 Thread Andrej Mitrovic
I was reading about the various range templates in std.range and I found this: http://www.digitalmars.com/d/2.0/phobos/std_range.html#isInfinite Seems simple enough. But I dont understand it's implementation, this from range.d: template isInfinite(Range) { static if (isInputRange!Range

Re: Understanding isInfinite(Range)

2010-09-03 Thread Steven Schveighoffer
On Fri, 03 Sep 2010 11:12:29 -0400, Andrej Mitrovic andrej.mitrov...@test.com wrote: I was reading about the various range templates in std.range and I found this: http://www.digitalmars.com/d/2.0/phobos/std_range.html#isInfinite Seems simple enough. But I dont understand it's

Re: Understanding isInfinite(Range)

2010-09-03 Thread Andrej Mitrovic
Ah, you're right. Whenever I see the square brackets my brain automatically things we're indexing something. I'll blame that on C. :p Thanks. Steven Schveighoffer Wrote: On Fri, 03 Sep 2010 11:12:29 -0400, Andrej Mitrovic andrej.mitrov...@test.com wrote: I was reading about the various

How to name things [Was: Re: SO rotate question]

2010-09-03 Thread bearophile
Jonathan M Davis: Still, naming functions is a bit of an art and most function names are debatable as to how good they are, so you're bound to have functions in any API that aren't named the way that you think they should be. I don't like what you say. If you take a look at how in the last

Re: Threading errors.

2010-09-03 Thread Andrej Mitrovic
So what's the word on this? Will we have simple indexing of tuples via T[] or do we still need T.field[] and T._1 ? The situations is the same in 2.048 as it was in 2.047. There are more examples of tuples being used via T[] in some Phobos code examples (which don't compile and I've reported

ubyte[] - immutable(ubyte)[]

2010-09-03 Thread Andrej Mitrovic
This is from TDPL page 407: import std.algorithm, std.concurrency, std.stdio; void main() { enum bufferSize = 1024 * 100; auto tid = spawn(fileWriter); // Read loop foreach (immutable(ubyte)[] buffer; stdin.byChunk(bufferSize)) { send(tid, buffer);

Re: How to name things [Was: Re: SO rotate question]

2010-09-03 Thread Jonathan M Davis
On Friday 03 September 2010 10:14:24 bearophile wrote: Jonathan M Davis: Still, naming functions is a bit of an art and most function names are debatable as to how good they are, so you're bound to have functions in any API that aren't named the way that you think they should be. I don't

RosettaCode: Echo Server

2010-09-03 Thread sybrandy
Hello, I decided to exercise my skills in D a bit and wrote an implementation for an echo server using D. I figure before I post it to the site, I'd post it here for comments as I'd like to get the best possible version on the site vs. something that's half-assed. Considering I'm very new

About std.stdio.File.writef

2010-09-03 Thread bearophile
With dmd 2.048 this code: import std.stdio: File; void main() { auto f = File(TEST.ppm, w); f.writef(%c, 50); } Prints at run-time: std.format.FormatError: std.format integral So to write a char I've had to use: f.write(cast(char)50); Partially unrelated: is is possible to perform