Re: RosettaCode factorial needs to use longs
On Sat, 2014-03-08 at 21:20 +, bearophile wrote: Jay Norwood: http://rosettacode.org/wiki/Factorial#D to whomever is maintaining these: Need to change all ints to longs in this example to get the displayed results since the 15! result requires more than 32 bits. I am maintaining the D entries, I have fixed that factorial code, thank you. I don't know why the shown output was for 64 bit numbers. That page appears to quietly ignore the fact that those languages that use hardware integers cannot correctly compute factorial(25), let alone factorial(1000), at least assuming 64-bit integer hardware, whereas those languages using hardware/software numbers as needed (e.g. Python, etc.) have no problem. And there doesn't seem to be mention of tail recursion and tail recursion optimization much either. Pity really, these comparative pages could be so useful but generally end up as dumping grounds. Good to see the D examples being more careful about the internal vs external iteration and recursion vs tail recursion issue. I wonder though if it would be better to split the examples up rather than have a single entry. Nothing to do with the code content just to do with the visual perception. The D code looks big and so bad, whereas those that split up the examples look small and so good. Can I suggest partitioning the D entry into four sub entries so as to make it clear that explicit iteration, implicit iteration, recursion and tail recursion are all covered? -- 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: New to std.algorithm and generics, no idea how to make simple things to work
On Friday, 7 March 2014 at 19:53:04 UTC, newguy wrote: I really can't wrap my head around these. I fought whole day trying to figure out how to do the simplest thing one can imagine: remove an element from a doubly linked list. Here's what I've tried, see if there is a recurring mistake of thought or something: import std.stdio; import std.algorithm; import std.range; import std.container; struct Point { int x; int y; } void main() { DList!Point points; points.insert(Point(0,0)); points.insert(Point(10,10)); points.insert(Point(5,5)); points.insert(Point(20,20)); points.remove(takeOne(find!(p = p.x 7)(points[]))); // test.d(18): Error: function std.container.DList!(Point).DList.remove (Range r) is not callable using argument types (Result) points.linearRemove(takeOne(find!(p = p.x 7)(points[]))); // test.d(21): Error: template std.container.DList!(Point).DList.linearRemove cannot deduce function from argument types !()(Result), candidates are: // /usr/include/dmd/phobos/std/container.d(2234): std.container.DList!(Point).DList.linearRemove(R)(R r) if (is(R == Range)) // /usr/include/dmd/phobos/std/container.d(2240): std.container.DList!(Point).DList.linearRemove(R)(R r) if (is(R == Range)) // /usr/include/dmd/phobos/std/container.d(2253): std.container.DList!(Point).DList.linearRemove(R)(R r) if (is(R == Take!Range)) points.remove(find!(p = p.x 7)(points[])); // 0 points.remove(takeOne(filter!(p = p.x 7)(points[]))); // test.d(30): Error: function std.container.DList!(Point).DList.remove (Range r) is not callable using argument types (Result) points.remove(filter!(p = p.x 7)(points[])); // test.d(33): Error: function std.container.DList!(Point).DList.remove (Range r) is not callable using argument types (FilterResult!(__lambda3, Range)) points.linearRemove(filter!(p = p.x 7)(points[])); // test.d(36): Error: template std.container.DList!(Point).DList.linearRemove cannot deduce function from argument types !()(FilterResult!(__lambda1, Range)), candidates are: // /usr/include/dmd/phobos/std/container.d(2234): std.container.DList!(Point).DList.linearRemove(R)(R r) if (is(R == Range)) // /usr/include/dmd/phobos/std/container.d(2240): std.container.DList!(Point).DList.linearRemove(R)(R r) if (is(R == Range)) // /usr/include/dmd/phobos/std/container.d(2253): std.container.DList!(Point).DList.linearRemove(R)(R r) if (is(R == Take!Range)) foreach (Point p; points) { if (p.x 7) { //points.remove(/* Somehow get the range */); break; } } foreach (Point p; points) { writeln(p.x); } } Purpose is to remove one element that matches predicate, or any amount really. Now DList.remove is defined as Range remove(Range r) and filter is auto filter(Range)(Range rs) if (isInputRange!(Unqual!Range)) with explanation The call filter!(predicate)(range) returns a new range only containing elements x in range for which predicate(x) is true. So if I understand correctly, filter should return a range that I can remove from the list. Why isn't this working? std.container isn't great and hasn't received enough attention. There are plans to improve it, but I believe we're waiting on std.allocator and possibly a higher level layer on top of it before any significant revamp is made. It is perfectly usable in its current state however, just not always totally pleasant. Don't let this put you off std.algorithm and std.range, which are both very high quality.
Re: Hacking DMD to Query Context Information at Source File Offset
On 2014-03-09 00:32, Nordlöw wrote: Where should I put logic-and-printfs in DMD to show lexical, syntactic and semantic information about a symbol and/or expression at given position in the source file being parsed. I'm guessing for some iteration in the lexical, syntactic and semantic passes respectively where I need to check whether my query offset lies inside inside some context window currently being analyzed by DMD. The lexer is implemented in lexer.c, the parser in parser.c and the semantics are implemented in the AST within these three functions semantic, semantic2 and semantic3. For more information see: http://wiki.dlang.org/DMD_Source_Guide -- /Jacob Carlborg
Re: RosettaCode factorial needs to use longs
Russel Winder: That page appears to quietly ignore the fact that those languages that use hardware integers cannot correctly compute factorial(25), The D entry has the same problem. But a pre-condition could be added to limit the input. I wonder though if it would be better to split the examples up rather than have a single entry. Nothing to do with the code content just to do with the visual perception. The D code looks big and so bad, whereas those that split up the examples look small and so good. Can I suggest partitioning the D entry into four sub entries so as to make it clear that explicit iteration, implicit iteration, recursion and tail recursion are all covered? OK, I will split the D entry in four parts, but I think an average programmer is able to see that code is made of four separated functions that do the same thing in different ways and it's not a single large chunk of code needed to do the task. That's why I didn't bother with four entries. (In Rosettacode some D entries are designed to be very short and sweet, other to be very fast, other to show the kind of elaborate code full of static typing and run-time tests you use in a piece of code that should be correct, and so on. So the code style varies on purpose to show various ways to write D. You can see this in the N-Queens problem D entries and elsewhere). Bye, bearophile
Re: linear search using 'find' on an array of structs?
assert(!find!(toLower(a) == b)(s, hello).empty); assert(!find!(toLower(a) == b)(clist.name, name2).empty); But clist is an array of c's, it has no `.name` field by itself. So, put the `.name` call inside the comparator: assert( find!(toLower(a.name) == b)(clist http://clist.name/*,* name2).empty); This gives me this code: import std.algorithm: find; import std.array: empty; import std.uni: toLower; struct C // Use UpperCase for you user-defined types { int idx; string name; } C[] clist = [ {1, name1}, {2, name2}, { 3, name3 } ]; void main() // no need to return 0 { auto target = clist.find!((a,b) = toLower(a.name) == b)(name2); assert(!target.empty); } Using UFCS (Universal Function Call Syntax) to tranform f(a,b) into a.f(b). I used it on `find`. I went looking to replace several foreach statements. Can 'find' (in understand it's just a linear search) be used on an array of structures like above. Sure, as long as you tell it how you will get the info from the range (it defaults to simple equality). Example pulled and modified. Above code gives me (naturally) - ... no property 'name' for type 'cp[]'. Interestingly, I had accidentally coded the commented out line before and it compiles correctly but will (as you guessed it) fail. I never use pointers in D. I suppose the `.name` call is propagated to the array elements?
Re: RosettaCode factorial needs to use longs
On Sun, 2014-03-09 at 10:28 +, bearophile wrote: Russel Winder: That page appears to quietly ignore the fact that those languages that use hardware integers cannot correctly compute factorial(25), The D entry has the same problem. But a pre-condition could be added to limit the input. I think leaving the code as is, my point was that the whole page has the problem that most of the implementation aren't actually useful in practice, even though for any combinatoric problem (cf. bioinformatics, quant calculations, etc.) this is a rather important function to get right :-) I wonder though if it would be better to split the examples up rather than have a single entry. Nothing to do with the code content just to do with the visual perception. The D code looks big and so bad, whereas those that split up the examples look small and so good. Can I suggest partitioning the D entry into four sub entries so as to make it clear that explicit iteration, implicit iteration, recursion and tail recursion are all covered? OK, I will split the D entry in four parts, but I think an average programmer is able to see that code is made of four separated functions that do the same thing in different ways and it's not a single large chunk of code needed to do the task. That's why I didn't bother with four entries. I don't disagree, any half-way competent programmer should certainly read it that way. The problem is that when scrolling down the page, the visual impact is far more important for attention grabbing than the content itself. (In Rosettacode some D entries are designed to be very short and sweet, other to be very fast, other to show the kind of elaborate code full of static typing and run-time tests you use in a piece of code that should be correct, and so on. So the code style varies on purpose to show various ways to write D. You can see this in the N-Queens problem D entries and elsewhere). It might be a good move to collect together all the URLs of codes like this so that there is a quick way of reviewing and providing feedback. Minimizing effort to do reviewing makes it more likely to happen. -- 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
Linux Dynamic Loading of shared libraries
Martin Nowak's Gihub druntime Page has module main; import core.runtime, core.thread; void main() { auto lib = Runtime.loadLibrary(./liba.so); auto thr = new Thread({ auto lib = Runtime.loadLibrary(./liba.so); Runtime.unloadLibrary(lib); }); thr.start(); thr.join(); Runtime.unloadLibrary(lib); } module liba; import std.stdio; shared static this() { writeln(shared static this()); } shared static ~this() { writeln(shared static ~this()); } static this() { writeln(static this()); } static ~this() { writeln(static ~this()); } Now suppose that my D shared library contains a class, rather that just module ctors/dtors, how do I go about creating an instance of that class and using its methods? Steve
build dustmite
Firstly build way describe at https://github.com/CyberShadow/DustMite/wiki/Building-DustMite seem to be wrong as dsplit.d was rename as splitter.d Secondly build fail take a look behind $ ldc2 -release -w -g -O3 dustmite.d splitter.d -of dustmite splitter.d(262): Error: delegate splitter.DSplitter.__lambda22 function literals cannot be class members splitter.d(262): Error: delegate splitter.DSplitter.__lambda22 circular dependency. Functions cannot be interpreted while being compiled splitter.d(284):called from here: (*_error_() { Token[string] lookup; auto t = Token.generated0; Token add(string s) { auto p = s in lookup; if (p) return *p; return lookup[s] = t++; } foreach (pair; pairs) { add(pair.start); add(pair.end); } foreach (i, synonyms; separators) { foreach (sep; synonyms) { add(sep); } } return lookup; } )()
Re: RosettaCode factorial needs to use longs
Russel Winder: It might be a good move to collect together all the URLs of codes like this so that there is a quick way of reviewing and providing feedback. Minimizing effort to do reviewing makes it more likely to happen. Code like what? What URLs? Currently I am maintaining about 850 D source files (plus few others in Haskell, Python, C, C++): http://rosettacode.org/wiki/Category:D Bye, bearophile
Re: strange compilation error
Jack Applegame: This fails to compile http://dpaste.dzfl.pl/e9a90e808af4 There are various ways to avoid this problem, using static classes/structs is one of the simplest: void main() { static class B { int a; this(int aa) { a = aa; } } auto foo = { return new B(1); }; } Bye, bearophile
Re: Linux Dynamic Loading of shared libraries
Now suppose that my D shared library contains a class, rather that just module ctors/dtors, how do I go about creating an instance of that class and using its methods? Steve For this, you create an Interface that matches to the method declaration of your class. But notice that instead of defining methods, you will define attributes those types' match to that class's methods. I did this before and it works. At least with Posix dlsym function's help.
Re: strange compilation error
On Sunday, 9 March 2014 at 12:21:18 UTC, bearophile wrote: Jack Applegame: This fails to compile http://dpaste.dzfl.pl/e9a90e808af4 There are various ways to avoid this problem, using static classes/structs is one of the simplest: void main() { static class B { int a; this(int aa) { a = aa; } } auto foo = { return new B(1); }; } Bye, bearophile thanks
Re: strange compilation error
On Sunday, 9 March 2014 at 12:21:18 UTC, bearophile wrote: Jack Applegame: This fails to compile http://dpaste.dzfl.pl/e9a90e808af4 There are various ways to avoid this problem, using static classes/structs is one of the simplest: void main() { static class B { int a; this(int aa) { a = aa; } } auto foo = { return new B(1); }; } The error message is confusing, though. main is not a nested function, rather it's a function _containing_ a nested class...
Re: strange compilation error
Marc Schütz: The error message is confusing, though. main is not a nested function, rather it's a function _containing_ a nested class... If you don't like the error message then I suggest to open in Bugzilla a diagnostic bug report (if you want with minor importance). Bye, bearophile
Re: Linux Dynamic Loading of shared libraries
On Sunday, 9 March 2014 at 12:07:22 UTC, Steve Teale wrote: Martin Nowak's Gihub druntime Page has module main; import core.runtime, core.thread; void main() { auto lib = Runtime.loadLibrary(./liba.so); auto thr = new Thread({ auto lib = Runtime.loadLibrary(./liba.so); Runtime.unloadLibrary(lib); }); thr.start(); thr.join(); Runtime.unloadLibrary(lib); } module liba; import std.stdio; shared static this() { writeln(shared static this()); } shared static ~this() { writeln(shared static ~this()); } static this() { writeln(static this()); } static ~this() { writeln(static ~this()); } Now suppose that my D shared library contains a class, rather that just module ctors/dtors, how do I go about creating an instance of that class and using its methods? Steve If you know the fully qualified name of the class and an interface auto newClassIF = cast(interfacename)object.factory(libmodule.libclass);