digitalmars.D.learn - conver BigInt to string
- Namal (7/7) Nov 05 2015 Hello I am trying to convert BigInt to string like that while
- BBasile (5/12) Nov 05 2015 try
- Namal (3/19) Nov 05 2015 auto did it, but idup leads to
- BBasile (2/25) Nov 05 2015 sorry, I feel embarrassed now...good luck i wish you.
- Meta (16/23) Nov 05 2015 Try this instead:
- Namal (5/19) Nov 05 2015 If I try it like that i get:
- Ilya Yaroshenko (2/24) Nov 05 2015 string s1 = to!string(a).dup.sort.idup;
- Namal (3/4) Nov 05 2015 well, but I was just told not to use sort ??
- TheGag96 (10/15) Nov 05 2015 Whoa whoa whoa... This is the first time I've heard about this
- Meta (9/18) Nov 05 2015 It's a legacy issue that will hopefully be fixed someday. The
- bearophile (9/11) Nov 05 2015 void main() {
- bearophile (4/10) Nov 05 2015 Better:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (3/5) Nov 05 2015 Were you immersed in another language? Rust?
- Chris (2/7) Nov 05 2015 His D doesn't seem to be Rusty though!
- =?UTF-8?Q?Ali_=c3=87ehreli?= (3/13) Nov 05 2015 Good one! ;) I'm really happy that he is still around.
- Chris (2/4) Nov 05 2015 So am I! The more, the merrier!
- Namal (2/13) Nov 06 2015 can I import libraries anywhere? Is this the proper way to do so?
- cym13 (6/24) Nov 06 2015 You can, and some say you should. The thing is, if you use
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?
Nov 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 = "
Nov 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:auto did it, but idup leads to Error: can only sort a mutable arrayHello 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 = "
Nov 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:sorry, I feel embarrassed now...good luck i wish you.On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:auto did it, but idup leads to Error: can only sort a mutable arrayHello 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 = "
Nov 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.
Nov 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: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):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()
Nov 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:string s1 = to!string(a).dup.sort.idup;On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote: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):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()
Nov 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 ??
Nov 05 2015
On Thursday, 5 November 2015 at 16:45:10 UTC, Meta wrote: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.Whoa whoa whoa... This is the first time I've heard about this difference, and I've used .sort plenty of times... That seems like really, REALLY bad design, especially considering the language allows functions to be called without parentheses. I thought I was using std.algorithm's version the whole time. What's the difference between the implementations of arrays' .sort property and std.algorithm.sort()? And does sort() throw out that "unable to deduce function argument" error for a character array of all things?
Nov 05 2015
On Thursday, 5 November 2015 at 20:45:45 UTC, TheGag96 wrote:Whoa whoa whoa... This is the first time I've heard about this difference, and I've used .sort plenty of times... That seems like really, REALLY bad design, especially considering the language allows functions to be called without parentheses. I thought I was using std.algorithm's version the whole time.It's a legacy issue that will hopefully be fixed someday. The issue is that ever since D1, arrays have had a .sort property that uses a built-in sorting function. The compiler will only recognize it as the std.algorithm version of sort if you use parentheses.What's the difference between the implementations of arrays' .sort property and std.algorithm.sort()? And does sort() throw out that "unable to deduce function argument" error for a character array of all things?The built-in sort is buggy and slow and should never be used. As far as I know it does not produce errors of its own, so any error messages you see like that are coming from the std.algorithm sort.
Nov 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
Nov 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
Nov 05 2015
On 11/05/2015 09:40 AM, bearophile wrote:Bye, bearophileWere you immersed in another language? Rust? Ali
Nov 05 2015
On Thursday, 5 November 2015 at 19:30:02 UTC, Ali Çehreli wrote:On 11/05/2015 09:40 AM, bearophile wrote:His D doesn't seem to be Rusty though!Bye, bearophileWere you immersed in another language? Rust? Ali
Nov 05 2015
On 11/05/2015 11:35 AM, Chris wrote:On Thursday, 5 November 2015 at 19:30:02 UTC, Ali Çehreli wrote:Good one! ;) I'm really happy that he is still around. AliOn 11/05/2015 09:40 AM, bearophile wrote:His D doesn't seem to be Rusty though!Bye, bearophileWere you immersed in another language? Rust? Ali
Nov 05 2015
On Thursday, 5 November 2015 at 19:38:23 UTC, Ali Çehreli wrote:Good one! ;) I'm really happy that he is still around. AliSo am I! The more, the merrier!
Nov 05 2015
On Thursday, 5 November 2015 at 17:40:12 UTC, bearophile wrote:Namal:can I import libraries anywhere? Is this the proper way to do so?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
Nov 06 2015
On Friday, 6 November 2015 at 10:00:23 UTC, Namal wrote:On Thursday, 5 November 2015 at 17:40:12 UTC, bearophile wrote:You can, and some say you should. The thing is, if you use templates and import the necessary libraries within the template then the import occurs only if the template is instantiated which can be a big gain. Phobos makes great use of this technique for example.Namal:can I import libraries anywhere? Is this the proper way to do so?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
Nov 06 2015