Jump to page: 1 2
Thread overview
using the full range of ubyte with iota
Jan 24, 2015
Tobias Pankrath
Jan 24, 2015
ketmar
Jan 25, 2015
ketmar
Jan 25, 2015
Vlad Levenfeld
Jan 25, 2015
bearophile
Jan 25, 2015
Tobias Pankrath
Jan 25, 2015
bearophile
Jan 25, 2015
ketmar
Jan 25, 2015
ketmar
Jan 24, 2015
bearophile
Jan 24, 2015
H. S. Teoh
Jan 24, 2015
ketmar
Jan 24, 2015
ketmar
January 24, 2015
Maybe I'm just too stupid, but I cannot manage to call a simple function
with all 256 possible values of ubyte with iote:

int foo(ubyte c);

auto myRange = iota(0,256).map!foo;

-->  Error: function foo(ubyte c) is not callable using argument types (int)

and this is because of the f*** end-type cannot be ubyte because in phobos everywhere end is excluded, so I have to define it too large by one.

Has anyone any idea how to work around this?
I would have no problem using an explicit cast, but where should I apply it?
January 24, 2015
On Saturday, 24 January 2015 at 20:49:03 UTC, Dominikus Dittes Scherkl wrote:
> Maybe I'm just too stupid, but I cannot manage to call a simple function
> with all 256 possible values of ubyte with iote:
>
> int foo(ubyte c);
>
> auto myRange = iota(0,256).map!foo;
>
> -->  Error: function foo(ubyte c) is not callable using argument types (int)
>
> and this is because of the f*** end-type cannot be ubyte because in phobos everywhere end is excluded, so I have to define it too large by one.
>
> Has anyone any idea how to work around this?
> I would have no problem using an explicit cast, but where should I apply it?

iota(0, 256).map!(x => foo(cast(ubyte) x))
January 24, 2015
Dominikus Dittes Scherkl:

> Has anyone any idea how to work around this?

In Bugzilla I have proposed to solve this problem with this syntax taken from std.range.uniform:

iota!"[]"(ubyte.min, ubyte.max)

Bye,
bearophile
January 24, 2015
On Sat, Jan 24, 2015 at 08:49:01PM +0000, Dominikus Dittes Scherkl via Digitalmars-d-learn wrote:
> Maybe I'm just too stupid, but I cannot manage to call a simple function with all 256 possible values of ubyte with iote:
> 
> int foo(ubyte c);
> 
> auto myRange = iota(0,256).map!foo;
[...]

Try:

	auto myRange = iota(0, 256).map!(a => foo(cast(ubyte)a));


T

-- 
Shin: (n.) A device for finding furniture in the dark.
January 24, 2015
On Sat, 24 Jan 2015 20:49:01 +0000, Dominikus Dittes Scherkl wrote:

> Maybe I'm just too stupid, but I cannot manage to call a simple function with all 256 possible values of ubyte with iote:
> 
> int foo(ubyte c);
> 
> auto myRange = iota(0,256).map!foo;
> 
> -->  Error: function foo(ubyte c) is not callable using argument types
> (int)
> 
> and this is because of the f*** end-type cannot be ubyte because in phobos everywhere end is excluded, so I have to define it too large by one.
> 
> Has anyone any idea how to work around this?
> I would have no problem using an explicit cast, but where should I apply
> it?

auto myRange = iota(0, 256).map!(a => foo(cast(ubyte)a));


January 24, 2015
juicy question!

January 24, 2015
On Saturday, 24 January 2015 at 21:00:06 UTC, Tobias Pankrath wrote:
> On Saturday, 24 January 2015 at 20:49:03 UTC, Dominikus Dittes Scherkl wrote:
>> I would have no problem using an explicit cast, but where should I apply it?
>
> iota(0, 256).map!(x => foo(cast(ubyte) x))

Ok, thank you very much.

4 times exactly the same answer - seems to be a common solution to a well known problem. :-/
January 24, 2015
On Sat, 24 Jan 2015 22:57:57 +0000, Dominikus Dittes Scherkl wrote:

> On Saturday, 24 January 2015 at 21:00:06 UTC, Tobias Pankrath wrote:
>> On Saturday, 24 January 2015 at 20:49:03 UTC, Dominikus Dittes Scherkl wrote:
>>> I would have no problem using an explicit cast, but where should I apply it?
>>
>> iota(0, 256).map!(x => foo(cast(ubyte) x))
> 
> Ok, thank you very much.
> 
> 4 times exactly the same answer - seems to be a common solution to a well known problem. :-/

people that are new to D aren't used to D lambdas, so it's fairly common. if you'll stay with D, you'll find yourself dreaming about such handy thing in another compiled languages very soon. ;-)

January 25, 2015
On Saturday, 24 January 2015 at 23:19:11 UTC, ketmar wrote:
> people that are new to D aren't used to D lambdas, so it's fairly common.
Oh, I am aware, but I didn't thought it would be necessary in this pace.

> if you'll stay with D, you'll find yourself dreaming about such handy thing in another compiled languages very soon. ;-)

I don't consider to use any other language as long as I have a choice :-)

But in the end, my one short and beautiful solution

auto myRange = iota(start, end).map!foo;

changed into something quite klumsy:

auto myRange = iota(start, end).map!(x => foo(cast(ParameterTypeTuple!foo[0])x));

which I think is against the philosophy of D as it turns away the eye of the reader from what is really going on, especially if previous constraints ensured that start and end-1 are of the correct type :-(

But ok, still far, far better than what would be neccessary in C++
January 25, 2015
On Sun, 25 Jan 2015 00:12:18 +0000, Dominikus Dittes Scherkl wrote:

> On Saturday, 24 January 2015 at 23:19:11 UTC, ketmar wrote:
>> people that are new to D aren't used to D lambdas, so it's fairly common.
> Oh, I am aware, but I didn't thought it would be necessary in this pace.
> 
>> if you'll stay with D, you'll find yourself dreaming about such handy thing in another compiled languages very soon. ;-)
> 
> I don't consider to use any other language as long as I have a choice :-)
> 
> But in the end, my one short and beautiful solution
> 
> auto myRange = iota(start, end).map!foo;
> 
> changed into something quite klumsy:
> 
> auto myRange = iota(start, end).map!(x =>
> foo(cast(ParameterTypeTuple!foo[0])x));
> 
> which I think is against the philosophy of D as it turns away the eye of the reader from what is really going on, especially if previous constraints ensured that start and end-1 are of the correct type :-(
> 
> But ok, still far, far better than what would be neccessary in C++

you can always write your own iota replacement, which will do "[]" and use ubytes, for example. writing that things is way easier than in C++. something like "myIota!ubyte(0, 255)", for example -- to make it visible that it emits ubytes.

« First   ‹ Prev
1 2