Re: iota and BigInt
On Thu, 2015-01-22 at 10:21 -0800, H. S. Teoh via Digitalmars-d-learn wrote: […] > > https://github.com/D-Programming-Language/phobos/pull/2895 > > This is just the tip of the iceberg. The full enhancement is described > in: > > https://issues.dlang.org/show_bug.cgi?id=10762 > Sadly, I suspect it will be a long time before this is usable by folk in the field :-( It's a pity there is not better support for arbitrary size integers and floating point in D. Hardware integers are too limited and hardware floating point far too inaccurate for an awful lot of computing out there (*). Groovy is getting a lot of traction exactly because the default floating point is BigDecimal and not IEEE754. (*) I know games requires speed above all else, as does a lot of the sort of calculations I have done in the past, but these calculations generally need very few significant digits of accuracy. We won't mention the calculation that are just wrong because insufficient attention to error management occurs. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
why spawn crash?
i wrote a test code: void worker(int firstNumber) { Thread.sleep(1.msecs); } void main() { foreach (i; 1 .. 1000) { spawn(&worker, i ); writeln(i); } thread_joinAll(); writeln("ok"); } sometimes it's ok,sometimes it's crashed ! why ? here is one of times callstack message: test.exe!_free() test.exe!_fwide() test.exe!_fwide() test.exe!__ReleaseSemaphore() test.exe!_fwide() test.exe!main@__modctor() 行 3 test.exe!rt@minfo@__T14runModuleFuncsS482rt5minfo11ModuleGroup11runTlsCtorsMFZ9__lambda1Z@runModuleFuncs() test.exe!___threadstartex@4()
Re: histogram [last thread contd]
ddos: auto numbers = iota(0,1).map!(_ => uniform(0.0,1.0)).array; Better: const numbers = 10_000.iota.map!(_ => uniform01).array; auto nmin = numbers.reduce!((a,b) => min(a,b)); Better: immutable nMin = numbers.reduce!min; You can also combine both (untested): immutable nMinMax = numbers.reduce!(min, max); auto nmax = numbers.reduce!((a,b) => max(a,b)) + double.epsilon; Bye, bearophile
histogram [last thread contd]
i wrote a histogram algorithm i tried to write it the shortest and most "D" way possible ... please tell me if you see any simpler way of doing it is there a simpler way of getting the minimum of a range? (by intuition i tried range.min) auto numbers = iota(0,1).map!(_ => uniform(0.0,1.0)).array; auto nmin = numbers.reduce!((a,b) => min(a,b)); auto nmax = numbers.reduce!((a,b) => max(a,b)) + double.epsilon; int bins = 100; auto bin = iota!float(0, bins).map!(a => tuple((nmax-nmin)/bins*a+nmin, (nmax-nmin)/bins*(a+1)+nmin)); auto bincount = bin.map!(a => numbers.map!(b => b >= a[0] && b < a[1] ? 1 : 0).sum); bincount.writeln;
Re: generate an array of 100 uniform distributed numbers
ddos: iota(0,100).map!(v => uniform(0.0,1.0)).writeln; You can also write: 100.iota.map!(_ => uniform01).writeln; Bye, bearophile
Re: generate an array of 100 uniform distributed numbers
thx, alot :) works as intended iota(0,100).map!(v => uniform(0.0,1.0)).writeln;
Re: generate an array of 100 uniform distributed numbers
On Thu, 22 Jan 2015 19:26:44 +, ddos wrote: > hi guys, firstly this has no direct application, i'm just playing around > and learning > > i want to create 100 uniform distributed numbers and print them my first > attempt, just written by intuition: > [0 .. 100].map!(v => uniform(0.0, 1.0).writeln); > > i found out i can't write [0 .. 100] to define a simple number range, > but is there a function to do so? The iota function from std.range: iota(0, 100).map!(...) > > second attempt, replacing the range with an simple array [0,1,2].map!(v > => uniform(0.0,1.0).writeln); > this does compile and run, but doesn't print anything, just an empty > string, why is that? Two issues: 1) The function supplied to map should be a projection function, e.g. it takes a value and returns a value. Your lambda returns void (the result of writeln). 2) map is lazy--it doesn't do any work until something consumes it. This is awesome for many reasons (e.g. you can process infinite ranges). Nothing in your code is causing the result of map to be consumed, so it does no work. > finally i got it working with this: > auto t = [0,1,2].map!(v => uniform(0.0,1.0)); > writeln(t); This works because writeln eagerly consumes the result of map, causing the work to actually be done. If you like, you can tack the writeln to the end of the pipeline: auto t = [0,1,2].map!(v => uniform(0.0,1.0)).writeln;
Re: generate an array of 100 uniform distributed numbers
On 01/22/2015 11:26 AM, ddos wrote: > i want to create 100 uniform distributed numbers and print them > my first attempt, just written by intuition: > [0 .. 100].map!(v => uniform(0.0, 1.0).writeln); > > i found out i can't write [0 .. 100] to define a simple number range, As currently being discussed in another thread, the a..b syntax does not correspond to a first-class D language construct. > but is there a function to do so? Yes, std.range.iota: auto numbers = iota(100).map!(_ => uniform(0.0, 1.0)); Note that, 'numbers' is lazy, the definition above does not call uniform() yet. To make an array out of it, call std.array.array at the end: auto numbers = iota(100).map!(_ => uniform(0.0, 1.0)).array; > second attempt, replacing the range with an simple array > [0,1,2].map!(v => uniform(0.0,1.0).writeln); > this does compile and run, but doesn't print anything, just an empty > string, why is that? What I said above: it is just a range waiting to be used. > finally i got it working with this: > auto t = [0,1,2].map!(v => uniform(0.0,1.0)); > writeln(t); > > seems pretty easy eh? writeln() consumes any range to print it on the standard output. There is no array to speak of though: writeln() consumes a copy of 't' and your 't' is still a lazy range waiting to be consumed. Ali
Re: generate an array of 100 uniform distributed numbers
On Thursday, 22 January 2015 at 19:26:46 UTC, ddos wrote: hi guys, firstly this has no direct application, i'm just playing around and learning i want to create 100 uniform distributed numbers and print them my first attempt, just written by intuition: [0 .. 100].map!(v => uniform(0.0, 1.0).writeln); i found out i can't write [0 .. 100] to define a simple number range, but is there a function to do so? Yes! iota(100) second attempt, replacing the range with an simple array [0,1,2].map!(v => uniform(0.0,1.0).writeln); this does compile and run, but doesn't print anything, just an empty string, why is that? Maybe you meant to put the .writeln outside of the parens? Since map is lazily evaluated, writeln is never called and the entire expression does nothing.
Re: Defining a static array with values in a range
On Thursday, 22 January 2015 at 19:13:46 UTC, Meta wrote: On Thursday, 22 January 2015 at 19:12:32 UTC, zeljkog wrote: On 22.01.15 20:05, Meta wrote: On Thursday, 22 January 2015 at 19:00:47 UTC, zeljkog wrote: On 22.01.15 19:26, Meta wrote: On Thursday, 22 January 2015 at 18:23:00 UTC, Meta wrote: Whoops, I forgot to make it a template to force CTFE. You can force CTFE assigning to manifest constant. enum t = charRange!... By wrapping it in a template the array will always be generated at compile time, even if you assign it to a runtime variable. Yes, but then you can not use runtime spec. OP wanted it to be done at compile time. A small improvement to ensure the input consists of all ASCII characters, and support for backward intervals. import std.stdio; template charRange(string spec) { static processSpec(string spec) { import std.algorithm; import std.ascii; import std.conv; import std.range; import std.string; return spec.split(',').map!((s) { s = s.strip; assert(s.all!(c => cast(ulong)c < 256), "Expected all characters in 'spec' to be ASCII"); auto start = cast(ubyte)s[1..$].front; auto end = cast(ubyte)s[0..$-1].back; return start <= end ? iota(start, end + 1) .map!(c => cast(char)c).array : iota(end, start + 1) .retro .map!(c => cast(char)c).array; }).join; } enum charRange = processSpec(spec); } unittest { assert(charRange!q{ 'a'..'z', 'A'..'Z', '0'..'9' } == charRange!q{ '9'..'0', 'Z'..'A', 'z'..'a' }.reverse); } void main(string[] argv) { auto t1 = charRange!q{ 'a'..'z', 'A'..'Z', '0'..'9' }; writeln(t1); auto t2 = charRange!q{ '9'..'0', 'Z'..'A', 'z'..'a' }; writeln(t2); }
generate an array of 100 uniform distributed numbers
hi guys, firstly this has no direct application, i'm just playing around and learning i want to create 100 uniform distributed numbers and print them my first attempt, just written by intuition: [0 .. 100].map!(v => uniform(0.0, 1.0).writeln); i found out i can't write [0 .. 100] to define a simple number range, but is there a function to do so? second attempt, replacing the range with an simple array [0,1,2].map!(v => uniform(0.0,1.0).writeln); this does compile and run, but doesn't print anything, just an empty string, why is that? finally i got it working with this: auto t = [0,1,2].map!(v => uniform(0.0,1.0)); writeln(t); seems pretty easy eh? d is bugging me alot like this ^_^ but i love it's syntax
Re: Defining a static array with values in a range
On Thursday, 22 January 2015 at 19:12:32 UTC, zeljkog wrote: On 22.01.15 20:05, Meta wrote: On Thursday, 22 January 2015 at 19:00:47 UTC, zeljkog wrote: On 22.01.15 19:26, Meta wrote: On Thursday, 22 January 2015 at 18:23:00 UTC, Meta wrote: Whoops, I forgot to make it a template to force CTFE. You can force CTFE assigning to manifest constant. enum t = charRange!... By wrapping it in a template the array will always be generated at compile time, even if you assign it to a runtime variable. Yes, but then you can not use runtime spec. OP wanted it to be done at compile time.
Re: Defining a static array with values in a range
On 22.01.15 20:05, Meta wrote: On Thursday, 22 January 2015 at 19:00:47 UTC, zeljkog wrote: On 22.01.15 19:26, Meta wrote: On Thursday, 22 January 2015 at 18:23:00 UTC, Meta wrote: Whoops, I forgot to make it a template to force CTFE. You can force CTFE assigning to manifest constant. enum t = charRange!... By wrapping it in a template the array will always be generated at compile time, even if you assign it to a runtime variable. Yes, but then you can not use runtime spec.
Re: Defining a static array with values in a range
On Thursday, 22 January 2015 at 19:00:47 UTC, zeljkog wrote: On 22.01.15 19:26, Meta wrote: On Thursday, 22 January 2015 at 18:23:00 UTC, Meta wrote: Whoops, I forgot to make it a template to force CTFE. You can force CTFE assigning to manifest constant. enum t = charRange!... By wrapping it in a template the array will always be generated at compile time, even if you assign it to a runtime variable.
Re: Defining a static array with values in a range
On 22.01.15 19:26, Meta wrote: On Thursday, 22 January 2015 at 18:23:00 UTC, Meta wrote: Whoops, I forgot to make it a template to force CTFE. You can force CTFE assigning to manifest constant. enum t = charRange!...
Re: Extracting Structure from HTML using Adam's dom.d
On Thu, 22 Jan 2015 18:39:25 +, Adam D. Ruppe wrote: > On Thursday, 22 January 2015 at 16:22:14 UTC, ketmar via > Digitalmars-d-learn wrote: >> i miss it in Phobos. > > I'm sure it'd fail the phobos review process though. But since it is an > independent file (or it + characterencodings.d for full functionality), > it is easy to just download and add to your project anyway. yes, but that's one more file to download. if it's in Phobos, i can just install dmd and go on writing my k00l skriptz right away. that's why i want it there. i know that it will hardly happen, but can i dream? ;-) signature.asc Description: PGP signature
Re: Extracting Structure from HTML using Adam's dom.d
On Thursday, 22 January 2015 at 10:14:58 UTC, Suliman wrote: Adam, please add more simple docs about your parser on site. I'll post some ddoc in the next dmd release, now that dmd finally supports some way to automatically escape xml examples. Also it would be perfect to create dub, for easier including parser to project. I don't use dub and don't like its requirements, so I won't do that. Someone else is free to package it though. But like I just said in the other email, it is just a single file for core functionality so you can just download that and add it to your project as a source file.
Re: Extracting Structure from HTML using Adam's dom.d
On Thursday, 22 January 2015 at 16:22:14 UTC, ketmar via Digitalmars-d-learn wrote: i miss it in Phobos. I'm sure it'd fail the phobos review process though. But since it is an independent file (or it + characterencodings.d for full functionality), it is easy to just download and add to your project anyway.
Re: Defining a static array with values in a range
On Thursday, 22 January 2015 at 18:23:00 UTC, Meta wrote: Whoops, I forgot to make it a template to force CTFE. import std.stdio; template charRange(string spec) { static processInput(string spec) { import std.algorithm; import std.ascii; import std.conv; import std.range; import std.string; return spec.split(',').map!((s) { s = s.strip; auto start = s[1..$].front; auto end = s[0..$-1].back; return iota(start, end + 1).map!(c => cast(char)c).array; }).array.join; } enum charRange = processInput(spec); } void main(string[] argv) { auto t = charRange!q{ 'a'..'z', 'A'..'Z', '0'..'9' }; writeln(t); readln(); }
Re: Defining a static array with values in a range
On Thursday, 22 January 2015 at 17:45:59 UTC, tcak wrote: So, at the end of the day (I left working on my Matcher class in the morning waiting an answer for this question), there is nothing to convert ['a'..'d', '0'..'3'] to ['a', 'b', 'c', 'd', '0', '1', '2', '3'] at compile time automatically. There is rarely never a way to do something in D, if you want to hack around a bit. import std.stdio; @property charRange(string spec)() { import std.algorithm; import std.ascii; import std.conv; import std.range; import std.string; return spec.split(',').map!((s) { s = s.strip; auto start = s[1..$].front; auto end = s[0..$-1].back; return iota(start, end + 1).map!(c => cast(char)c).array; }).array.join; } void main(string[] argv) { auto t = charRange!q{ 'a'..'z', 'A'..'Z', '0'..'9' }; //abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 writeln(t); } This is very rough code, as it only works for ASCII codepoints, and it can't do backward intervals, but you get the idea.
Re: iota and BigInt
On Thu, Jan 22, 2015 at 05:12:17PM +, Russel Winder via Digitalmars-d-learn wrote: > Using reduce for factorial, seems to require iota, not a bad things > per se, with ulongs: > > reduce!"a*b"(1, iota(1, n + 1)) > > works fine. Now switch to BigInt: > > reduce!"a*b"(one, iota(one, n + one)) > > fails to compile, one and n + one are of different types. Problem is > that one is immutable but n + one is not. I can't remember how to > create an immutable value as an expression so: > > immutable BigInt v = n + one; > reduce!"a*b"(one, iota(one, v)) > > now I get an error telling me that I can't use iota because BigInt is > not a float or an integer. Is this a known bug? Yes it is: > https://issues.dlang.org/show_bug.cgi?id=6447 > > Does anyone know how to fix this 3.5 year old bug that is a blocker > for doing anything with factorial and Fibonacci Series.? [...] https://github.com/D-Programming-Language/phobos/pull/2895 This is just the tip of the iceberg. The full enhancement is described in: https://issues.dlang.org/show_bug.cgi?id=10762 T -- Give a man a fish, and he eats once. Teach a man to fish, and he will sit forever.
Re: Changing by ref a class passed in function
On Thursday, 22 January 2015 at 14:39:45 UTC, anonymous wrote: On Thursday, 22 January 2015 at 14:29:59 UTC, anonymous wrote: o needs to be typed as Object: Object o = new MyClass(); nullIt(o); Alternatively, templatize nullIt: void nullIt(O)(ref O o) {o = null;} auto o = new MyClass(); nullIt(o); Template way is better here, thank you.
Re: Defining a static array with values in a range
tcak: So, at the end of the day (I left working on my Matcher class in the morning waiting an answer for this question), there is nothing to convert ['a'..'d', '0'..'3'] to ['a', 'b', 'c', 'd', '0', '1', '2', '3'] at compile time automatically. Right. The 'a'..'d' is not first class, and you can't invent new syntax. Bye, bearophile
Re: Defining a static array with values in a range
On Thursday, 22 January 2015 at 17:15:34 UTC, Jonathan M Davis via Digitalmars-d-learn wrote: On Thursday, January 22, 2015 15:16:07 bearophile via Digitalmars-d-learn wrote: Jonathan M Davis: > but that's easy fixed with some +1's. But the +1 changes the char to an int. True, though iota doesn't seem to like to operate on char anyway, so from the little playing around with it I did to answer the OP, it looks like you're forced to use casts for it anyway. And depending, you probably want to cast to ubyte and then convert that to char when you convert the range to an array to avoid the conversion to dchar anyway. So, all around, trying to use iota with char is a bit awkward. - Jonathan M Davis So, at the end of the day (I left working on my Matcher class in the morning waiting an answer for this question), there is nothing to convert ['a'..'d', '0'..'3'] to ['a', 'b', 'c', 'd', '0', '1', '2', '3'] at compile time automatically.
Re: On Variable References
On Thursday, 22 January 2015 at 14:52:26 UTC, Marc Schütz wrote: On Wednesday, 21 January 2015 at 17:14:29 UTC, Meta wrote: On Wednesday, 21 January 2015 at 08:23:44 UTC, Per Nordlöw wrote: On Wednesday, 21 January 2015 at 08:22:44 UTC, Per Nordlöw wrote: int x; auto ref xr; Correction: I, of course mean, int x = 42; auto ref xr = x; Walter is strongly against adding references a la C++ to D, as he believes they are too complicated and bug prone. He's made several posts on this, but I can't find them now. In DIP69, he himself proposed `ref` locals. However, that's not the same as "references a la C++". Hmm, it seems that he did. If D were to allow creating ref variables, wouldn't D ref be more or less like in C++, then? Aside from the weirder things like T&& and such.
Re: Defining a static array with values in a range
On Thursday, January 22, 2015 15:16:07 bearophile via Digitalmars-d-learn wrote: > Jonathan M Davis: > > > but that's easy fixed with some +1's. > > But the +1 changes the char to an int. True, though iota doesn't seem to like to operate on char anyway, so from the little playing around with it I did to answer the OP, it looks like you're forced to use casts for it anyway. And depending, you probably want to cast to ubyte and then convert that to char when you convert the range to an array to avoid the conversion to dchar anyway. So, all around, trying to use iota with char is a bit awkward. - Jonathan M Davis
Re: for ranges
On 1/22/15 11:41 AM, Russel Winder via Digitalmars-d-learn wrote: Playing with factorial implementations, as you do. I had a D implementation using ulong. Not sensible obviously since overflow is a bit of a problem. But the code worked, as did the tests. Now converting to BigInt and… The standard explicit iteration form uses a loop: for(i; 2..n+1) for n = 0 or 1 this loop doesn't loop since the range is [,). However for BigInt: for(i; two..n + one) the loop starts at 0 and just keeps on going. This is clearly not good. Am I having a mental breakdown or is this a real bug? The issue: import std.stdio; struct S { int x; int opCmp(const S other) const { writeln("compare"); return x < other.x ? -1 : x > other.x ? 1 : 0;} bool opEquals(const S other) const { writeln("equals"); return x == other.x;} ref S opOpAssign(string op)(int other) { mixin("x " ~ op ~ "= other;"); return this; } } void main() { immutable S one = S(1); immutable S two = S(2); foreach(s; one..two) {} } output: equals equals So foreach(S; one..two) translates to: for(S x = one; x != two; x += 1) which explains the infinite loop. I'm almost positive foreach(x; 1..2) uses comparison instead of equality to check for end condition. -Steve
iota and BigInt
Using reduce for factorial, seems to require iota, not a bad things per se, with ulongs: reduce!"a*b"(1, iota(1, n + 1)) works fine. Now switch to BigInt: reduce!"a*b"(one, iota(one, n + one)) fails to compile, one and n + one are of different types. Problem is that one is immutable but n + one is not. I can't remember how to create an immutable value as an expression so: immutable BigInt v = n + one; reduce!"a*b"(one, iota(one, v)) now I get an error telling me that I can't use iota because BigInt is not a float or an integer. Is this a known bug? Yes it is: https://issues.dlang.org/show_bug.cgi?id=6447 Does anyone know how to fix this 3.5 year old bug that is a blocker for doing anything with factorial and Fibonacci Series.? -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: for ranges
On Thu, 2015-01-22 at 16:48 +, bearophile via Digitalmars-d-learn wrote: > > It works for me: > > Sorry, you are right, it loops: So it is a bug, and I now have to find out how to report it! -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: for ranges
Russel Winder: However for BigInt: for(i; two..n + one) the loop starts at 0 and just keeps on going. This is clearly not good. It works for me: void main() { import std.stdio, std.bigint; immutable BigInt one = 1; immutable BigInt two = 2; uint n = 100; foreach (immutable i; two .. n + one) i.writeln; } Bye, bearophile
Re: for ranges
It works for me: Sorry, you are right, it loops: void main() { import std.stdio, std.bigint; immutable BigInt one = 1; immutable BigInt two = 2; uint n = 0; foreach (immutable i; two .. n + one) i.writeln; } Bye, bearophile
for ranges
Playing with factorial implementations, as you do. I had a D implementation using ulong. Not sensible obviously since overflow is a bit of a problem. But the code worked, as did the tests. Now converting to BigInt and… The standard explicit iteration form uses a loop: for(i; 2..n+1) for n = 0 or 1 this loop doesn't loop since the range is [,). However for BigInt: for(i; two..n + one) the loop starts at 0 and just keeps on going. This is clearly not good. Am I having a mental breakdown or is this a real bug? -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: Extracting Structure from HTML using Adam's dom.d
On Thursday, 22 January 2015 at 11:40:53 UTC, Gary Willoughby wrote: doc.querySelectorAll(`form[name="myform"] input[type="text"]`) dom.d is awesome! Something to remember btw is this also works in browser JavaScript AND css itself, since IE8 and Firefox 3.5. (no need for slow, bloated jquery) My implementation is different in some ways but mostly compatible, including some of the more advanced features like [attr^=starts_with_this] and $= and *= and so on. Also the sibling selectors ~ and +, and so on. (search for CSS selector info to learn more) dom.d also does thigns like :first-child, but it does NOT support like :nth-of-type and a few more of those newer CSS3 things. I might add them some day but I fhind this is pretty good as is.
Re: Extracting Structure from HTML using Adam's dom.d
On Thursday, 22 January 2015 at 09:27:17 UTC, Per Nordlöw wrote: BTW: Would you be interested in receiving a PR for dom.d where I replace array allocations with calls to lazy ranges? Maybe. It was on my todo list to do that for getElementsByTagName at least, which is supposed to be a live list rather than a copy of references. querySelectorAll, however, is supposed to be a copy, so don't want that to be a range. (this is to match the W3C standard and what javascript does) There are lazy range functions in there btw: element.tree is a lazy range. If you combine it with stuff like std.algorithm.filter and map, etc., it'd be easy to do a bunch of them. getElementsByTagName for example is filter!((e) => e.tagName == want)(element.tree). So the lazy implementations could just be in those terms. (actually though, that's not hard to write on the spot, so maybe it should just be explained instead of adding/changing methods. It is nice that they are plain methods instead of templates now because they can be so easily wrapped in things like script code)
Re: Extracting Structure from HTML using Adam's dom.d
On Thu, 22 Jan 2015 11:40:52 + Gary Willoughby via Digitalmars-d-learn wrote: > On Thursday, 22 January 2015 at 11:23:49 UTC, Nordlöw wrote: > > What is the meaning of selectors such as > > > > `a[href]` > > > > used in > > > > doc.querySelectorAll(`a[href]`) > > > > ? > > Select all `a` tags that have a `href` attribute. > > You can also select using the attribute value too. For example > get all the text inputs in a form: > > doc.querySelectorAll(`form[name="myform"] input[type="text"]`) > > dom.d is awesome! i miss it in Phobos. signature.asc Description: PGP signature
Re: Initialization of structure field w/o default ctor
On Thursday, 22 January 2015 at 12:45:53 UTC, drug wrote: On 22.01.2015 15:30, bearophile wrote: drug: Also can I avoid "dummy" non-default ctor for Bar? One solution: struct Foo { int foo; @disable this(); this(int foo_) pure nothrow @safe @nogc { this.foo = foo_; } } struct Bar { enum arraySize = 3; Foo[arraySize] foo = Foo(1); } void main() @safe { import std.stdio; Bar bar; bar.writeln; } Bye, bearophile Yes, that's what the doctor prescribed. Thank you! Or you can use block assignment in the constructor. struct Bar { enum ArraySize = 3; Foo[ArraySize] foo; this(string dummy) // <== here because compiler demands to initialize field foo { import std.algorithm: fill; //fill(foo[], Foo(0)); foo[] = Foo(0); // <== OK } } Compiler can recognize the block assignment as a construction for the field Bar.foo. Kenji Hara
Re: Defining a static array with values in a range
Jonathan M Davis: but that's easy fixed with some +1's. But the +1 changes the char to an int. Bye, bearophile
Re: Defining a static array with values in a range
On Thursday, January 22, 2015 10:42:59 bearophile via Digitalmars-d-learn wrote: > Jonathan M Davis: > > > auto r = chain(uiota('a', 'z'), uiota('A', 'Z'), uiota('0', > > '9')); > > Those ranges are probably open on the right. They probably are actually, since open on the right is usually how things are done, but that's easy fixed with some +1's. > Kagamin: > > > If you declare the string as immutable, the concatenation will > > be done at compile time. > > In function-scope I think you need enum. Yeah. immutable has nothing to do with compile time, though if you're talking about concatenating constants, there's a decent chance that they'd be optimized so that no concatenation occurs at runtime. However, to force anything to happen at compile time, you need to be initializing something that _has_ to be initialized at compile time (enum, static variable, direct initialization of member variable, etc.). If the compiler ever has a choice, it won't do it aside from built-in stuff that it understands well enough to translate as an optimization. - Jonathan M Davis
Re: On Variable References
On Wednesday, 21 January 2015 at 17:14:29 UTC, Meta wrote: On Wednesday, 21 January 2015 at 08:23:44 UTC, Per Nordlöw wrote: On Wednesday, 21 January 2015 at 08:22:44 UTC, Per Nordlöw wrote: int x; auto ref xr; Correction: I, of course mean, int x = 42; auto ref xr = x; Walter is strongly against adding references a la C++ to D, as he believes they are too complicated and bug prone. He's made several posts on this, but I can't find them now. In DIP69, he himself proposed `ref` locals. However, that's not the same as "references a la C++".
Re: Changing by ref a class passed in function
On Thursday, 22 January 2015 at 14:29:59 UTC, anonymous wrote: o needs to be typed as Object: Object o = new MyClass(); nullIt(o); Alternatively, templatize nullIt: void nullIt(O)(ref O o) {o = null;} auto o = new MyClass(); nullIt(o);
Re: Changing by ref a class passed in function
On Thursday, 22 January 2015 at 13:06:42 UTC, Zaher Dirkey wrote: See example bellow, i want to pass object to function nullIt and want this function to null it. import std.stdio; static if (!is(typeof(writeln))) alias writefln writeln; class MyClass{ } void nullIt(ref Object o) { o = null; } void main() { auto o = new MyClass(); nullIt(o); if (o is null) writeln("It is null"); } Thanks in advance. o needs to be typed as Object: Object o = new MyClass(); nullIt(o); If the compiler accepted your original code, it would be possible to assign a non-MyClass to o: void messWithIt(ref Object) o) {o = new Object;} auto o = new MyClass(); messWithIt(o); /* If this compiled, o would be typed as MyClass, but would not actually be a MyClass. */
Re: What to do with InvalidMemoryOperationError
On Wednesday, 21 January 2015 at 20:50:30 UTC, anonymous wrote: Or maybe dustmite can help here? If the file contains sensitive information, and you cannot reduce it to a reasonable size, you may be able to programmatically replace classes of characters with one character. E.g. replace all alphanumeric characters with 'x'. Be cautious of replacing multibyte characters with single bytes. And newlines should probably be left intact. Breaking out that single code calling File.byLine on the same file didn't trigger the error, unfortunately... I guess dustmite is my only friend here. I'm guessing some other code prior to the call together with the call is causing the bug. I'll do some more testing and see if I can reduce my code...
Changing by ref a class passed in function
See example bellow, i want to pass object to function nullIt and want this function to null it. import std.stdio; static if (!is(typeof(writeln))) alias writefln writeln; class MyClass{ } void nullIt(ref Object o) { o = null; } void main() { auto o = new MyClass(); nullIt(o); if (o is null) writeln("It is null"); } Thanks in advance.
Re: Initialization of structure field w/o default ctor
On 22.01.2015 15:30, bearophile wrote: drug: Also can I avoid "dummy" non-default ctor for Bar? One solution: struct Foo { int foo; @disable this(); this(int foo_) pure nothrow @safe @nogc { this.foo = foo_; } } struct Bar { enum arraySize = 3; Foo[arraySize] foo = Foo(1); } void main() @safe { import std.stdio; Bar bar; bar.writeln; } Bye, bearophile Yes, that's what the doctor prescribed. Thank you!
Re: Initialization of structure field w/o default ctor
drug: Also can I avoid "dummy" non-default ctor for Bar? One solution: struct Foo { int foo; @disable this(); this(int foo_) pure nothrow @safe @nogc { this.foo = foo_; } } struct Bar { enum arraySize = 3; Foo[arraySize] foo = Foo(1); } void main() @safe { import std.stdio; Bar bar; bar.writeln; } Bye, bearophile
Initialization of structure field w/o default ctor
What's the best way to initialize structure field that has no default ctor? http://dpaste.dzfl.pl/64cd0a3879fa Also can I avoid "dummy" non-default ctor for Bar?
Re: Extracting Structure from HTML using Adam's dom.d
On Thursday, 22 January 2015 at 11:23:49 UTC, Nordlöw wrote: What is the meaning of selectors such as `a[href]` used in doc.querySelectorAll(`a[href]`) ? Select all `a` tags that have a `href` attribute. You can also select using the attribute value too. For example get all the text inputs in a form: doc.querySelectorAll(`form[name="myform"] input[type="text"]`) dom.d is awesome!
Re: Extracting Structure from HTML using Adam's dom.d
On Thursday, 22 January 2015 at 02:06:16 UTC, Adam D. Ruppe wrote: You can do that with a CSS selector like: document.querySelector("#H2_A + p"); What is the meaning of selectors such as `a[href]` used in doc.querySelectorAll(`a[href]`) ?
Re: Defining a static array with values in a range
Jonathan M Davis: auto r = chain(uiota('a', 'z'), uiota('A', 'Z'), uiota('0', '9')); Those ranges are probably open on the right. In Bugzilla I have asked for the syntax iota!"[]"(a, b) to change how the extrema are handled, modelled on std.random.uniform syntax. --- Kagamin: If you declare the string as immutable, the concatenation will be done at compile time. In function-scope I think you need enum. Bye, bearophile
Re: Defining a static array with values in a range
On Thursday, 22 January 2015 at 09:26:21 UTC, tcak wrote: There are convenient constants defined in std.ascii. import std.ascii; string arr = lowercase ~ uppercase ~ digits; // also 'std.ascii.letters' gives ('A' .. 'Z' ~ 'a' .. 'z') Well, that's just disguising what we can't do. D has alot of compile time structures, even much complex than what I asked. So, this type of thing should be doable for immutable arrays. If you declare the string as immutable, the concatenation will be done at compile time.
Re: Defining a static array with values in a range
On Thursday, January 22, 2015 05:56:39 tcak via Digitalmars-d-learn wrote: > I want to define alphanumeric characters in an easy way. > Something like that: > > char[] arr = ['a'..'z', 'A'..'Z', '0'..'9']; > > Though above example doesn't work. Is there any easy way to do > this? > > I am trying to do something like EBNF definitions. So, I do not > want to use loops, or define every single thing one by one by > hand. std.range.iota is what's used to creat ranges of values, and std.array.array can be used to convert a range to an array. So, the functionality si in the standard library, even if it's not quite as clean as you might like it to be. e.g. import std.range; alias uiota = iota!(ubyte, ubyte); auto r = chain(uiota('a', 'z'), uiota('A', 'Z'), uiota('0', '9')); auto arr = cast(char[])array(r); And actually, it's worse with chars than it would be with integers, because for some reason, iota doesn't seem to want to operate directly on char, but presumably that could be fixed, which would help clean up the code. But regardless, the functionality is there without needing to add it to the language, much as it might be nice to have it in the language where it would look a bit cleaner. - Jonathan M Davis
Re: Extracting Structure from HTML using Adam's dom.d
Adam, please add more simple docs about your parser on site. Also it would be perfect to create dub, for easier including parser to project.
Re: Defining a static array with values in a range
tcak: Well, that's just disguising what we can't do. When the a..b syntax was added to foreach() someone criticized that syntax saing it's a "one trick pony", and indeed I don't know why Walter didn't make it a little more first-class. But note that in D the a..b ranges are always open on the right, so 'a'..'z' can't include the 'z'. What do you think of this? import std.stdio, std.range, std.algorithm, std.array; auto charInterval(in char a, in char b) pure nothrow @safe @nogc { return iota(a, b + 1).map!(i => cast(char)i); } void main() @safe { char[] arr = chain(charInterval('a', 'z'), charInterval('A', 'Z'), charInterval('0', '9')).array; arr.writeln; } charInterval can of course become eager too, so you just need ~ to concatenate the results: import std.stdio, std.range, std.algorithm, std.array; char[] charInterval(in char a, in char b) pure nothrow @safe { return iota(a, b + 1).map!(i => cast(char)i).array; } void main() @safe { char[] arr = charInterval('a', 'z') ~ charInterval('A', 'Z') ~ charInterval('0', '9'); arr.writeln; } Bye, bearophile
Re: Defining a static array with values in a range
On Thursday, 22 January 2015 at 07:29:05 UTC, anony wrote: On Thursday, 22 January 2015 at 05:56:40 UTC, tcak wrote: I want to define alphanumeric characters in an easy way. Something like that: char[] arr = ['a'..'z', 'A'..'Z', '0'..'9']; Though above example doesn't work. Is there any easy way to do this? I am trying to do something like EBNF definitions. So, I do not want to use loops, or define every single thing one by one by hand. There are convenient constants defined in std.ascii. import std.ascii; string arr = lowercase ~ uppercase ~ digits; // also 'std.ascii.letters' gives ('A' .. 'Z' ~ 'a' .. 'z') Well, that's just disguising what we can't do. D has alot of compile time structures, even much complex than what I asked. So, this type of thing should be doable for immutable arrays.
Re: Extracting Structure from HTML using Adam's dom.d
On Thursday, 22 January 2015 at 02:06:16 UTC, Adam D. Ruppe wrote: On Wednesday, 21 January 2015 at 23:31:26 UTC, Nordlöw wrote: This means that I need some kind of interface to extract all the contents of each paragraph that is preceeded by a heading with a specific id (say "H2_A") or content (say "More important"). How do I accomplish that? You can do that with a CSS selector like: document.querySelector("#H2_A + p"); or even document.querySelectorAll("h2 + p") to get every P immediately following a h2. My implementation works mostly the same as in javascript so you can read more about css selectors anywhere on the net like https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelector Further, is there a way to extract the "contents" only of an Element instance, that is "Stuff" from "Stuff" for each Element in the return of for example getElementsByTagName(`p`)? Element.innerText returns all the plain text inside with all tags stripped out (same as the function in IE) Element.innerHTML returns all the content inside, including tags (same as the function in all browsers) Element.firstInnerText returns all the text up to the first tag, but then stops there. (this is a custom extension) You can call those in a regular foreach loop or with something like std.algorithm.map to get the info from an array of elements. Brilliant! Thanks! BTW: Would you be interested in receiving a PR for dom.d where I replace array allocations with calls to lazy ranges?