Re: Circular Buffer
On Mon, 2014-02-10 at 09:16 +, Gary Willoughby wrote: On Monday, 10 February 2014 at 03:14:31 UTC, Jonathan Dunlap wrote: (disclaimer: I'm new around here) Is it possible to cycle backwards? If not, what's the best approach? import std.algorithm; import std.array; import std.range; import std.stdio; void main(string[] args) { auto data = [1,2,3]; assert(data.cycle.take(5).array == [1,2,3,1,2]); assert(data.retro.cycle.take(5).array == [3,2,1,3,2]); } As Gary is aware, I posted this problem to ACCU asking for a C++ version. I think Steve Love has had a go with an added range library not just pure C++14. I'll post when I have looked at his code, and ensured it works. He is using Catch for testing so I suspect it will. I had a quick go at doing a Python 3 version using PyTest: def provide(sourceSequence, resultLength): return (sourceSequence[i % len(sourceSequence)] for i in range(resultLength)) def provideReverse(sourceSequence, resultLength): sourceLength = len(sourceSequence) return (sourceSequence[sourceLength - 1 - i % sourceLength] for i in range(resultLength)) data = [1, 2, 3] def test_forward(): assert tuple(provide(data, 5)) == (1,2,3,1,2) def test_reverse(): assert tuple(provideReverse(data, 5)) == (3,2,1,3,2) -- 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
Re: Circular Buffer
On Mon, 2014-02-10 at 11:33 +, bearophile wrote: Russel Winder: This really needs to get onto the D website somewhere. retro+cycle is very simple code, you can also combine them: alias retroCycle = compose!(cycle, retro); point-free composition. We like this :-) Ranges and algorithms can be combined together in so many ways :-) For an imperative/OO programmer writing code based on lazy ranges and higher order functions is a new kind of programming that should be learnt patiently, but it's not hard and it doesn't contain many low-level pitfalls :-) Tell me about it. I run training courses trying to get people to do this higher-order stuff, and meta-object protocol stuff, in Python, Java and Groovy (with some Scala) and some get it and some don't. Rule 1: don't mention monads. -- 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
Re: Ranges, constantly frustrating
On Tue, 11 Feb 2014 17:11:46 -, Ali Çehreli acehr...@yahoo.com wrote: On 02/11/2014 06:25 AM, Rene Zwanenburg wrote: On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote: foreach (i, line; range.take(4)) //Error: cannot infer argument types { ..etc.. } foreach (i, line; iota(size_t.max).zip(range.take(4))) { } There is also the following, relying on tuples' automatic expansion in foreach: foreach (i, element; zip(sequence!n, range.take(4))) { // ... } Thanks for the workarounds. :) Both seem needlessly opaque, but I realise you're not suggesting these are better than the original, just that they actually work today. R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Re: Ranges, constantly frustrating
On Tue, 11 Feb 2014 19:48:40 -, Jesse Phillips jesse.k.phillip...@gmail.com wrote: On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote: Things like this should just work.. File input ... auto range = input.byLine(); while(!range.empty) { range.popFront(); foreach (i, line; range.take(4)) //Error: cannot infer argument types { ..etc.. } range.popFront(); } Tried adding 'int' and 'char[]' or 'auto' .. no dice. Can someone explain why this fails, and if this is a permanent or temporary limitation of D/MD. R In case the other replies weren't clear enough. A range does not have an index. It isn't *required* to (input/forward), but it could (random access). I think we even have a template to test if it's indexable as we can optimise some algorithms based on this. What do you expect 'i' to be? Is it the line number? Is it the index within the line where 'take' begins? Where 'take' stops? If I say take(5) I expect 0,1,2,3,4. The index into the take range itself. The reason I wanted it was I was parsing blocks of data over 6 lines - I wanted to ignore the first and last and process the middle 4. In fact I wanted to skip the 2nd of those 4 as well, but there was not single function (I could find) which would do all that so I coded the while above. There is a feature of foreach and tuple() which results in the tuple getting expanded automatically. And also the opApply overload taking a delegate with both parameters. byLine has its own issues with reuse of the buffer, it isn't inherent to ranges. I haven't really used it (needed it from std.process), when I wanted to read a large file I went with wrapping std.mmap: https://github.com/JesseKPhillips/libosm/blob/master/source/util/filerange.d Cool, thanks. R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Re: Ranges, constantly frustrating
On Wednesday, 12 February 2014 at 10:44:57 UTC, Regan Heath wrote: Ahh.. so this is a limitation of the range interface. Any plans to fix this? R Did my original reply not arrive? It is the first reply in the thread... Reproduced: See this pull request[1] and the linked enhancement report. Also note that calling `r.popFront()` without checking `r.empty` is a program error (so it's recommended to at least put in an assert). [1] https://github.com/D-Programming-Language/phobos/pull/1866
Minimal D executable on Windows x32
Did it just for fun, maybe it will help someone :) Requires UniLink: ftp://ftp.styx.cabel.net/pub/UniLink main.d extern(C) void _acrtused_con() { mainFunc(); } extern(Windows) { int MessageBoxA(uint hWnd, char* lpText, char* lpCaption, uint uType); void ExitProcess(uint uExitCode); } void mainFunc() { MessageBoxA(0, cast(char*) Hello, World!.ptr, cast(char*) Nano-D.ptr, 0); ExitProcess(0); } object.d module object; struct ModuleInfo { // nothing } Compiling: dmd -c main.d Linking: ulink.exe -e__acrtused_con -L{YOUR_PATH_TO_DMD}\windows\lib -zkernel32;user32 main.obj Size of main.exe is 1068 bytes.
Re: Thrift maintained..?
On Wednesday, 12 February 2014 at 07:53:44 UTC, simendsjo wrote: Well, that seems quite unprofessional.. Not fixing bugs that makes parts of their repository unusable.. To be fair he wasn't able to reproduce the problem. It built fine on his box. They list automake 1.9 as the requirement so presumably he isn't running 1.13 and that's why he didn't run into it. I'm hoping to fix it in 1.13 in a way that also works for earlier versions of automake. If I can I'll reopen an contribute a patch. Now if I can just figure out how automake works
Multiple-OS Header Access
Alright, so I'm making a little utility, and it looks like I'm going to need to access OS-specific header functions on each operating system in order to make it work, because 1) I want to get a list of all active processes and their names and 2) I want to periodically check which window currently has focus. I've already found SetWinEventHook and GetForegroundWindow (for checking the foreground window and setting an event hook for focus change), and also EnumProcesses for getting the list, but these are all Windows specific, not to mention C++ specific on Windows, and I can't see any way of making one set of functions that would work on Windows, Mac and Linux. So anyway, my question is, does D have the capability of interfacing with Linux and Mac such that those functions are accessible, and/or would I be better of working in C++ for this particular venture? How would I go about accessing those functions, and using the information they provide in D?
Re: Multiple-OS Header Access
On Wednesday, 12 February 2014 at 17:21:52 UTC, Malkierian wrote: not to mention C++ specific on Windows They aren't really C++ specific, you can use them from a lot of languages, including C and D. So anyway, my question is, does D have the capability of interfacing with Linux and Mac such that those functions are accessible Not those specific functions, since they are part of Windows. Similar functions on Linux would be... I think you'd have to read the /proc directory for directories with numeric names, then inside there, you can read a file called cmdline to get the process name (sort of, it is the command line used to launch it) and other stuff. The name of the directory is the process id. To get the currently focused window on linux, you could do it by asking the X server. The functions XGetInputFocus or fetching the _NET_ACTIVE_WINDOW atom should give a usable window id. Tying window IDs to process IDs is a bit harder, you might be able to get it by looking for a _NET_WM_PID atom... which is opt-in by the application, I don't think the X server necessarily knows the pid of the owner (indeed, the owner might be running on a different computer anyway). So yeah, kinda in depth low level stuff. I don't know if there's any library to help with this. I have no idea about Mac (or even other unixes for that matter). , and/or would I be better of working in C++ for this particular venture? Linux is a pain in the ass to program for in any language... but if you can do it in C or C++, you can mostly just to the same thing in D. Might have to copy/paste function prototypes though.
Re: Custom default exception handler?
On Wednesday, 12 February 2014 at 02:41:34 UTC, Nick Sabalausky wrote: On 2/11/2014 6:35 PM, Sean Kelly wrote: Throw a static exception (maybe even derived directly from Throwable), I assume then that throwing something directly derived from Throwable would still run cleanup code (like scope guards and finally) like throwing Exception would? Or is it like throwing an Error, skipping cleanup code? Everything runs cleanup code right now, unless someone changed things on me. But if a change were made, I'd say that only Error and its children would skip cleanup. The default would be to perform cleanup. That lets users create their own exception hierarchies.
Re: Custom default exception handler?
On Wednesday, 12 February 2014 at 03:31:38 UTC, Nick Sabalausky wrote: Hmm, my custom toString isn't being executed. Am I doing something wrong here? Same result if I inherit direct from Throwable instead of Exception. class Fail : Exception { private this() { super(null); } private static Fail opCall(string msg, string file=__FILE__, int line=__LINE__) { auto f = cast(Fail) cast(void*) Fail.classinfo.init; f.msg = msg; f.file = file; f.line = line; return f; } override string toString() { writeln(In Fail.toString()); return someapp: ERROR: ~msg; } } It looks like this has changed, and the method that's called now is: void toString(scope void delegate(in char[]) sink) const; I suspect this has broken a lot of custom exception messages, since everything in core.exception still uses toString() for its output.
Re: Multiple-OS Header Access
On Wednesday, 12 February 2014 at 18:26:45 UTC, Jakob Ovrum wrote: I find POSIX much more palatable than the Windows API. POSIX is ok for what it does, but it doesn't actually do very much. The topics here, for example, are not standardized (I'm pretty sure anyway). Even de-facto standards like X11 is pretty minimal; you end having to do things yourself or get random third party libraries (which are often poorly documented and the user may not have installed!). Biggest examples are GTK and Qt for doing gui widgets since X doesn't do that, but even for basic drawing, X is pretty minimal... yet pretty complex in the stuff you have to get right to do that minimal amount of work. An example that irks me is the lack of draw this image sized for this rectangle; there's nothing like StretchBlt from Windows. Instead, you need to create an image (being sure to get the right format) and resize it yourself before sending those bits down to the display. (which btw cannot be compressed in the X protocol itself!) But generally, I find when I have a task I need to get done, Win32 offers some kind of function to do it and it doesn't take too long to find it on MSDN. It might be an ugly function with a dozen arguments wrapped up in a struct, but it gets the job done and just works for the user. On Linux, gotta go hunting for third party libraries... which might not work on the user's environment... or cannot complete the task at all (terminal emulators SUCK! want a key up notification? too bad) or can only very inefficiently (anti-aliased text on X for example... you get a screenshot, draw the text, then draw the modified image. The network won't be very transparent through that process, I tell you what.) Though I guess GUI programming can be a little more involved, having to figure out which libraries to code against. Yeah, that's a big problem too.
Re: Multiple-OS Header Access
On 2/12/2014 12:21 PM, Malkierian wrote: Alright, so I'm making a little utility, and it looks like I'm going to need to access OS-specific header functions on each operating system in order to make it work, because 1) I want to get a list of all active processes and their names and 2) I want to periodically check which window currently has focus. I've already found SetWinEventHook and GetForegroundWindow (for checking the foreground window and setting an event hook for focus change), and also EnumProcesses for getting the list, but these are all Windows specific, not to mention C++ specific on Windows, and I can't see any way of making one set of functions that would work on Windows, Mac and Linux. So anyway, my question is, does D have the capability of interfacing with Linux and Mac such that those functions are accessible, and/or would I be better of working in C++ for this particular venture? How would I go about accessing those functions, and using the information they provide in D? Regarding actually accessing system libraries: --- The system APIs on Windows and Linux are in C (and sometimes some C++ on Windows), and D is perfectly capable of linking with C (and a certain subset of C++), so yes this is possible. All you have to do to access these system libraries from D is write the extern declarations as described here: http://dlang.org/interfaceToC.html (C, for most system calls) http://dlang.org/cpp_interface.html (C++, in case you happen to need it) All in all, it's actually quite simple. There's also a really good series of articles out somewhere that explains more about it. Unfortunately I don't have a link to it handy right now, but you could maybe search for it, or maybe someone else knows. OSX might be a little trickier since a lot of its libs are in Objective-C instead of straight C, but Objective-C is still link-compatible with C just like D is, so it's definitely possible. Also, keep in mind that a lot of OS-specific calls are already available through D's standard library. See std.windows, std.linux, std.c.windows, std.c.posix, and I think there's some others. As for dealing with differences between OSes: --- In C/C++, you'd do this with something like #ifdef. In D you use version(). //One way to do it: version(Windows) void myOwnWrapperFunction(blah blah blah) { // Use the Windows API } version(linux) void myOwnWrapperFunction(blah blah blah) { // Use the Linux API } version(OSX) void myOwnWrapperFunction(blah blah blah) { // Use the OSX API } //Another way to do it: void myOwnWrapperFunction(blah blah blah) { version(windows) { // Do it the Windows way } else version(OSX) { // Do it the Mac way } else version(Posix) { // Do it the Posix way } else static assert(0, This OS not supported.) } Or any combination of the above. This way all the OS-specific differences are hidden inside your myOwnWrapperFunction, and you can call it from any OS you've supported. As for knowing what OS-specific functions to use: --- For that, you'd have to look at the documentation for the OS's API.
Re: Custom default exception handler?
On Wednesday, 12 February 2014 at 22:42:45 UTC, Nick Sabalausky wrote: Hmm, that still isn't getting called for me either: void toString(scope void delegate(in char[]) sink) const { import std.stdio; writeln(In Fail.toString()); sink(someapp: ERROR: ~msg); } Tried on both 2.064.2 and 2.065-b3. Could it be that the custom toString just doesn't get run for static exceptions? It should. I'm not entirely sure why it isn't working. For reference, the relevant code is in rt/dmain2.d (printThrowable) and object_.d (toString(sink)) in Druntime.
std.random.uniform for enums
I'm confused about how to use random.uniform to select a member of an enum. Say I have an enum like enum Animals { cat = 0, dog = 1, chimpanzee = 2 } I want to select a random animal. So far I've been trying to do uniform(Animals), but every time I try to compile that, I get a does not match any function template declaration error. Am I misunderstanding how this function is meant to be used?
Re: std.random.uniform for enums
On Thursday, 13 February 2014 at 02:02:38 UTC, Anton wrote: I'm confused about how to use random.uniform to select a member of an enum. Say I have an enum like enum Animals { cat = 0, dog = 1, chimpanzee = 2 } I want to select a random animal. So far I've been trying to do uniform(Animals), but every time I try to compile that, I get a does not match any function template declaration error. Am I misunderstanding how this function is meant to be used? The problem with using `uniform` for enums is that not all enums are sequential without holes, which would make the `uniform` implementation quite non-trivial if it were to try to handle enums generically. If you know your enum is sequential and doesn't have any holes, assume responsibility for that fact with a cast: --- enum Animals { cat = 0, dog = 1, chimpanzee = 2 } void main() { import std.random, std.stdio; foreach(immutable _; 0 .. 10) writeln(cast(Animals)uniform![](Animals.min, Animals.max)); } ---
Re: std.random.uniform for enums
On Thursday, 13 February 2014 at 02:02:38 UTC, Anton wrote: Am I misunderstanding how this function is meant to be used? Yeah, uniform takes two numerical arguments: a min and a max. It returns a value between the two, including the min, but not including the max. So int a = uniform(0, 10); // returns 0,1,2,3,4,5,6,7,8, or 9. You could do a random animal by doing `cast(Animals) uniform(0, 3);`, or getting fancier with reflection stuff... that'd take a few more lines, use __traits(getMember) and __traits(allMembers) to randomize rather than .min and .max because the latter wouldn't handle holes in the values.
Re: std.random.uniform for enums
On Thursday, 13 February 2014 at 02:14:02 UTC, Jakob Ovrum wrote: On Thursday, 13 February 2014 at 02:02:38 UTC, Anton wrote: I'm confused about how to use random.uniform to select a member of an enum. Say I have an enum like enum Animals { cat = 0, dog = 1, chimpanzee = 2 } I want to select a random animal. So far I've been trying to do uniform(Animals), but every time I try to compile that, I get a does not match any function template declaration error. Am I misunderstanding how this function is meant to be used? The problem with using `uniform` for enums is that not all enums are sequential without holes, which would make the `uniform` implementation quite non-trivial if it were to try to handle enums generically. If you know your enum is sequential and doesn't have any holes, assume responsibility for that fact with a cast: --- enum Animals { cat = 0, dog = 1, chimpanzee = 2 } void main() { import std.random, std.stdio; foreach(immutable _; 0 .. 10) writeln(cast(Animals)uniform![](Animals.min, Animals.max)); } --- Could you not simply select one at random by name? Even though the values of the enum may not be sequential the keys are.
Re: std.random.uniform for enums
I guess I'm mostly confused because the description for one of the templates of std.random.uniform says Returns a uniformly selected member of enum E. If no random number generator is passed, uses the default rndGen. So I was wondering why that functionality didn't seem to work as I thought it would. Otherwise, thanks for the workarounds.
Re: std.random.uniform for enums
On Thursday, 13 February 2014 at 02:52:44 UTC, Anton wrote: I guess I'm mostly confused because the description for one of the templates of std.random.uniform says Returns a uniformly selected member of enum E. Oooh, I didn't know it had one of those, the documentation can be so hard to read sometimes. Try this then: auto randomAnimal = uniform!Animals(); The enum E there is a compile time argument, so you need the ! in there to pass them. (uniform(Animal) would be trying to send it as a run time argument which doesn't work for types)
Re: std.random.uniform for enums
On Thursday, 13 February 2014 at 02:30:47 UTC, Frustrated wrote: Could you not simply select one at random by name? Even though the values of the enum may not be sequential the keys are. Yeah, and there is apparently already an overload that does that. Regardless, I wrote a version that has an optimized path for sequential enums: https://gist.github.com/JakobOvrum/8968977
Re: std.random.uniform for enums
On Thursday, 13 February 2014 at 03:04:06 UTC, Jakob Ovrum wrote: On Thursday, 13 February 2014 at 02:30:47 UTC, Frustrated wrote: Could you not simply select one at random by name? Even though the values of the enum may not be sequential the keys are. Yeah, and there is apparently already an overload that does that. Regardless, I wrote a version that has an optimized path for sequential enums: https://gist.github.com/JakobOvrum/8968977 It's also worth noting that both the existing std.random.uniform and the one I posted would fail when the base type of the enum has mutable indirection.
Re: std.random.uniform for enums
On Thursday, 13 February 2014 at 02:30:47 UTC, Frustrated wrote: On Thursday, 13 February 2014 at 02:14:02 UTC, Jakob Ovrum wrote: On Thursday, 13 February 2014 at 02:02:38 UTC, Anton wrote: I'm confused about how to use random.uniform to select a member of an enum. Say I have an enum like enum Animals { cat = 0, dog = 1, chimpanzee = 2 } I want to select a random animal. So far I've been trying to do uniform(Animals), but every time I try to compile that, I get a does not match any function template declaration error. Am I misunderstanding how this function is meant to be used? The problem with using `uniform` for enums is that not all enums are sequential without holes, which would make the `uniform` implementation quite non-trivial if it were to try to handle enums generically. If you know your enum is sequential and doesn't have any holes, assume responsibility for that fact with a cast: --- enum Animals { cat = 0, dog = 1, chimpanzee = 2 } void main() { import std.random, std.stdio; foreach(immutable _; 0 .. 10) writeln(cast(Animals)uniform![](Animals.min, Animals.max)); } --- Could you not simply select one at random by name? Even though the values of the enum may not be sequential the keys are. import std.random, std.stdio, std.traits; enum Animals { dog = dog, cat = cat, fox = fox, cow = cow, } void main() { auto animals = [EnumMembers!Animals]; auto rnd = uniform![)(0, animals.length); writeln(animals[rnd]); } You have to wrap the EnumMembers template in an array, because tuples can only be sliced at compile-time, and uniform doesn't work at compile time.
Re: std.random.uniform for enums
On Thursday, 13 February 2014 at 02:59:01 UTC, Adam D. Ruppe wrote: On Thursday, 13 February 2014 at 02:52:44 UTC, Anton wrote: I guess I'm mostly confused because the description for one of the templates of std.random.uniform says Returns a uniformly selected member of enum E. Oooh, I didn't know it had one of those, the documentation can be so hard to read sometimes. Try this then: auto randomAnimal = uniform!Animals(); The enum E there is a compile time argument, so you need the ! in there to pass them. (uniform(Animal) would be trying to send it as a run time argument which doesn't work for types) Yeah, the docs aren't the easiest to read, especially coming from Python. But thanks a lot for your help. Using ! does the trick.
Can't get the tools suite to build using the posix.mak make file
I created a straightforward script for x86_64 Linux which comes directly from the wiki: http://wiki.dlang.org/Building_DMD. The script is to automate pulling down a development snapshot of the ecosystem and build + install it. The build and test process for DMD, D runtime, and Phobos work fine. I am having trouble getting the tools suite (https://github.com/D-Programming-Language/tools) to build successfully. I get the error shown below from the linked script (here is a link to the same text: https://gist.github.com/joichiro/8971131). The results below were run against the following commits DMD = 5f4764ad465a02b2c06fa9cd5480a2730cd34dcc D runtime = 29a05b0fb8a8fc514803f122503c8a5a92641faf Phobos = 2b71a0bf5c828b1916673a8c0bbcdb1d01babf2b Tools = ef71a52ddf5f59fb31a7dcd9d1fa4c025f1b919d Anyone know what's missing? rm -f generated/linux/64/dustmite generated/linux/64/rdmd generated/linux/64/ddemangle generated/linux/64/catdoc generated/linux/64/detab generated/linux/64/tolf generated/linux/64/dget generated/linux/64/changed generated/linux/64/findtags generated/linux/64/dman expression.tag statement.tag *.o generated/linux/64/*.o /usr/src/d/dmd/src/dmd -m64 -ofgenerated/linux/64/rdmd rdmd.d /usr/src/d/dmd/src/dmd -m64 -ofgenerated/linux/64/ddemangle ddemangle.d /usr/src/d/dmd/src/dmd -m64 -ofgenerated/linux/64/catdoc catdoc.d /usr/src/d/dmd/src/dmd -m64 -ofgenerated/linux/64/detab detab.d /usr/src/d/dmd/src/dmd -m64 -ofgenerated/linux/64/tolf tolf.d Error: cannot find source code for runtime library file 'object.d' dmd might not be correctly installed. Run 'dmd -man' for installation instructions. import path[0] = /usr/include/dmd/phobos import path[1] = /usr/include/dmd/druntime/import Error: cannot find source code for runtime library file 'object.d' dmd might not be correctly installed. Run 'dmd -man' for installation instructions. import path[0] = /usr/include/dmd/phobos import path[1] = /usr/include/dmd/druntime/import make: *** [generated/linux/64/rdmd] Error 1 make: *** Waiting for unfinished jobs make: *** [generated/linux/64/ddemangle] Error 1 Error: cannot find source code for runtime library file 'object.d' dmd might not be correctly installed. Run 'dmd -man' for installation instructions. import path[0] = /usr/include/dmd/phobos import path[1] = /usr/include/dmd/druntime/import make: *** [generated/linux/64/catdoc] Error 1 Error: cannot find source code for runtime library file 'object.d' dmd might not be correctly installed. Run 'dmd -man' for installation instructions. import path[0] = /usr/include/dmd/phobos import path[1] = /usr/include/dmd/druntime/import make: *** [generated/linux/64/detab] Error 1 Error: cannot find source code for runtime library file 'object.d' dmd might not be correctly installed. Run 'dmd -man' for installation instructions. import path[0] = /usr/include/dmd/phobos import path[1] = /usr/include/dmd/druntime/import make: *** [generated/linux/64/tolf] Error 1 Thanks for the help Joseph Script link: https://github.com/joichiro/zero/blob/master/build_and_install_dmd.sh