[beginner] Why nothing is printed to stdout ?
Hi, Can someone give me a clue on why nothing is printed to stdout ? I wish a list of files with their size. code: // import std.stdio; import std.file; void main(string[] args) { foreach (DirEntry e; dirEntries(., SpanMode.shallow)) { writeln(e.name, \t, e.size); } } // Build with (dmd2): dmd -w test.d
std.container ranges
Hello there. I seem to be having problems wrapping my head around how to use the ranges in the context of containers in phobos. Specifically, I can't seem to figure out how to remove an element from a linked list. foreach(cell; organism) { if(cell.x == x cell.y == y) { organism.stableLinearRemove(cell); break; } } mind.d(123): Error: function std.container.SList!(Cell).SList.linearRemove (Range r) is not callable using argument types (Cell) mind.d(123): Error: cannot implicitly convert expression (cell) of type cell.Cell to Take!(Range) I somehow get the feeling such a basic operation should just...work? /Max
Re: [beginner] Why nothing is printed to stdout ?
On 30.10.2011 11:00, Frédéric Galusik wrote: Hi, Can someone give me a clue on why nothing is printed to stdout ? I wish a list of files with their size. code: // import std.stdio; import std.file; void main(string[] args) { foreach (DirEntry e; dirEntries(., SpanMode.shallow)) { writeln(e.name, \t, e.size); } } // Build with (dmd2): dmd -w test.d Works for me on 2.055 and 2.056 on windows. What compiler and OS are you using? c:\tempdmd -w test.d c:\temptest|more .\.a.d.un~ 5326 .\.asciidoc_user-guide.txt.un~ 942 (...)
Re: [beginner] Why nothing is printed to stdout ?
Le Sun, 30 Oct 2011 12:17:24 +0100, simendsjo a écrit : Works for me on 2.055 and 2.056 on windows. What compiler and OS are you using? c:\tempdmd -w test.d c:\temptest|more .\.a.d.un~ 5326 .\.asciidoc_user-guide.txt.un~ 942 (...) Tested with dmd 2.055 and now 2.056 on Linux ++
Re: [beginner] Why nothing is printed to stdout ?
On Sun, 30 Oct 2011 10:00:21 +, Frédéric Galusik wrote: Hi, Can someone give me a clue on why nothing is printed to stdout ? I wish a list of files with their size. code: // import std.stdio; import std.file; void main(string[] args) { foreach (DirEntry e; dirEntries(., SpanMode.shallow)) { writeln(e.name, \t, e.size); } } // Build with (dmd2): dmd -w test.d works for me, dmd 2.054 linux.
Re: std.container ranges
On Sunday, October 30, 2011 11:38:30 Max Wolter wrote: Hello there. I seem to be having problems wrapping my head around how to use the ranges in the context of containers in phobos. Specifically, I can't seem to figure out how to remove an element from a linked list. foreach(cell; organism) { if(cell.x == x cell.y == y) { organism.stableLinearRemove(cell); break; } } mind.d(123): Error: function std.container.SList!(Cell).SList.linearRemove (Range r) is not callable using argument types (Cell) mind.d(123): Error: cannot implicitly convert expression (cell) of type cell.Cell to Take!(Range) I somehow get the feeling such a basic operation should just...work? linearRemove (and stableLinearRemove) takes a _range_ not a value. cell is an element in the list, not a range over the list. The range that it takes must be either of type SList.Range or Take!(SList.Range). You get that range by slicing an SList. Take!(SList.Range) is for the case where you want only a portion of the beginning of a range rather than the whole range. Your example actually has a really simple solution: auto found = find!((a){return a.x == x a.y == y;})(organism[]); organism.stableLinearRemove(take(found, 1)); It finds the element in the list that you're looking for, and then it passes a range with that one element to stableLinearRemove so that it'll remove it. - Jonathan M Davis
Re: template expressions in C++ to an equivalent in D
I was thinking about porting http://eigen.tuxfamily.org/index.php in D. Don't we have a LinAlg library now thx to that GSoC project? I'm currently on SFML2 but it goes quite fast (thanks to Trass3r for porting SFML to D2 so a big part of the job is done). You're welcome.
Re: std.container ranges
On 10/30/2011 6:45 PM, Jonathan M Davis wrote: On Sunday, October 30, 2011 11:38:30 Max Wolter wrote: Hello there. I seem to be having problems wrapping my head around how to use the ranges in the context of containers in phobos. Specifically, I can't seem to figure out how to remove an element from a linked list. foreach(cell; organism) { if(cell.x == x cell.y == y) { organism.stableLinearRemove(cell); break; } } mind.d(123): Error: function std.container.SList!(Cell).SList.linearRemove (Range r) is not callable using argument types (Cell) mind.d(123): Error: cannot implicitly convert expression (cell) of type cell.Cell to Take!(Range) I somehow get the feeling such a basic operation should just...work? linearRemove (and stableLinearRemove) takes a _range_ not a value. cell is an element in the list, not a range over the list. The range that it takes must be either of type SList.Range or Take!(SList.Range). You get that range by slicing an SList. Take!(SList.Range) is for the case where you want only a portion of the beginning of a range rather than the whole range. Your example actually has a really simple solution: auto found = find!((a){return a.x == x a.y == y;})(organism[]); organism.stableLinearRemove(take(found, 1)); It finds the element in the list that you're looking for, and then it passes a range with that one element to stableLinearRemove so that it'll remove it. - Jonathan M Davis Hello there. Thank you very much for the explanation. However, while I really liked the concept of ranges in Andrei's book and a lot of it seems intuitive and faster than using iterators, I can't shake the feeling that in this case, it's just unnecessarily convoluted. Maybe it's just the fact that the container library is still very basic, but I don't think I should go through such a complicated procedure to remove an known element from a list. It's just not a very simple or intuitive solution, which is something I came to love D for thus far. /Max
Re: std.container ranges
On Sunday, October 30, 2011 20:53:02 Max Wolter wrote: Hello there. Thank you very much for the explanation. However, while I really liked the concept of ranges in Andrei's book and a lot of it seems intuitive and faster than using iterators, I can't shake the feeling that in this case, it's just unnecessarily convoluted. Maybe it's just the fact that the container library is still very basic, but I don't think I should go through such a complicated procedure to remove an known element from a list. It's just not a very simple or intuitive solution, which is something I came to love D for thus far. You would be doing exactly the same thing in C++ except that it would be with an iterator rather than a range. You would use find to find the iterator to the element that you wanted and then you'd pass that to the list's erase function. It is only more complicated in D in that you get a range which _starts_ with the element that you want (essentially, you get the iterator to that element plus the end iterator for the list), so if you want to remove only the first element, you take it from the front of the range. Other than that, it's the same as in C++. You can't remove an element from a list in C++ by giving that element to the list anymore than you can in D. In both cases, you need an iterator or a range. So, in comparison to C++, there's no significant difference. Now, Java does have a remove function which will take an element and remove the first occurence of that element from a list, and we could theoretically add one, but why? It would just be duplicating the functionality that find already gives. Java doesn't use iterators the way the C++ does, and it doesn't have ranges, so it _can't_ have a find function the way that C++ and D do, but D can. And in some cases, find can do it more efficiently than your loop could have. I grant you that if you're not used to using find like this in C++ (and far, far too many C++ programmers use loops instead of find - in part because pre- C++11, there were no lambdas and any time you need a custom comparison function, it's a pain to get one), then it's not immediately intuitive, but it's far more flexible and powerful than removing elements by giving the element to a remove function on the list. And if you really want to use a loop, then you still can. You just can't use foreach. for(auto r = organism[]; !r.empty; r.popFront()) { if(r.front.x == x r.front.y == y) { organism.stableLinearRemove(take(r, 1)); break; } } but that's a lot more verbose than simply using find, and as I said, in at least some cases, find can be more efficient than simply looping, since it's optimized for finding elements. - Jonathan M Davis
Re: [beginner] Why nothing is printed to stdout ?
Frédéric Galusik fr...@salixosnospam.org wrote in message news:j8j77l$pfv$1...@digitalmars.com... Hi, Can someone give me a clue on why nothing is printed to stdout ? I wish a list of files with their size. code: // import std.stdio; import std.file; void main(string[] args) { foreach (DirEntry e; dirEntries(., SpanMode.shallow)) { writeln(e.name, \t, e.size); } } // Build with (dmd2): dmd -w test.d My aplogies it you already know this, It's not my intention to be patronizing: You did run the resulting executable, right ( ./test )? And there are files in the dir its beng run from?
Re: [beginner] Why nothing is printed to stdout ?
Nick Sabalausky a@a.a wrote in message news:j8kd11$25v1$1...@digitalmars.com... Frédéric Galusik fr...@salixosnospam.org wrote in message news:j8j77l$pfv$1...@digitalmars.com... Hi, Can someone give me a clue on why nothing is printed to stdout ? I wish a list of files with their size. code: // import std.stdio; import std.file; void main(string[] args) { foreach (DirEntry e; dirEntries(., SpanMode.shallow)) { writeln(e.name, \t, e.size); } } // Build with (dmd2): dmd -w test.d My aplogies it you already know this, It's not my intention to be patronizing: You did run the resulting executable, right ( ./test )? And there are files in the dir its beng run from? And you did run it in a DOS window or from a batch file with a PAUSE to allow you to read the output ;-)
Re: [beginner] Why nothing is printed to stdout ?
On 30.10.2011 14:00, Frédéric Galusik wrote: Hi, Can someone give me a clue on why nothing is printed to stdout ? I wish a list of files with their size. code: // import std.stdio; import std.file; void main(string[] args) { foreach (DirEntry e; dirEntries(., SpanMode.shallow)) { writeln(e.name, \t, e.size); } } // Build with (dmd2): dmd -w test.d I might be way off on this. Yet this silly stuff used to catch me all the time when switching from windows to *nixes, back and forth: sh$: compile something to 'test' sh$: test no output - WTF? Ahem, it just called that command line tool named test... sh$: ./test wow, now something happens ;) -- Dmitry Olshansky
Re: std.container ranges
On Monday, October 31, 2011 12:11:45 Mike Parker wrote: On 10/31/2011 5:28 AM, Jonathan M Davis wrote: So, in comparison to C++, there's no significant difference. Now, Java does have a remove function which will take an element and remove the first occurence of that element from a list, and we could theoretically add one, but why? IMO, it's much more intuitive to say list.remove(item). It's the first thing a lot of people expect coming from a Java background, that's for sure. The first time I tried to use SList, being unfamiliar with ranges as I was, it took a while to figure out what I needed to do. IIRC, I had to post here to ask. The problem is that you really have to understand ranges and the Phobos functions that manipulate them before you can begin to use containers. I think that's wrong. Ideally, containers should be usable without having to know about find and Take and whatever else. This isn't the first time this question has come up in the NG and I've got a feeling it won't be the last. At the very least it would be nice to see something in the documentation tying std.algorithm and std.range together with std.container. Something to point the way for those to whom it isn't obvious. I definitely agree that the documentation should be improved to at least give the basic example of using find with linearRemove. Adding something similar to C++'s remove_if which removes all elements which match a predicate could also be useful, but I don't at all agree that there should be a function which takes an element and removes the first element which is equal to it. find allows you to do that just fine, and such a remove function would simply be duplicating its functionality. As for understanding ranges, it's definitely true that there needs to be more documentation and/or articles on them so that the concept can better communicated. That's a definite flaw in the current documentation. But there's a lot in Phobos which you're just not going to be able to use if you don't understand ranges, and the library would definitely be worse if it used them less, since they're such a powerful concept, so I think that the problem is the lack of communication on ranges, not the fact that ranges are used so heavily in Phobos. - Jonathan M Davis