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. > >

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: 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 w

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 n

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, i

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 ma

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 o

Re: SO rotate question

2010-09-02 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 isRota

Re: SO rotate question

2010-09-02 Thread simendsjo
On 02.09.2010 22:24, 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 isRotated

Re: SO rotate question

2010-09-02 Thread bearophile
> bool isRotated(T)(T[] s1, T[] s2) { A better signature for the same function, I need to train myself to use pure and const more often :-) pure bool isRotated(T)(const T[] s1, const T[] s2) { Bye, bearophile

Re: SO rotate question

2010-09-02 Thread bearophile
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 isRotated(T)(T[] s1, T[] s2) { return (s1.leng

SO rotate question

2010-09-02 Thread simendsjo
http://stackoverflow.com/questions/2553522/interview-question-check-if-one-string-is-a-rotation-of-other-string I created a solution, but I don't want D to look bad, so I won't post it. It's a bit for loop heavy... At least it's shorter than the c example, but I don't know about speed or readab