Re: sort a string

2020-05-02 Thread notna via Digitalmars-d-learn

On Friday, 1 May 2020 at 19:25:43 UTC, Steven Schveighoffer wrote:


Nice! Yeah, I was sloppy in my newsgroup coding, sorry.

One minor nit here, the to!(dchar[])(word.dup), the dup is not 
necessary, you are going to end up allocating a temporary array 
and throwing it away.


Just do word.to!(dchar[]). `to` takes care of all the 
formalities.


-Steve


THANK YOU for all the great hints and explanations here and in 
general in this NG, Steve!


No blaming for the "sloopy code" at all. I should have seen the 
missing [] by myself, but...


_AND_ btw. your hint with the "release" was key to the discussion 
/ solution anyhow! :)


Re: sort a string

2020-05-01 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/1/20 11:17 AM, drug wrote:

01.05.2020 18:04, notna пишет:


hmmm, whích results in:
  Error: cannot use [] operator on expression of type dchar



try this:
```D
import std;
void main()
{
     string word = "Привет";
     dchar[] line3 = to!(dchar[])(word.dup) // make a copy to get a 
range of mutable char

    // and convert char to dchar
     .sort  // sort it
     .release;  // get the sorted range
     assert(line3 == "Пвеирт");
}
```


Nice! Yeah, I was sloppy in my newsgroup coding, sorry.

One minor nit here, the to!(dchar[])(word.dup), the dup is not 
necessary, you are going to end up allocating a temporary array and 
throwing it away.


Just do word.to!(dchar[]). `to` takes care of all the formalities.

-Steve


Re: sort a string

2020-05-01 Thread notna via Digitalmars-d-learn

On Friday, 1 May 2020 at 15:17:53 UTC, drug wrote:

01.05.2020 18:04, notna пишет:


hmmm, whích results in:
  Error: cannot use [] operator on expression of type dchar



try this:
```D
import std;
void main()
{
string word = "Привет";
dchar[] line3 = to!(dchar[])(word.dup) // make a copy to 
get a range of mutable char
   // and convert char 
to dchar

.sort  // sort it
.release;  // get the sorted 
range

assert(line3 == "Пвеирт");
}
```


THANKS, this looks even cleaner :)


Re: sort a string

2020-05-01 Thread notna via Digitalmars-d-learn

On Friday, 1 May 2020 at 15:15:29 UTC, bachmeier wrote:

On Friday, 1 May 2020 at 15:04:01 UTC, notna wrote:
On Friday, 1 May 2020 at 12:29:26 UTC, Steven Schveighoffer 
wrote:



    dchar[] line3 = sort(line2.to!(dchar[]));


dchar[] line3 = sort(line2.to!dchar[]).release;

https://dlang.org/phobos/std_range.html#.SortedRange.release



hmmm, whích results in:
 Error: cannot use [] operator on expression of type dchar


Working program:

import std.algorithm, std.conv, std.string, std.stdio;

void main() {
  string word = "bar";
  string line2 = toLower!(string)(word);
  dchar[] line3 = sort(line2.to!(dchar[])).release;
  writeln(line3);
}

You need to add parens.


well, this makes very much sense ;)
THANKS a lot, works and helped to adopt some old code



Re: sort a string

2020-05-01 Thread bachmeier via Digitalmars-d-learn

On Friday, 1 May 2020 at 15:04:01 UTC, notna wrote:
On Friday, 1 May 2020 at 12:29:26 UTC, Steven Schveighoffer 
wrote:



    dchar[] line3 = sort(line2.to!(dchar[]));


dchar[] line3 = sort(line2.to!dchar[]).release;

https://dlang.org/phobos/std_range.html#.SortedRange.release



hmmm, whích results in:
 Error: cannot use [] operator on expression of type dchar


Working program:

import std.algorithm, std.conv, std.string, std.stdio;

void main() {
  string word = "bar";
  string line2 = toLower!(string)(word);
  dchar[] line3 = sort(line2.to!(dchar[])).release;
  writeln(line3);
}

You need to add parens.


Re: sort a string

2020-05-01 Thread drug via Digitalmars-d-learn

01.05.2020 18:04, notna пишет:


hmmm, whích results in:
  Error: cannot use [] operator on expression of type dchar



try this:
```D
import std;
void main()
{
string word = "Привет";
dchar[] line3 = to!(dchar[])(word.dup) // make a copy to get a 
range of mutable char

   // and convert char to dchar
.sort  // sort it
.release;  // get the sorted range
assert(line3 == "Пвеирт");
}
```


Re: sort a string

2020-05-01 Thread notna via Digitalmars-d-learn

