Re: How can I make a program which uses all cores and 100% of cpu power?
On Fri, 2019-10-11 at 00:41 +, Murilo via Digitalmars-d-learn wrote: > I have started working with neural networks and for that I need a > lot of computing power but the programs I make only use around > 30% of the cpu, or at least that is what Task Manager tells me. > How can I make it use all 4 cores of my AMD FX-4300 and how can I > make it use 100% of it? Why do you want to get CPU utilisation to 100%? I would have thought you'd want to get the neural net to be as fast as possible, this does not necessarily imply that all CPU cycles must be used. A neural net is, at it's heart, a set of communicating nodes. This is as much an I/O bound model as it is compute bound one – nodes are generally waiting for input as much as they are computing a value. The obvious solution architecture for a small computer is to create a task per node on a thread pool, with a few more threads in the pool than you have processors, and hope that you can organise the communication between tasks so as to avoid cache misses. This can be tricky when using multi-core processors. It gets even worse when you have hyperthreads – many organisations doing CPU bound computations switch off hyperthreads as they cause more problems than theysolve. -- Russel. === Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Roadm: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk signature.asc Description: This is a digitally signed message part
Re: Undefined symbol: _dyld_enumerate_tlv_storage (OSX)
On Thursday, 10 October 2019 at 18:12:51 UTC, Robert M. Münch wrote: I have two project I want to compile and both times get this error: Undefined symbols for architecture x86_64: "_dyld_enumerate_tlv_storage", referenced from: __d_dyld_getTLSRange in libphobos2.a(osx_tls.o) I'm wondering where this comes from as I didn't see it in the past. Any idea? I had the same missing symbol at runtime, when trying to run an already compiled binary (LDC 1.16 I think) after a Catalina update. In that case, recompiling (LDC 1.17 or DMD 2.088.0) was apparently enough to mitigate the issue.
Re: How can I make a program which uses all cores and 100% of cpu power?
On 10/10/2019 05:41 PM, Murilo wrote: > I have started working with neural networks and for that I need a lot of > computing power but the programs I make only use around 30% of the cpu, > or at least that is what Task Manager tells me. How can I make it use > all 4 cores of my AMD FX-4300 and how can I make it use 100% of it? Your threads must allocate as little memory as possible because memory allocation can trigger garbage collection and garbage collection stops all threads (except the one that's performing collection). We studied the effects of different allocation schemes during our last local D meetup[1]. The following program has two similar worker threads. One allocates in an inner scope, the other one uses a static Appender and clears its state as needed. The program sets 'w' to 'worker' inside main(). Change it to 'worker2' to see a huge difference: On my 4-core laptop its 100% versus 400% CPU usage. import std.random; import std.range; import std.algorithm; import std.concurrency; import std.parallelism; enum inner_N = 100; void worker() { ulong result; while (true) { int[] arr; foreach (j; 0 .. inner_N) { arr ~= uniform(0, 2); } result += arr.sum; } } void worker2() { ulong result; static Appender!(int[]) arr; while (true) { arr.clear(); foreach (j; 0 .. inner_N) { arr ~= uniform(0, 2); } result += arr.data.sum; } } void main() { // Replace with 'worker2' to see the speedup alias w = worker; auto workers = totalCPUs.iota.map!(_ => spawn(&w)).array; w(); } The static Appender is thread-safe because each thread gets their own copy due to data being thread-local by default in D. However, it doesn't mean that the functions are reentrant: If they get called recursively perhaps indirectly, then the subsequent executions would corrupt previous executions' Appender states. Ali [1] https://www.meetup.com/D-Lang-Silicon-Valley/events/kmqcvqyzmbzb/ Are you someone in the Bay Area but do not come to our meetups? We've been eating your falafel wraps! ;)
Re: How can I make a program which uses all cores and 100% of cpu power?
On Fri, Oct 11, 2019 at 6:58 AM Daniel Kozak wrote: > > so can stress your CPU. can't
Re: How can I make a program which uses all cores and 100% of cpu power?
On Fri, Oct 11, 2019 at 2:45 AM Murilo via Digitalmars-d-learn wrote: > > I have started working with neural networks and for that I need a > lot of computing power but the programs I make only use around > 30% of the cpu, or at least that is what Task Manager tells me. > How can I make it use all 4 cores of my AMD FX-4300 and how can I > make it use 100% of it? You should use minimally same amount of threads as you have cores. So in your case 4 or even more. Than you should buy a new CPU if you really need a lot of computing power :). Other issue can be using blocking IO, so your threads are in idle, so can stress your CPU.
Re: formatting a float or double in a string with all significant digits kept
On Thursday, 10 October 2019 at 22:44:05 UTC, H. S. Teoh wrote: On Thu, Oct 10, 2019 at 09:13:05PM +, Jon Degenhardt via Digitalmars-d-learn wrote: On Thursday, 10 October 2019 at 17:12:25 UTC, dan wrote: > Thanks also berni44 for the information about the dig > attribute, Jon > for the neat packaging into one line using the attribute on > the > type. > Unfortunately, the version of gdc that comes with the > version of > debian that i am using does not have the dig attribute yet, > but > perhaps i can upgrade, and eventually i think gdc will have > it. What's the output of dmd --version? I find it extremely odd that .dig isn't supported. AFAIK, it's been there since the early days of D. Certainly, it has been there since I started using D, which was before dmd was ever available in Debian. What's the compiler output of: pragma(msg, float.dig); ? T Thanks HS! I sure thought i got a compile time error when i used .dig, but i tried it again, and it works. I tried the pragma, and it printed out 6, and i tried PI.dig and double.dig and they're all working now. Just for reference, i'm using what i think is the standard gdc on debian 9, not dmd, and the version is 2068L. Thanks again for your help in encouraging me to try harder, and thanks again everybody else for all the help. dan
How can I make a program which uses all cores and 100% of cpu power?
I have started working with neural networks and for that I need a lot of computing power but the programs I make only use around 30% of the cpu, or at least that is what Task Manager tells me. How can I make it use all 4 cores of my AMD FX-4300 and how can I make it use 100% of it?
_getmaxstdio / _setmaxstdio
Missing _getmaxstdio / _setmaxstdio? I'd like to try and increase the limit of open files without resorting to Windows API, is it possible or will I have to resort to the WinAPI to achieve this? Thanks Damian
Re: formatting a float or double in a string with all significant digits kept
On Thu, Oct 10, 2019 at 09:13:05PM +, Jon Degenhardt via Digitalmars-d-learn wrote: > On Thursday, 10 October 2019 at 17:12:25 UTC, dan wrote: > > Thanks also berni44 for the information about the dig attribute, Jon > > for the neat packaging into one line using the attribute on the > > type. > > Unfortunately, the version of gdc that comes with the version of > > debian that i am using does not have the dig attribute yet, but > > perhaps i can upgrade, and eventually i think gdc will have it. What's the output of dmd --version? I find it extremely odd that .dig isn't supported. AFAIK, it's been there since the early days of D. Certainly, it has been there since I started using D, which was before dmd was ever available in Debian. What's the compiler output of: pragma(msg, float.dig); ? T -- Many open minds should be closed for repairs. -- K5 user
Re: formatting a float or double in a string with all significant digits kept
On Thursday, 10 October 2019 at 17:12:25 UTC, dan wrote: Thanks also berni44 for the information about the dig attribute, Jon for the neat packaging into one line using the attribute on the type. Unfortunately, the version of gdc that comes with the version of debian that i am using does not have the dig attribute yet, but perhaps i can upgrade, and eventually i think gdc will have it. Glad these ideas helped. The value of the 'double.dig' property is not going to change between compilers/versions/etc. It's really a property of IEEE 754 floating point for 64 bit floats. (D specified the size of double as 64). So, if you are using double, then it's pretty safe to use 15 until the compiler you're using is further along on versions. Declare an enum or const variable to give it a name so you can track it down later. Also, don't get thrown off by the PI is a real, not a double. D supports 80 bit floats as real, so constants like PI are defined as real. But if you convert PI to a double, it'll then have 15 significant bits of precision. --Jon
Re: D man pages
On Thursday, 10 October 2019 at 19:26:36 UTC, Daniel Kozak wrote: On Thursday, 10 October 2019 at 19:25:22 UTC, Daniel Kozak wrote: On Thursday, 10 October 2019 at 19:21:06 UTC, Daniel Kozak wrote: On Thursday, 10 October 2019 at 19:19:42 UTC, Daniel Kozak wrote: On Thursday, 10 October 2019 at 18:52:32 UTC, Jarek wrote: On Monday, 23 September 2019 at 12:31:16 UTC, Adam D. Ruppe wrote: [...] Hello, thanks for reply. This is my first dlang work: import std.stdio; import std.conv; import core.sys.posix.dirent; [...] You should use fromStringZ: https://dlang.org/phobos/std_string.html#.fromStringz stdout.writeln("Subdir: ", ent.d_name.fromStringz); One more time :) stdout.writeln("Subdir: ", ent.d_name.ptr.fromStringz); and to!string works too: ent.d_name.ptr.to!string and there is a more D idiomatic version: import std.stdio; import std.algorithm; import std.file; import std.path; int main() { try { auto dFiles = dirEntries("/proc", SpanMode.shallow) .filter!(f => f.isDir) .each!((a){writefln("Subdir: %s", a.baseName);}); } catch (FileException fe) { stderr.writeln(fe.msg); return 1; } return 0; }
Re: D man pages
On Thursday, 10 October 2019 at 19:21:06 UTC, Daniel Kozak wrote: On Thursday, 10 October 2019 at 19:19:42 UTC, Daniel Kozak wrote: On Thursday, 10 October 2019 at 18:52:32 UTC, Jarek wrote: On Monday, 23 September 2019 at 12:31:16 UTC, Adam D. Ruppe wrote: [...] Hello, thanks for reply. This is my first dlang work: import std.stdio; import std.conv; import core.sys.posix.dirent; [...] You should use fromStringZ: https://dlang.org/phobos/std_string.html#.fromStringz stdout.writeln("Subdir: ", ent.d_name.fromStringz); One more time :) stdout.writeln("Subdir: ", ent.d_name.ptr.fromStringz);
Re: D man pages
On Thursday, 10 October 2019 at 19:25:22 UTC, Daniel Kozak wrote: On Thursday, 10 October 2019 at 19:21:06 UTC, Daniel Kozak wrote: On Thursday, 10 October 2019 at 19:19:42 UTC, Daniel Kozak wrote: On Thursday, 10 October 2019 at 18:52:32 UTC, Jarek wrote: On Monday, 23 September 2019 at 12:31:16 UTC, Adam D. Ruppe wrote: [...] Hello, thanks for reply. This is my first dlang work: import std.stdio; import std.conv; import core.sys.posix.dirent; [...] You should use fromStringZ: https://dlang.org/phobos/std_string.html#.fromStringz stdout.writeln("Subdir: ", ent.d_name.fromStringz); One more time :) stdout.writeln("Subdir: ", ent.d_name.ptr.fromStringz); and to!string works too: ent.d_name.ptr.to!string
Re: D man pages
On Thursday, 10 October 2019 at 19:19:42 UTC, Daniel Kozak wrote: On Thursday, 10 October 2019 at 18:52:32 UTC, Jarek wrote: On Monday, 23 September 2019 at 12:31:16 UTC, Adam D. Ruppe wrote: [...] Hello, thanks for reply. This is my first dlang work: import std.stdio; import std.conv; import core.sys.posix.dirent; [...] You should use fromStringZ: https://dlang.org/phobos/std_string.html#.fromStringz stdout.writeln("Subdir: ", ent.d_name.fromStringz);
Re: D man pages
On Thursday, 10 October 2019 at 18:52:32 UTC, Jarek wrote: On Monday, 23 September 2019 at 12:31:16 UTC, Adam D. Ruppe wrote: [...] Hello, thanks for reply. This is my first dlang work: import std.stdio; import std.conv; import core.sys.posix.dirent; [...] You should use fromStringZ: https://dlang.org/phobos/std_string.html#.fromStringz
Re: D man pages
On Monday, 23 September 2019 at 12:31:16 UTC, Adam D. Ruppe wrote: On Monday, 23 September 2019 at 06:06:05 UTC, Jarek wrote: I have the same question. Where to find something similar to man pages from C? They are the same functions, so the idea is you can just use the C man pages directly. There's just the pattern of the D module name to know. but... does it mean that I can't use seekdir() on systems with Musl? (Alpine linux)? it is possibly just not copied in there, I'd say to just try it and see if it triggers the static assert down there. the man page says CONFORMING TO 4.3BSD, POSIX.1-2001. so it probably should work with the core.sys.posix header there, maybe it just isn't verified as to the type of the argument (the notes section warns it has changed, so the D devs are surely being extra cautious about which one it actually has in there, waiting for someone to verify it before putting in the file) Hello, thanks for reply. This is my first dlang work: import std.stdio; import std.conv; import core.sys.posix.dirent; int main(string[] args) { DIR* proc; dirent *ent; proc = opendir("/proc"); if(proc == null){ stderr.writeln("Open /proc error"); return 1; } stdout.writeln("proc opened"); while((ent = readdir(proc)) != null){ if(ent.d_type != DT_DIR){ continue; } stdout.writeln("Subdir: ", to!string(ent.d_name)); } if(closedir(proc) == -1){ stderr.writeln("Close dir error"); return 1; } return 0; } How to handle ent.d_name? When I writeln it with to!string() conversion then it doesn't print well. As a result I got something like this: (...) Subdir: 8� 10�11�12�13�14�15�17�18�19� 10�11�12�13�14�15�17�18�19�22� Subdir: 10�11�12�13�14�15�17�18�19�22�23� ubdir: 11�12�13�14�15�17�18�19�22�23�24�K Subdir: 12�13�14�15�17�18�19�22�23�24�K25�L Subdir: 13�14�15�17�18�19�22�23�24�K25�L73�M Subdir: 14�15�17�18�19�22�23�24�K25�L73�M74�N Subdir: 15�17�18�19�22�23�24�K25�L73�M74�N75�O Subdir: 17�18�19�22�23�24�K25�L73�M74�N75�O76�Q Subdir: 18�19�22�23�24�K25�L73�M74�N75�O76�Q77�R Subdir: 19�22�23�24�K25�L73�M74�N75�O76�Q77�R79�S Subdir: 22�23�24�K25�L73�M74�N75�O76�Q77�R79�S80�T Subdir: 23�24�K25�L73�M74�N75�O76�Q77�R79�S80�T81�U (...) to!string() doesn't work? thanks for help
Re: Undefined symbol: _dyld_enumerate_tlv_storage (OSX)
What dmd version? https://issues.dlang.org/show_bug.cgi?id=20019 On Thu, Oct 10, 2019 at 8:15 PM Robert M. Münch via Digitalmars-d-learn wrote: > > I have two project I want to compile and both times get this error: > > Undefined symbols for architecture x86_64: > "_dyld_enumerate_tlv_storage", referenced from: > __d_dyld_getTLSRange in libphobos2.a(osx_tls.o) > > I'm wondering where this comes from as I didn't see it in the past. Any idea? > > -- > Robert M. Münch > http://www.saphirion.com > smarter | better | faster >
Undefined symbol: _dyld_enumerate_tlv_storage (OSX)
I have two project I want to compile and both times get this error: Undefined symbols for architecture x86_64: "_dyld_enumerate_tlv_storage", referenced from: __d_dyld_getTLSRange in libphobos2.a(osx_tls.o) I'm wondering where this comes from as I didn't see it in the past. Any idea? -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Re: How to do IPC using Unix Domain Socket?
On Thursday, 10 October 2019 at 12:30:25 UTC, Hossain Adnan wrote: Here I have a program that wants to 1. detect whether if it's the only instance 1.1. it does that by trying to create a Unix Domain Socket and trying to binding it to a specific address. 2. if a duplicate program is not running, establish an UDS and then listen to the socket. 2.1. if any message comes through that socket, the program will log the incoming message 2.2. otherwise it should keep listening to the socket forever 3. if there's a duplicate program it should send a message and then exit. Here's what I have: import std.socket, std.experimental.logger; immutable string socketAddress = "\0/tmp/com.localserver.myapp"; void main() { auto socket = new std.socket.Socket(std.socket.AddressFamily.UNIX, std.socket.SocketType.STREAM); auto addr = new std.socket.UnixAddress(socketAddress); auto isUnique = () { bool result; scope (success) log("returns: ", result); try { socket.bind(addr); result = true; } catch (std.socket.SocketOSException e) result = false; // else throw error return result; }(); if (isUnique) { log("Unique instance detected. Listening..."); // works upto now char[] buffer = []; while (1) { socket.listen(0); socket.receive(buffer); if (buffer != []) { log("Received message: ", buffer); } buffer = []; } } else { log("Duplicate instance detected."); socket.connect(addr); import std.stdio; stdout.write("Enter your message:\t"); socket.send(readln()); log("Message has been sent. Exiting."); } } The documentation does not seem very friendly to those who does not have any experience in socket programming. Consulted stackoverflow and came up with this: import std.socket, std.experimental.logger; class UDSIPC { private: static immutable string socketAddress = "\0/tmp/com.localserver.myapp"; static immutable size_t messageBufferSize = 64; static immutable string socketAddressName = "\0/tmp/com.localserver.myapp"; Socket socket; UnixAddress uaddr; public: this(in string socketAddressName = socketAddressName) { socket = new Socket(AddressFamily.UNIX, SocketType.STREAM); uaddr = new UnixAddress(socketAddress); } bool getUniqueness() { bool result; scope (success) log("returns: ", result); try { socket.bind(uaddr); result = true; } catch (SocketOSException e) result = false; // else throw error return result; } string getMessage() { socket.listen(0); auto receiverSocket = socket.accept(); char[messageBufferSize] buffer; auto amount = receiverSocket.receive(buffer); import std.string; return format!"%s"(buffer[0 .. amount]); } void sendMessage(in string message) { socket.connect(uaddr); socket.send(message); } } void main() { auto ipc = new UDSIPC(); if (ipc.getUniqueness()) { while (true) { log(ipc.getMessage()); } } else { import std.stdio, std.string; ipc.sendMessage(readln().chomp()); } }
Re: formatting a float or double in a string with all significant digits kept
On Wednesday, 9 October 2019 at 10:54:49 UTC, David Briant wrote: On Tuesday, 8 October 2019 at 20:37:03 UTC, dan wrote: I have a double precision number that i would like to print all significant digits of, but no more than what are actually present in the number. Or more exactly, i want to print the minimum number of digits necessary to recover the original number to within 2 or 3 least significant bits in the stored, in-core, version of its bit pattern. For example, import std.string; import std.stdio; import std.math; void main( ) { auto t = format("%3.30f", PI ); writeln("Value of PI is: ", PI, " or, : ", t); } The default way writeln prints is 5 digits to the right of the decimal point. I can format to print with any number of digits, such as 30 above, but that's too many. For pi, the correct number of digits to print looks to be about 18 (and the extra 12 digits presumably are from the decimal expansion of the least significant bit?). But i would like to be able to do this without knowing the expansion of pi, or writing too much code, especially if there's some d function like writeAllDigits or something similar. Thanks in advance for any pointers! dan Hi Dan, What's your usecase here, e.g. a csv/json reader / writer? You say it's for double precision numbers (64bit format) then provide an example for reals (80bit format). So I'm not certain your goal. If you google "what every developer should know about doubles" you'll hit a number of useful articles that explain the common issues of floating point representation in detail. -- David Thanks David for your reply. Thanks also berni44 for the information about the dig attribute, Jon for the neat packaging into one line using the attribute on the type. Unfortunately, the version of gdc that comes with the version of debian that i am using does not have the dig attribute yet, but perhaps i can upgrade, and eventually i think gdc will have it. And thanks GreatSam4sure for your reply --- i searched the archives first, but very poorly :(. But it's easy to believe that i'm not the first person in the history of the world with this issue. Now, my use case is nothing so useful or general as a csv/json reader/writer. I'm just doing some computations, incorrectly i think, and i want to be able to print out the results and feed them to other software. I'm trying to chase down a problem and rule out as many places for error as i can, and it just seemed strange not to be able to get all the digits out in some easy way. But the dig attribute seems to be a big step forward, and for that i am grateful. dan
Re: C#'s 'is' equivalent in D
On Thursday, 10 October 2019 at 16:33:47 UTC, H. S. Teoh wrote: On Thu, Oct 10, 2019 at 03:58:02PM +, jmh530 via Digitalmars-d-learn wrote: On Thursday, 10 October 2019 at 15:47:58 UTC, Just Dave wrote: > In C# you can do something like: > > > if (obj is Person) > { > var person = obj as Person; > // do stuff with person... > } [...] You mean something like below: class Person { int id; this(int x) { id = x; } } void main() { auto joe = new Person(1); if (is(typeof(joe) == Person)) { assert(joe.id == 1); } } Unfortunately, typeof is a compile-time construct, so this will not work if you're receiving a Person object via a base class reference. The correct solution is to cast the base class to the derived type, which will yield null if it's not an instance of the derived type. T Ah, you mean something like below: class Person { int id; this(int x) { id = x; } } class Employee : Person { int job_id; this(int x, int y) { super(x); job_id = y; } } void main() { import std.stdio : writeln; Person joe = new Employee(1, 2); if (is(typeof(joe) == Employee)) { writeln("here"); //not called in this case } }
Re: C#'s 'is' equivalent in D
On Thu, Oct 10, 2019 at 03:58:02PM +, jmh530 via Digitalmars-d-learn wrote: > On Thursday, 10 October 2019 at 15:47:58 UTC, Just Dave wrote: > > In C# you can do something like: > > > > > > if (obj is Person) > > { > > var person = obj as Person; > > // do stuff with person... > > } [...] > You mean something like below: > > class Person { > int id; > this(int x) { > id = x; > } > } > > void main() { > auto joe = new Person(1); > if (is(typeof(joe) == Person)) { > assert(joe.id == 1); > } > } Unfortunately, typeof is a compile-time construct, so this will not work if you're receiving a Person object via a base class reference. The correct solution is to cast the base class to the derived type, which will yield null if it's not an instance of the derived type. T -- LINUX = Lousy Interface for Nefarious Unix Xenophobes.
Re: Functional Programming in D
On Thursday, 10 October 2019 at 08:59:49 UTC, Russel Winder wrote: I feel that it is best to leave functional programming to functional programming language, e.g. Haskell, Scheme, etc. rather than try to do functional programming in imperative languages, e.g. Java, C++, Rust, D. The reason is things like lazy evaluation and the consistency of everything being a function, etc. The underlying computational models of functional programming languages and imperative programming languages need different mindsets to use well. Witness the issues in using Scala. My impressions is that the complaints about Scala are similar to C++: too many features that clash with one another and make the language complicated, plus extremely slow compilation times. I haven't seen a lot of complaints about mixing imperative and functional.
Re: Functional Programming in D
On Thursday, 10 October 2019 at 10:08:14 UTC, H. S. Teoh wrote: On Thu, Oct 10, 2019 at 09:59:49AM +0100, Russel Winder via Digitalmars-d-learn wrote: On Wed, 2019-10-09 at 11:12 -0700, H. S. Teoh via Digitalmars-d-learn wrote: […] > Actually, std.functional is somewhat of a misnomer. It > mostly deals with higher-order functions, i.e., functions > that return functions, currying, that sort of thing. These > are part of functional programming, but there's more to > functional programming than that. I'd say std.range and > std.algorithm are another major part of functional-style > programming support in D, along with the purity system. […] I feel that it is best to leave functional programming to functional programming language, e.g. Haskell, Scheme, etc. rather than try to do functional programming in imperative languages, e.g. Java, C++, Rust, D. [...] Note this is why I wrote "functional-style programming" w.r.t. D, rather than "functional programming". Clearly, what D has isn't "real" functional programming in the strict sense, but it does share similar characteristics when written in that style. T An even better phrase is simply "good programming practice". Avoiding global mutable state, writing pure functions, passing around functions as arguments, and letting the language handle iteration rather than using for loops and related outdated constructs is good practice. You might need to make exceptions for performance reasons, but then you're in the realm of hacks rather than practices.
Re: C#'s 'is' equivalent in D
On Thursday, 10 October 2019 at 15:47:58 UTC, Just Dave wrote: In C# you can do something like: if (obj is Person) { var person = obj as Person; // do stuff with person... } where you can check the type of an object prior to casting. Does D have a similar mechanism? It's so widely useful in the C# realm that they even added syntactic sugar to allow: if (obj is Person person) { // do stuff with person... } I would presume since D has reference objects there must exist some mechanism for this... You mean something like below: class Person { int id; this(int x) { id = x; } } void main() { auto joe = new Person(1); if (is(typeof(joe) == Person)) { assert(joe.id == 1); } }
Re: Difference between template and mixin template
On Thursday, 10 October 2019 at 15:56:36 UTC, Just Dave wrote: I'm trying to get my head around mixing templates. I'm using it as kind of a replacement for class inheritance as it seems to fit better composition over inheritance. So I do something like: mixin template NumberTemplate() { private: int number = 0; public: int getNumber(int number) { return number; } } interface INumber { getNumber(int number); } class Number : INumber { template NumberTemplate; }; So two questions: a) Is this correct usage? b) It compiles if I just do: template NumberTemplate() { private: int number = 0; public: int getNumber(int number) { return number; } } what is the difference between template and mixin template? Sorry I messed up the above code example the following should look like: class Number : INumber { mixin NumberTemplate; };
Re: C#'s 'is' equivalent in D
On Thursday, 10 October 2019 at 15:53:20 UTC, Adam D. Ruppe wrote: On Thursday, 10 October 2019 at 15:47:58 UTC, Just Dave wrote: if (obj is Person person) Looks the same as D's if(auto person = cast(Person) obj) { // use person in here } else { // it was some other type } Excellent!
Difference between template and mixin template
I'm trying to get my head around mixing templates. I'm using it as kind of a replacement for class inheritance as it seems to fit better composition over inheritance. So I do something like: mixin template NumberTemplate() { private: int number = 0; public: int getNumber(int number) { return number; } } interface INumber { getNumber(int number); } class Number : INumber { template NumberTemplate; }; So two questions: a) Is this correct usage? b) It compiles if I just do: template NumberTemplate() { private: int number = 0; public: int getNumber(int number) { return number; } } what is the difference between template and mixin template?
Re: C#'s 'is' equivalent in D
On Thursday, October 10, 2019 9:47:58 AM MDT Just Dave via Digitalmars-d- learn wrote: > In C# you can do something like: > > > if (obj is Person) > { > var person = obj as Person; > // do stuff with person... > } > > where you can check the type of an object prior to casting. Does > D have a similar mechanism? It's so widely useful in the C# realm > that they even added syntactic sugar to allow: > > if (obj is Person person) > { > // do stuff with person... > } > > I would presume since D has reference objects there must exist > some mechanism for this... D's solution is basically the same as C++'s solution. You cast and then check whether the result is null. So, if(cast(Person)obj !is null) { } or since using a pointer or reference in an if condition checks whether it's null or not if(cast(Person)obj) { } and you can even declare a variable that way if you want. e.g. if(auto person = cast(Person)obj) { } When D's is is used to compare two objects, it checks whether they're equal bitwise. So, it's typically used for comparing pointers or references for equality (whereas using == with references would compare the objects themselves for equality). - Jonathan M Davis
Re: C#'s 'is' equivalent in D
On 10/10/19 6:47 PM, Just Dave wrote: In C# you can do something like: if (obj is Person) { var person = obj as Person; // do stuff with person... } where you can check the type of an object prior to casting. Does D have a similar mechanism? It's so widely useful in the C# realm that they even added syntactic sugar to allow: if (obj is Person person) { // do stuff with person... } I would presume since D has reference objects there must exist some mechanism for this... ```D if (auto person = cast(Person) obj) { // do stuff with person... } ```
Re: C#'s 'is' equivalent in D
On Thursday, 10 October 2019 at 15:47:58 UTC, Just Dave wrote: if (obj is Person person) Looks the same as D's if(auto person = cast(Person) obj) { // use person in here } else { // it was some other type }
Re: C#'s 'is' equivalent in D
Even though static solutions would be more performance minded, I'd actually prefer to see the runtime equivalent so I don't have to rethink how I think as performance isn't really my major concern right now.
C#'s 'is' equivalent in D
In C# you can do something like: if (obj is Person) { var person = obj as Person; // do stuff with person... } where you can check the type of an object prior to casting. Does D have a similar mechanism? It's so widely useful in the C# realm that they even added syntactic sugar to allow: if (obj is Person person) { // do stuff with person... } I would presume since D has reference objects there must exist some mechanism for this...
Re: Functional Programming in D
On Thursday, 10 October 2019 at 10:08:14 UTC, H. S. Teoh wrote: On Thu, Oct 10, 2019 at 09:59:49AM +0100, Russel Winder via Digitalmars-d-learn wrote: On Wed, 2019-10-09 at 11:12 -0700, H. S. Teoh via Digitalmars-d-learn wrote: […] > Actually, std.functional is somewhat of a misnomer. It > mostly deals with higher-order functions, i.e., functions > that return functions, currying, that sort of thing. These > are part of functional programming, but there's more to > functional programming than that. I'd say std.range and > std.algorithm are another major part of functional-style > programming support in D, along with the purity system. […] I feel that it is best to leave functional programming to functional programming language, e.g. Haskell, Scheme, etc. rather than try to do functional programming in imperative languages, e.g. Java, C++, Rust, D. [...] Note this is why I wrote "functional-style programming" w.r.t. D, rather than "functional programming". Clearly, what D has isn't "real" functional programming in the strict sense, but it does share similar characteristics when written in that style. I did programming in Smalltalk for about 10 years. Smalltalk has all those things that are now called "functional-style programming" or even IMHO incorrectly "functional programming" that are about applying operations on collections. In Smalltalk these functions (like collect, map, reject, detect, inject, etc.) are simply called "collection iterators". They already exist in Smalltalk-80, which is called that way, because it was published in 1980. To verify this you can download the Smalltalk-80 "blue book" from here and do a text search on these function names: http://stephane.ducasse.free.fr/FreeBooks/BlueBook/Bluebook.pdf In those 10 years where I did Smalltalk development I met no one who called making use of those collection iterators functional programming. There are several books about Smalltalk who have some importance in CS, because Smalltalk was the first truly useable OO language afer Simula. I don't think the term functional programming is ever used there. Calling collection iterators functional programming opened Pandoras box of mixing up things. I fear it is too late to explain to people that functional progeramming is what is done in Haskel and Lisp and related languages and nothing else. Collection iterators carry some aspects of functional programming such as non-destructive programming as the result of applying a function on a call returns a new collection leaving the original collection unchanged, but that alone is not enogh for things to be called functional programming. It is merely simply making use of closures.
Re: How to do IPC using Unix Domain Socket?
On Thursday, 10 October 2019 at 12:30:25 UTC, Hossain Adnan wrote: Here I have a program that wants to 1. detect whether if it's the only instance 1.1. it does that by trying to create a Unix Domain Socket and trying to binding it to a specific address. [...] If it helps explaining better, similar rust code is here: https://gitlab.com/snippets/1893594
How to do IPC using Unix Domain Socket?
Here I have a program that wants to 1. detect whether if it's the only instance 1.1. it does that by trying to create a Unix Domain Socket and trying to binding it to a specific address. 2. if a duplicate program is not running, establish an UDS and then listen to the socket. 2.1. if any message comes through that socket, the program will log the incoming message 2.2. otherwise it should keep listening to the socket forever 3. if there's a duplicate program it should send a message and then exit. Here's what I have: import std.socket, std.experimental.logger; immutable string socketAddress = "\0/tmp/com.localserver.myapp"; void main() { auto socket = new std.socket.Socket(std.socket.AddressFamily.UNIX, std.socket.SocketType.STREAM); auto addr = new std.socket.UnixAddress(socketAddress); auto isUnique = () { bool result; scope (success) log("returns: ", result); try { socket.bind(addr); result = true; } catch (std.socket.SocketOSException e) result = false; // else throw error return result; }(); if (isUnique) { log("Unique instance detected. Listening..."); // works upto now char[] buffer = []; while (1) { socket.listen(0); socket.receive(buffer); if (buffer != []) { log("Received message: ", buffer); } buffer = []; } } else { log("Duplicate instance detected."); socket.connect(addr); import std.stdio; stdout.write("Enter your message:\t"); socket.send(readln()); log("Message has been sent. Exiting."); } } The documentation does not seem very friendly to those who does not have any experience in socket programming.
Re: Functional Programming in D
On Thu, 2019-10-10 at 03:08 -0700, H. S. Teoh via Digitalmars-d-learn wrote: […] > Note this is why I wrote "functional-style programming" w.r.t. D, rather > than "functional programming". Clearly, what D has isn't "real" > functional programming in the strict sense, but it does share similar > characteristics when written in that style. Indeed, I was trying to support that view. I think declarative rather than functional-style is a better label for this since it avoids the functional vs imperative paradigm conflict left over from the late 1980s and 1990s. -- Russel. === Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Roadm: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk signature.asc Description: This is a digitally signed message part
Re: Functional Programming in D
On Thu, Oct 10, 2019 at 09:59:49AM +0100, Russel Winder via Digitalmars-d-learn wrote: > On Wed, 2019-10-09 at 11:12 -0700, H. S. Teoh via Digitalmars-d-learn wrote: > […] > > Actually, std.functional is somewhat of a misnomer. It mostly deals > > with higher-order functions, i.e., functions that return functions, > > currying, that sort of thing. These are part of functional > > programming, but there's more to functional programming than that. > > I'd say std.range and std.algorithm are another major part of > > functional-style programming support in D, along with the purity > > system. > […] > > I feel that it is best to leave functional programming to functional > programming language, e.g. Haskell, Scheme, etc. rather than try to do > functional programming in imperative languages, e.g. Java, C++, Rust, > D. [...] Note this is why I wrote "functional-style programming" w.r.t. D, rather than "functional programming". Clearly, what D has isn't "real" functional programming in the strict sense, but it does share similar characteristics when written in that style. T -- It is of the new things that men tire --- of fashions and proposals and improvements and change. It is the old things that startle and intoxicate. It is the old things that are young. -- G.K. Chesterton
Re: Functional Programming in D
On Wed, 2019-10-09 at 11:12 -0700, H. S. Teoh via Digitalmars-d-learn wrote: […] > Actually, std.functional is somewhat of a misnomer. It mostly deals with > higher-order functions, i.e., functions that return functions, currying, > that sort of thing. These are part of functional programming, but > there's more to functional programming than that. I'd say std.range and > std.algorithm are another major part of functional-style programming > support in D, along with the purity system. […] I feel that it is best to leave functional programming to functional programming language, e.g. Haskell, Scheme, etc. rather than try to do functional programming in imperative languages, e.g. Java, C++, Rust, D. The reason is things like lazy evaluation and the consistency of everything being a function, etc. The underlying computational models of functional programming languages and imperative programming languages need different mindsets to use well. Witness the issues in using Scala. Having said this, declarative programming can be pursued in functional languages, logic languages (e.g. Prolog), and imperative languages. Tools such as higher order functions are crucial to this, but currying is not, unless it is core to partial evaluation as it is in Haskell. Modern C++, D, Rust – but not Go – all admit programming using a declarative way of working just as Haskell and Scheme do. My experience of emphasising declarative programming in imperative languages is that people build smaller, more comprehensible, and maintainable code that achives the goal compared with using tradition imperative programming. -- Russel. === Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Roadm: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk signature.asc Description: This is a digitally signed message part
Re: Functional Programming in D
On Wednesday, 9 October 2019 at 18:57:01 UTC, SrMordred wrote: https://garden.dlang.io/ This should be more prominent. Very nice.