eliminate cast
Dee Girl
deegirl at noreply.com
Wed May 14 10:04:06 PDT 2008
downs Wrote:
> Dee Girl wrote:
> > There is a file data.txt with numbers:
> >
> > 345
> > 5467
> > 45
> > 238
> > ...
> >
> > And I want to load into an array of uint like this.
> >
> > auto sizes = map!
> > (to!(uint, string))
> > (compose!(split, q{cast(string) std.file.read(a)})("data.txt"));
> >
> > It works but cast is always bad ^_^.
>
> Where did you get that idea?
> In this case, basically, we know the file is really text. So we put this knowledge into code by assuring the compiler that yes, the arbitrary data you've just read _is_ text after all.
> In any case, it's just a reinterpreting cast. It doesn't change any actual data. So no time lost.
>
> Also, just fyi, here's how that code would look like in tools.functional:
>
> gentoo-pc ~ $ cat test2.d; echo -----; rebuild test2.d -oftest2 && ./test2
> module test2;
> import std.stdio, std.file, tools.functional, std.string: split, atoi;
> void main() {
> auto sizes = (cast(string) "test.txt".read()).split() /map/ &atoi;
> writefln(sizes);
> }
> -----
> [345,5467,45,238]
>
>
> I still prefer infix ^^
>
> --downs
Infix is nice but... I am sorry only a few days and I fight with everybody! ^_^ Sorry! Infix is nice (less ((()))) but it forces arity too 2. If you have two arrays and want to map over them you do:
arr1 ~ arr2 /map/ &atoi;
But with map you do:
map!(atoi)(arr1, arr2);
There is no more data copy. I think Also atoi is called directly (remember the previous thread ^_^) not by pointer. It looks to me map in std s superior in two ways.
But I think std.map can be better. Maybe you want to map many functions over one or many arrays. You should write:
auto t = map!(sin, cos)(array1, array2);
Then t is array of tuple with results. I think it is possible. std.reduce does it. So Andrei knows to do it but maybe did not have time.
D is so powerful!
Also maybe compose can take many functions. So in my dream I write:
auto sizes = compose!
(map!(to!(uint, string)), split, fileToText)
("data.txt");
It would be even better than all functional languages and scripting languages! Thank you, Dee Girl
More information about the Digitalmars-d
mailing list