On Friday, 1 May 2020 at 12:29:26 UTC, Steven Schveighoffer wrote:


    dchar[] line3 = sort(line2.to!(dchar[]));


dchar[] line3 = sort(line2.to!dchar[]).release;

https://dlang.org/phobos/std_range.html#.SortedRange.release



hmmm, whích results in:
 Error: cannot use [] operator on expression of type dchar



Re: sort a string

2020-05-01 Thread drug via Digitalmars-d-learn

01.05.2020 15:29, Steven Schveighoffer пишет:


Don't do this, use to!(dchar[]) as you have above. This will create 
incorrect dchars for non-ascii text.


-Steve



Argh, as always you're right. Funny that I never did that and sadly that 
I posted wrong code. Thank you, Steven, for correction of my wrong 
posts, I appreciate it.


Re: sort a string

2020-05-01 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/1/20 4:12 AM, drug wrote:

01.05.2020 10:38, Chris Katko пишет:
I'm making anagrams. According to the nextPermutation() docs, I need 
to 'sort by less' to get all permutations. ... Except the doc page 
doesn't mention how to do that, nor does std.algorithm.sort show how 
to sort a string. ... and the google results on the dlang forums from 
2017 don't work.


I've tried .byCodeUnit. , .representation. I've tried sorting on the 
dchar. I've tried sorting the on string.


The closest I've gotten:

 string word = "bar";
 string line2 = toLower!(string)(word);
    dchar[] line3 = sort(line2.to!(dchar[]));


dchar[] line3 = sort(line2.to!dchar[]).release;

https://dlang.org/phobos/std_range.html#.SortedRange.release




"Error: cannot implicitly convert expression sort(to(line2)) of type 
SortedRange!(dchar[], "a < b") to dchar[]"




import std;
void main()
{
     string word = "bar";
     dchar[] line3 = word.dup // make a copy to get a range of mutable 
elements

     .map!"dchar(a)" // convert char to dchar


Don't do this, use to!(dchar[]) as you have above. This will create 
incorrect dchars for non-ascii text.


-Steve


Re: sort a string

2020-05-01 Thread Chris Katko via Digitalmars-d-learn

On Friday, 1 May 2020 at 08:17:33 UTC, norm wrote:

On Friday, 1 May 2020 at 07:38:53 UTC, Chris Katko wrote:

[...]


You need to convert the sort output to dchar[], e.g.
---
dchar[] line3 = sort(line2.to!(dchar[])).to!(dchar[]);
---

Cheers,
Norm


That works, thanks!



Re: sort a string

2020-05-01 Thread norm via Digitalmars-d-learn

On Friday, 1 May 2020 at 07:38:53 UTC, Chris Katko wrote:
I'm making anagrams. According to the nextPermutation() docs, I 
need to 'sort by less' to get all permutations. ... Except the 
doc page doesn't mention how to do that, nor does 
std.algorithm.sort show how to sort a string. ... and the 
google results on the dlang forums from 2017 don't work.


I've tried .byCodeUnit. , .representation. I've tried sorting 
on the dchar. I've tried sorting the on string.


The closest I've gotten:

string word = "bar";
string line2 = toLower!(string)(word);
dchar[] line3 = sort(line2.to!(dchar[]));

"Error: cannot implicitly convert expression sort(to(line2)) of 
type SortedRange!(dchar[], "a < b") to dchar[]"


You need to convert the sort output to dchar[], e.g.
---
dchar[] line3 = sort(line2.to!(dchar[])).to!(dchar[]);
---

Cheers,
Norm



Re: sort a string

2020-05-01 Thread drug via Digitalmars-d-learn

01.05.2020 10:38, Chris Katko пишет:
I'm making anagrams. According to the nextPermutation() docs, I need to 
'sort by less' to get all permutations. ... Except the doc page doesn't 
mention how to do that, nor does std.algorithm.sort show how to sort a 
string. ... and the google results on the dlang forums from 2017 don't 
work.


I've tried .byCodeUnit. , .representation. I've tried sorting on the 
dchar. I've tried sorting the on string.


The closest I've gotten:

 string word = "bar";
 string line2 = toLower!(string)(word);
    dchar[] line3 = sort(line2.to!(dchar[]));

"Error: cannot implicitly convert expression sort(to(line2)) of type 
SortedRange!(dchar[], "a < b") to dchar[]"




import std;
void main()
{
string word = "bar";
dchar[] line3 = word.dup // make a copy to get a range of mutable 
elements

.map!"dchar(a)" // convert char to dchar
.array // convert range to random access range (dynamic array 
here) to enable sorting

.sort  // sort
.array; // convert SortedRange to dynamic array
assert(line3 == "abr");
}