May 14, 2008 Re: eliminate cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dee Girl | My libs are designed to solve exactly such problems, this is the lib: http://www.fantascienza.net/leonardo/so/libs_d.zip Usage examples to do the things shown in this thread: import std.string: stripr; import std.conv: toInt; import d.func: map, xrange, range, toStruct, cinterval, zip; import d.string: xsplit, putr; import d.xio: xfile; void main() { auto values = map((string l){return toInt(stripr(l));}, xfile("test.txt")); putr(values); // mapping on two joined iterable things: auto arr = map((int x){return -x;}, xrange(6) ~ [1, 2, 3]); putr(arr); // zipping int a struct array: auto array1 = range(3, 30, 2); auto array2 = cinterval('b', 'h'); auto pairs = map((int i, char c){return toStruct(i, c);}, array1, array2); // Or just: putr(pairs); putr(zip(array1, array2)); } The output is verbatim: [345, 5467, 45, 238] [0, -1, -2, -3, -4, -5, -1, -2, -3] [<3, 'b'>, <5, 'c'>, <7, 'd'>, <9, 'e'>, <11, 'f'>, <13, 'g'>, <15, 'h'>] [<3, 'b'>, <5, 'c'>, <7, 'd'>, <9, 'e'>, <11, 'f'>, <13, 'g'>, <15, 'h'>] Note that xfile() is lazy and it returns strings, so if the file is large it doesn't build a huge array of lines. If you want there is an xmap() too that is lazy, and allows you the lazy printing too. The putr() is able to print any struct too, putting it inside <...>. cinterval gives a (closed on the right too) interval of chars. std.conv.toInt() is stupid, it's not even able to strip spaces by itself... so I've used the (quite slow) std.string.stripr(). The ~ after the xrange to chain those sequences is possible because all lazy iterators (like xrange) support the xchain protocol, but you can use the xchain() thing by itself too, for example if you want to chain two arrays, or you can add the Chainable mixin to your iterable classes. There are full ddocs with examples and full coverage unittests for everything that's present in the lib. Bye, bearophile | |||
May 15, 2008 Re: eliminate cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dee Girl | On 14/05/2008, Dee Girl <deegirl@noreply.com> wrote:
> 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
First off, char and string mean UTF-8, not ASCII. So by casting to string, you are saying "I know this is UTF-8".
There is a way to say "This is ASCII", which is to cast to AsciiString instead of to string. (AsciiString is defined in std.encoding).
There are currently two ways to validate. There's the old way: std.utf.validate(), which throws an exception if the validation fails. That will work for UTF-8, but it won't work for ASCII.
Then there's the new way: std.encoding.isValid(), which returns bool if validation fails. That one works for ASCII too, providing your data has type AsciiString instead of string.
Yes, there's currently duplicate functionality in Phobos, but std.utf will eventally be deprecated in favor of std.encoding.
| |||
June 10, 2008 Re: eliminate cast (style) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to downs | downs wrote: > > 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 Once again I see that wicked operator overloading. Am I the only one to find this code... ingenious but distasteful? Isn't this nicer?: auto sizes = (cast(string) "test.txt".read()).split().map(&atoi); -- Bruno Medeiros - Software Developer, MSc. in CS/E graduate http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply