Re: Comparison issue
On 19/03/12 15:45, H. S. Teoh wrote: On Mon, Mar 19, 2012 at 08:50:02AM -0400, bearophile wrote: James Miller: writeln(v1 == 1); //false writeln(v1 == 1.0); //false writeln(v1 == 1.0f); //false writeln(v1+1 == 2.0f); //true Maybe I'd like to deprecate and then statically forbid the use of == among floating point values, and replace it with a library-defined function. [...] I agree. Using == for any floating point values is pretty much never right. Either we should change the definition of == for floats to use abs(y-x)epsilon for some given epsilon value, or we should prohibit it altogether, and force people to always write abs(y-x)epsilon. No, no, no. That's nonsense. For starters, note that ANY integer expression which is exact, is also exact in floating point. Another important case is that if (f == 0) is nearly always correct. Using == to compare floating point values is wrong. Due to the nature of floating point computation, there's always a possibility of roundoff error. Therefore, the correct way to compare floats is: immutable real epsilon = 1.0e-12; // adjustable accuracy here if (abs(y-x) epsilon) { // approximately equal } else { // not equal } And this is wrong, if y and x are both small, or both large. Your epsilon value is arbitrary. Absolute tolerance works for few functions like sin(), but not in general. See std.math.feqrel for a method which gives tolerance in terms of roundoff error, which is nearly always what you want. To summarize: For scientific/mathematical programming: * Usually you want relative tolerance * Sometimes you want exact equality. * Occasionally you want absolute tolerance But it depends on your application. For graphics programming you probably want absolute tolerance in most cases.
Re: regex issue
On 19.03.2012 23:24, Jay Norwood wrote: On Monday, 19 March 2012 at 13:55:39 UTC, Dmitry Olshansky wrote: That's right, however counting is completely separate from regex, you'd want to use std.algorithm count: count(match(,\n)); or more unicode-friendly: count(match(, regex($,m)); //note the multi-line flag Ehm, forgot g flag myself, so it would be count(match(, regex($,gm)); and count(match(, regex(\n,g)); Note that if your task is to split buffer by exactly '\n' byte then loop with memchr is about as fast as it gets, no amount of magic compiler optimizations would make other generic ways better (even theoretically). What they *could* do is bring the difference lower. This only sets l_cnt to 1 void wcp_cnt_match1 (string fn) { string input = cast(string)std.file.read(fn); enum ctr = ctRegex!($,m); ulong l_cnt = std.algorithm.count(match(input,ctr)); } This works ok, but though concise it is not very fast void wcp (string fn) { string input = cast(string)std.file.read(fn); ulong l_cnt = std.algorithm.count(input,\n); } BTW I suggest to separate I/O from actual work or better yet, time both separately via std.datetime.StopWatch. This fails to build, so I'd guess is missing \p void wcp (string fn) { enum ctr = ctRegex!(\p{WhiteSpace},m); } -- Build started: Project: a7, Configuration: Release Win32 -- Building Release\a7.exe... a7.d(210): undefined escape sequence \p Not a bug, a compiler escape sequence. How do you think \n works in your non-regex examples ? ;) -- Dmitry Olshansky
Re: regex issue
On Tuesday, 20 March 2012 at 10:28:11 UTC, Dmitry Olshansky wrote: Note that if your task is to split buffer by exactly '\n' byte then loop with memchr is about as fast as it gets, no amount of magic compiler optimizations would make other generic ways better (even theoretically). What they *could* do is bring the difference lower. ok, I'll use memchr. This works ok, but though concise it is not very fast void wcp (string fn) { string input = cast(string)std.file.read(fn); ulong l_cnt = std.algorithm.count(input,\n); } BTW I suggest to separate I/O from actual work or better yet, time both separately via std.datetime.StopWatch. I'm timing with the stopwatch. I have separate functions where I've measured empty func, just the file reads with empty loop, so I can see the deltas. All these are being executed inside a parallel foreach loop ... so 7 threads reading different files, and since that is the end target, the overall measurement in the context is more meaningful to me. The file io is on the order of 25ms for chunk reads or 30ms for full file reads in these results, as it is all reads of about 20MB for the full test from a 510 series ssd drive with sata3. The reads are being done in parallel by the threads in the threadpool. Each file is 2MB. So any total times you see in my comments are for 10 tasks being executed in a parallel foreach loop, with the file read portion previously timed at around 30ms. This fails to build, so I'd guess is missing \p void wcp (string fn) { enum ctr = ctRegex!(\p{WhiteSpace},m); } -- Build started: Project: a7, Configuration: Release Win32 -- Building Release\a7.exe... a7.d(210): undefined escape sequence \p Not a bug, a compiler escape sequence. How do you think \n works in your non-regex examples ? ;) yes, thanks. I read your other link and that was helpful. I think I presumed that the escape handling was something belonging to stdio, while regex would have its own valid escapes that would include \p. But I see now that the string literals have their own set of escapes.
Is there an elegant way of making a Result eager instead of lazy?
I understand the point of lazy evaluation but I often want to use the lazy algorithm library functions in an eager way. Other than looping through them all which feels rather messy is there a good way of doing this? Is there a reason not to allow the following to be automatically treated eagerly or is there some kind of cast or conv way of doing it? int[] test1 = [1,2,3,4]; int[] test2 = map!(a + a)(test1); //Eager, not allowed auto test3 = map!(a + a)(test1); //Lazy
Re: Is there an elegant way of making a Result eager instead of lazy?
On Tue, 20 Mar 2012 18:36:46 +0100, ixid nuacco...@gmail.com wrote: I understand the point of lazy evaluation but I often want to use the lazy algorithm library functions in an eager way. Other than looping through them all which feels rather messy is there a good way of doing this? Is there a reason not to allow the following to be automatically treated eagerly or is there some kind of cast or conv way of doing it? int[] test1 = [1,2,3,4]; int[] test2 = map!(a + a)(test1); //Eager, not allowed auto test3 = map!(a + a)(test1); //Lazy std.array includes a method, array(), for doing exactly this: int[] test2 = array(map!a+a(test1)); //eager
Re: Is there an elegant way of making a Result eager instead of lazy?
simendsjo: std.array includes a method, array(), for doing exactly this: int[] test2 = array(map!a+a(test1)); //eager With 2.059 you can write that also in a more readable way, because there is less nesting: int[] test2 = test1.map!q{a + a}().array(); Bye, bearophile
Re: Is there an elegant way of making a Result eager instead of lazy?
Thanks, very handy!
Re: XML Parsing
On Tuesday, 20 March 2012 at 04:32:13 UTC, Adam D. Ruppe wrote: I know very little about std.xml (I looked at it and said 'meh' and wrote my own lib), but my lib makes this pretty simple. https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff grab dom.d and characterencodings.d This has a bit of an html bias, but it works for xml too. === import arsd.dom; import std.file; import std.stdio; import std.conv; void main() { auto document = new Document(readText(test12.xml), true, true); auto map = document.requireSelector(map); writeln(to!int(map.width), x, to!int(map.height)); foreach(tile; document.getElementsByTagName(tile)) writeln(tile.gid); } === $ dmd test12.d dom.d characterencodings.d $ test12 25x19 snip tile data Let me explain the lines: auto document = new Document(readText(test12.xml), true, true); We use std.file.readText to read the file as a string. Document's constructor is: (string data, bool caseSensitive, bool strictMode). So, true, true means it will act like an XML parser, instead of trying to correct for html tag soup. Now, document is a DOM, like you see in W3C or web browsers (via javascript), though it is expanded with a lot of convenience and sugar. auto map = document.requireSelector(map); querySelector and requireSelector use CSS selector syntax to fetch one element. querySelector may return null, whereas requireSelector will throw an exception if the element is not found. You can learn more about CSS selector syntax on the web. I tried to cover a good chunk of the standard, including most css2 and some css3. Here, I'm asking for the first element with tag name map. You can also use querySelectorAll to get all the elements that match, returned as an array, which is great for looping. writeln(to!int(map.width), x, to!int(map.height)); The attributes on an element are exposed via dot syntax, or you can use element.getAttribute(name) if you prefer. They are returned as strings. Using std.conv.to, we can easily convert them to integers. foreach(tile; document.getElementsByTagName(tile)) writeln(tile.gid); And finally, we get all the tile tags in the document and print out their gid attribute. Note that you can also call the element search functions on individual elements. That will only return that element and its children. Here, you didn't need it, but you can also use element.innerText to get the text inside a tag, pretty much covering basic data retrieval. Note: my library is not good at handling huge files; it eats a good chunk of memory and loads the whole document at once. But, it is the easiest way I've seen (I'm biased though) to work with xml files, so I like it. Thank you. I'll check it out.
Re: Is there an elegant way of making a Result eager instead of lazy?
On 03/20/2012 10:50 AM, bearophile wrote: simendsjo: std.array includes a method, array(), for doing exactly this: int[] test2 = array(map!a+a(test1)); //eager With 2.059 you can write that also in a more readable way, because there is less nesting: int[] test2 = test1.map!q{a + a}().array(); Going off-topic but there is also the new = lambda syntax: int[] test2 = test1.map!(a = a + a)(test1).array(); Although, it makes it longer in cases like the one above. :) By the way, is there a name for the = syntax? Bye, bearophile Ali
Re: Is there an elegant way of making a Result eager instead of lazy?
On Tue, Mar 20, 2012 at 02:52:05PM -0700, Ali Çehreli wrote: [...] By the way, is there a name for the = syntax? [...] You just named it. :-) T -- Real programmers can write assembly code in any language. :-) -- Larry Wall
Re: Comparison issue
On 03/20/2012 02:08 AM, Don Clugston wrote: For starters, note that ANY integer expression which is exact, is also exact in floating point. With the note that the integer type has better precision at higher values. For example, there are many 32-bit values that uint can, but float cannot represent. Ali
Re: regex issue
On 21 March 2012 04:26, Jay Norwood j...@prismnet.com wrote: yes, thanks. I read your other link and that was helpful. I think I presumed that the escape handling was something belonging to stdio, while regex would have its own valid escapes that would include \p. But I see now that the string literals have their own set of escapes. Can you imagine the madness if escapes were specific to stdio, or some other library! Ok, and I'll just send this newline over the network... Dammit, std.network doesn't escape \n. Also means that you have perfect consistency between usages of strings, no strange other usages of the same escape sequence... -- James Miller
Converting C .h Files to D Modules
Hi all, How to convert the following struct to D? typedef struct S { int type; void *obj; } S; I didn't found anything at http://dlang.org/htomodule.html.
Re: Converting C .h Files to D Modules
Ouch, void* is the same in both languages, sorry. I addressed a new problem: typedef struct SomeFunctions { void *(*funcA)(char*, size_t); void *(*funcB)(void); } SomeFunctions; How do I convert that functions references into an D struct? On Tue, Mar 20, 2012 at 3:01 PM, Pedro Lacerda kanvua...@gmail.com wrote: Hi all, How to convert the following struct to D? typedef struct S { int type; void *obj; } S; I didn't found anything at http://dlang.org/htomodule.html.
Re: Converting C .h Files to D Modules
On 3/21/12, Pedro Lacerda kanvua...@gmail.com wrote: Ouch, void* is the same in both languages, sorry. I addressed a new problem: typedef struct SomeFunctions { void *(*funcA)(char*, size_t); void *(*funcB)(void); } SomeFunctions; How do I convert that functions references into an D struct? extern(C) struct SomeFunctions { void function(char*, size_t) funcA; void function() funcB; } Use HTOD (http://dlang.org/htod.html) if you can to convert .h to .D (it's Windows-only but might be usable via Wine).
Re: Converting C .h Files to D Modules
Andrej Mitrovic: extern(C) struct SomeFunctions { void function(char*, size_t) funcA; void function() funcB; } Are you sure that works? If that doesn't work then use this and write a bug report: struct SomeFunctions { extern(C) void function(char*, size_t) funcA; extern(C) void function() funcB; } Bye, bearophile
Re: Converting C .h Files to D Modules
On 3/21/12, bearophile bearophileh...@lycos.com wrote: Are you sure that works? It's easy to test: extern(C) struct SomeFunctions { void function(char*, size_t) funcA; void function() funcB; } void main() { writeln(typeof(SomeFunctions.funcA).stringof); }
Re: Converting C .h Files to D Modules
On Wednesday, 21 March 2012 at 01:09:58 UTC, Andrej Mitrovic wrote: On 3/21/12, Pedro Lacerda kanvua...@gmail.com wrote: Ouch, void* is the same in both languages, sorry. I addressed a new problem: typedef struct SomeFunctions { void *(*funcA)(char*, size_t); void *(*funcB)(void); } SomeFunctions; How do I convert that functions references into an D struct? extern(C) struct SomeFunctions { void function(char*, size_t) funcA; void function() funcB; } Use HTOD (http://dlang.org/htod.html) if you can to convert .h to .D (it's Windows-only but might be usable via Wine). Why not void* function(char*, size_t) funcA; void* function() funcB;
Re: Converting C .h Files to D Modules
On 3/21/12, Pedro Lacerda kanvua...@gmail.com wrote: dnewbie, you're correct, the return type is void* instead of void. I didn't notice the pointer thanks to the silly C function pointer syntax. :)
Re: Regarding writefln formatting
Andrej Mitrovic: I didn't know that! Is this documented anywhere? It's documented formally, but I see no usage examples of the nested formatting syntax: http://dlang.org/phobos/std_format.html Bye, bearophile