Jump to page: 1 2
Thread overview
conver BigInt to string
Nov 05, 2015
Namal
Nov 05, 2015
BBasile
Nov 05, 2015
Namal
Nov 05, 2015
BBasile
Nov 05, 2015
Meta
Nov 05, 2015
Namal
Nov 05, 2015
Ilya Yaroshenko
Nov 05, 2015
Namal
Nov 05, 2015
TheGag96
Nov 05, 2015
Meta
Nov 05, 2015
bearophile
Nov 05, 2015
bearophile
bearophile is back! :) was: Re: conver BigInt to string
Nov 05, 2015
Ali Çehreli
Nov 05, 2015
Chris
Nov 05, 2015
Ali Çehreli
Nov 05, 2015
Chris
Nov 06, 2015
Namal
Nov 06, 2015
cym13
November 05, 2015
Hello I am trying to convert BigInt to string like that while trying to sort it:

string s1 = to!string(a).dup.sort;

and get an error

cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string

what do I do wrong?
November 05, 2015
On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
> Hello I am trying to convert BigInt to string like that while trying to sort it:
>
> string s1 = to!string(a).dup.sort;
>
> and get an error
>
> cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string
>
> what do I do wrong?

try

".idup"

otherwise

"auto s1 = "
November 05, 2015
On Thursday, 5 November 2015 at 16:35:01 UTC, BBasile wrote:
> On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
>> Hello I am trying to convert BigInt to string like that while trying to sort it:
>>
>> string s1 = to!string(a).dup.sort;
>>
>> and get an error
>>
>> cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string
>>
>> what do I do wrong?
>
> try
>
> ".idup"
>
> otherwise
>
> "auto s1 = "

auto did it, but idup leads to

Error: can only sort a mutable array

November 05, 2015
On Thursday, 5 November 2015 at 16:39:03 UTC, Namal wrote:
> On Thursday, 5 November 2015 at 16:35:01 UTC, BBasile wrote:
>> On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
>>> Hello I am trying to convert BigInt to string like that while trying to sort it:
>>>
>>> string s1 = to!string(a).dup.sort;
>>>
>>> and get an error
>>>
>>> cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string
>>>
>>> what do I do wrong?
>>
>> try
>>
>> ".idup"
>>
>> otherwise
>>
>> "auto s1 = "
>
> auto did it, but idup leads to
>
> Error: can only sort a mutable array

sorry, I feel embarrassed now...good luck i wish you.
November 05, 2015
On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
> Hello I am trying to convert BigInt to string like that while trying to sort it:
>
> string s1 = to!string(a).dup.sort;
>
> and get an error
>
> cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string
>
> what do I do wrong?

Try this instead:

string s1 = to!string(a).idup.sort()

I suspect there are two things happening here. First, calling dup on a string yields char[], not string (the difference being that string is immutable and char[] is not). A char[] cannot implicitly convert to string due to the difference in immutability, although the compiler *should* realize that the result of dup is a "unique" expression and be able to implicitly convert it... It may be because you then pass it to sort, which must keep it as a char[].

The second issue is that using .sort instead of .sort() (note the parentheses) calls the built-in sort instead of std.algorithm.sort, which is strongly discouraged. You should always use std.algorithm.sort over the built-in sort, which you can do just by appending those parentheses.
November 05, 2015
On Thursday, 5 November 2015 at 16:45:10 UTC, Meta wrote:
> On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
>> Hello I am trying to convert BigInt to string like that while trying to sort it:
>>
>> string s1 = to!string(a).dup.sort;
>>
>> and get an error
>>
>> cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string
>>
>> what do I do wrong?
>
> Try this instead:
>
> string s1 = to!string(a).idup.sort()
>
If I try it like that i get:

Error: template std.algorithm.sorting.sort cannot deduce function from argument types !()(char[]), candidates are:

/../src/phobos/std/algorithm/sorting.d(996):

November 05, 2015
On Thursday, 5 November 2015 at 16:53:50 UTC, Namal wrote:
> On Thursday, 5 November 2015 at 16:45:10 UTC, Meta wrote:
>> On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
>>> Hello I am trying to convert BigInt to string like that while trying to sort it:
>>>
>>> string s1 = to!string(a).dup.sort;
>>>
>>> and get an error
>>>
>>> cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string
>>>
>>> what do I do wrong?
>>
>> Try this instead:
>>
>> string s1 = to!string(a).idup.sort()
>>
> If I try it like that i get:
>
> Error: template std.algorithm.sorting.sort cannot deduce function from argument types !()(char[]), candidates are:
>
> /../src/phobos/std/algorithm/sorting.d(996):

string s1 = to!string(a).dup.sort.idup;
November 05, 2015
On Thursday, 5 November 2015 at 17:13:07 UTC, Ilya Yaroshenko wrote:

> string s1 = to!string(a).dup.sort.idup;
well, but I was just told not to use sort ??

November 05, 2015
Namal:

> Hello I am trying to convert BigInt to string like that while trying to sort it:

void main() {
    import std.stdio, std.algorithm, std.conv, std.bigint, std.string;

    auto n = 17.BigInt ^^ 179;
    n.text.dup.representation.sort().release.assumeUTF.writeln;
}

Bye,
bearophile
November 05, 2015
> void main() {
>     import std.stdio, std.algorithm, std.conv, std.bigint, std.string;
>
>     auto n = 17.BigInt ^^ 179;
>     n.text.dup.representation.sort().release.assumeUTF.writeln;
> }

Better:

    n.to!(char[]).representation.sort().release.assumeUTF.writeln;

Bye,
bearophile
« First   ‹ Prev
1 2