Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
May 28, 2013 Re: iota and BigInt | ||||
---|---|---|---|---|
| ||||
On Tuesday, May 28, 2013 08:55:10 Russel Winder wrote:
> Arising from a thread on the GoLangNuts email list, I wrote the following:
>
> import std.algorithm: reduce;
> import std.bigint;
> import std.range: iota;
> import std.stdio: writeln;
>
> int main(immutable string[] args) {
> foreach (int i; iota(10, 50, 10)) {
> writeln(reduce!"a * b"(BigInt(1), iota(BigInt(1),
> BigInt(i))));
> }
> return 0;
> }
>
> Sadly the compiler refuses to acknowledge that iota works for BigInt. My first assumption is that my code is wrong. My second assumption is that std.range doesn't work with real integers — as opposed to those pesky limited hardware things ;-)
The problem is simple enough. iota doesn't currently try and work with any type that has addition (like it probably should). It specifically only works with integers, floating point values, and pointers. BigInt is not an integral type as far as std.traits is concerned. Only the built-in integer types are integral types as far as it's concerned. What's required is a different trait that tests that a type has arithmetic operations or for iota to simply test that the type has + or += or whatever it needs internally. Then iota can have an overload that works on any type that has the necessary operations.
- Jonathan m Davis
|
May 28, 2013 Re: iota and BigInt | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On 5/28/2013 1:32 AM, Jonathan M Davis wrote:
> On Tuesday, May 28, 2013 08:55:10 Russel Winder wrote:
>> Arising from a thread on the GoLangNuts email list, I wrote the
>> following:
>>
>> import std.algorithm: reduce;
>> import std.bigint;
>> import std.range: iota;
>> import std.stdio: writeln;
>>
>> int main(immutable string[] args) {
>> foreach (int i; iota(10, 50, 10)) {
>> writeln(reduce!"a * b"(BigInt(1), iota(BigInt(1),
>> BigInt(i))));
>> }
>> return 0;
>> }
>>
>> Sadly the compiler refuses to acknowledge that iota works for BigInt. My
>> first assumption is that my code is wrong. My second assumption is that
>> std.range doesn't work with real integers — as opposed to those pesky
>> limited hardware things ;-)
>
> The problem is simple enough. iota doesn't currently try and work with any
> type that has addition (like it probably should). It specifically only works
> with integers, floating point values, and pointers. BigInt is not an integral
> type as far as std.traits is concerned. Only the built-in integer types are
> integral types as far as it's concerned. What's required is a different trait
> that tests that a type has arithmetic operations or for iota to simply test
> that the type has + or += or whatever it needs internally. Then iota can have
> an overload that works on any type that has the necessary operations.
>
> - Jonathan m Davis
>
Please enter this into Bugzilla.
|
May 28, 2013 Re: iota and BigInt | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright: > Please enter this into Bugzilla. My request is in Bugzilla: http://d.puremagic.com/issues/show_bug.cgi?id=6447 Bye, bearophile |
May 28, 2013 Re: iota and BigInt | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile Attachments:
| On Tue, 2013-05-28 at 11:27 +0200, bearophile wrote: > Walter Bright: > > > Please enter this into Bugzilla. > > My request is in Bugzilla: http://d.puremagic.com/issues/show_bug.cgi?id=6447 > > Bye, > bearophile I have added myself to the cc list of this 2 year old problem. I don't have the time, and likely the knowledge, to fix this and provide a pull request. It would be great if DMD 2.063 got a fix for this and it percolated to LDC and GDC. Is this a symptom of Phobos being predicated on hardware data types? If so there is a mass of applications out there that D cannot handle sensible (unlike Python (especially SciPy), Julia, R, Matlab, Mathematica, etc. -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder |
May 28, 2013 Re: iota and BigInt | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On 5/28/13 4:32 AM, Jonathan M Davis wrote:
> The problem is simple enough. iota doesn't currently try and work with any
> type that has addition (like it probably should). It specifically only works
> with integers, floating point values, and pointers. BigInt is not an integral
> type as far as std.traits is concerned. Only the built-in integer types are
> integral types as far as it's concerned. What's required is a different trait
> that tests that a type has arithmetic operations or for iota to simply test
> that the type has + or += or whatever it needs internally. Then iota can have
> an overload that works on any type that has the necessary operations.
We should fix that; I keep on meaning to add traits like isIntegerLike etc.
Andrei
|
May 28, 2013 Re: iota and BigInt | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | On Tuesday, 28 May 2013 at 10:41:46 UTC, Russel Winder wrote:
> Is this a symptom of Phobos being predicated on hardware data types? If
> so there is a mass of applications out there that D cannot handle
> sensible (unlike Python (especially SciPy), Julia, R, Matlab,
> Mathematica, etc.
I think it is a problem is that iota is over parametrize-able.
iota can takes a lower bound (B), and upper bound (E), and a step (S), and returns a value (R).
The problem is that none of these parameters actually need to be the same. This forces the code to have to mess with things like "CommonType", which it really wouldn't have to if the only parameter(s) where "Type" and "Step".
The none-step version only requires that Type be incrementable, and the Step one needs to support "Type+=Step" (which returns type "Type")
Also: iota doesn't work with chars:
iota('k', 't'); //Nope.
Technically, the types don't even need to be "integer-like": as long as ++ or += works, it's all good.
|
May 28, 2013 Re: iota and BigInt | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 5/28/2013 7:26 AM, Andrei Alexandrescu wrote:
> On 5/28/13 4:32 AM, Jonathan M Davis wrote:
>> The problem is simple enough. iota doesn't currently try and work with any
>> type that has addition (like it probably should). It specifically only works
>> with integers, floating point values, and pointers. BigInt is not an integral
>> type as far as std.traits is concerned. Only the built-in integer types are
>> integral types as far as it's concerned. What's required is a different trait
>> that tests that a type has arithmetic operations or for iota to simply test
>> that the type has + or += or whatever it needs internally. Then iota can have
>> an overload that works on any type that has the necessary operations.
>
> We should fix that; I keep on meaning to add traits like isIntegerLike etc.
With iota, shouldn't it test for the existence of ++ and --, rather than being integer-like?
|
May 28, 2013 Re: iota and BigInt | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Tuesday, May 28, 2013 10:03:59 Walter Bright wrote:
> With iota, shouldn't it test for the existence of ++ and --, rather than being integer-like?
It gets more complicated if you give a step argument, but yes, it really should be testing for the operations that it needs rather than that the type has the whole set of arithmetic operators.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation