| Thread overview | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 14, 2008 eliminate cast | ||||
|---|---|---|---|---|
| ||||
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 ^_^. How can I eliminate cast? Is there function to read entire text file in string? Thank you, Dee Girl
| ||||
May 14, 2008 Re: eliminate cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dee Girl | Reply to Dee,
> 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 ^_^. How can I eliminate cast? Is
> there function to read entire text file in string? Thank you, Dee Girl
>
IMHO in this case the cast is /not/ bad. It is an explicit documentation of your intent. The file.read function knows nothing about what it is reading and therefor returns ubyte[] to say "this is data" and the cast is the programer saying "I known this is text".
| |||
May 14, 2008 Re: eliminate cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dee Girl | 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
| |||
May 14, 2008 Re: eliminate cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS Wrote:
> Reply to Dee,
>
> > 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 ^_^. How can I eliminate cast? Is there function to read entire text file in string? Thank you, Dee Girl
> >
>
> IMHO in this case the cast is /not/ bad. It is an explicit documentation of your intent. The file.read function knows nothing about what it is reading and therefor returns ubyte[] to say "this is data" and the cast is the programer saying "I known this is text".
O.K. but let say we want to check for invalid file (binary and not ascii). How can I do it? Thank you, Dee Girl
| |||
May 14, 2008 Re: eliminate cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to downs | 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
| |||
May 14, 2008 Re: eliminate cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dee Girl | Dee Girl <deegirl@noreply.com> wrote:
> 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);
That's one of the reasons we want better tuples.
(arr1, arr2) /map/ &atoi;
-- Simen
| |||
May 14, 2008 Re: eliminate cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dee Girl | Reply to Dee, > O.K. but let say we want to check for invalid file (binary and not > ascii). How can I do it? Thank you, Dee Girl > http://www.digitalmars.com/d/1.0/phobos/std_utf.html validate might be a start | |||
May 14, 2008 Re: eliminate cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | Simen Kjaeraas Wrote:
> Dee Girl <deegirl@noreply.com> wrote:
>
> > 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);
>
> That's one of the reasons we want better tuples.
>
> (arr1, arr2) /map/ &atoi;
>
> -- Simen
I search for tuple in std.algorithm. Good to have source! I found in the mismatch function. So I think this should work:
tuple(arr1, arr2) /map/ &atoi;
This can work? If it works then maybe it is better because syntax is not ambiguous. For a new comer, it seems D people care about syntax very much! This is good. ^_^ Thank you, Dee Girl
| |||
May 14, 2008 Re: eliminate cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to downs | "downs" <default_357-line@yahoo.de> wrote in message news:g0f48g$19jh$1@digitalmars.com... > 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 ^^ Where is "tools.functional" from? I don't see it in phobos, tango, cashew, dsource, wiki4d, google, or the D newsgroups. | |||
May 14, 2008 Re: eliminate cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | Reply to Nick, > Where is "tools.functional" from? I don't see it in phobos, tango, > cashew, dsource, wiki4d, google, or the D newsgroups. it's in scrapple: http://dsource.org/projects/scrapple/browser/trunk/tools/tools http://svn.dsource.org/projects/scrapple/trunk/tools/tools | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply