Re: More uses of operator in
Without that, algorithms can't make guarantees about their complexity, and it could have very negative impact on the performance of some programs. Yeah. For the programs where efficiency matters but that are never ever profiled.
Template constraint: T is a value type
Hi there, I'm looking to write a function template that operates only on value types. Basically I'm looking for an equivalent of where T: struct from C# (http://msdn.microsoft.com/en-us/library/d5x73970.aspx) I was thinking that maybe using template Foo(T) if (is(T : struct) or is(T: union)) { ... } or perhaps template Foo(T) if (!is(T : class)) { ... } would be correct, but I fear the first may be too limiting (would simple data types such as int, char etc be allowed here? I'd guess not.) and the second may be too lax. Any advice? Am I even going in remotely the right direction? Thanks.
Re: More uses of operator in
On Tuesday, 28 October 2014 at 20:24:03 UTC, Steven Schveighoffer wrote: It's also O(lgn) on a sorted set, or O(1) on a hash set. So? As araq points out this should be caught in profiling. Avoiding generality is not the best approach. A linear scan of a SIMD friendly array that is in cache may be a lot faster than your hash set.
Re: Template constraint: T is a value type
On 29/10/2014 11:01 p.m., Gareth Foster wrote: Hi there, I'm looking to write a function template that operates only on value types. Basically I'm looking for an equivalent of where T: struct from C# (http://msdn.microsoft.com/en-us/library/d5x73970.aspx) I was thinking that maybe using template Foo(T) if (is(T : struct) or is(T: union)) { ... } or perhaps template Foo(T) if (!is(T : class)) { ... } would be correct, but I fear the first may be too limiting (would simple data types such as int, char etc be allowed here? I'd guess not.) and the second may be too lax. Any advice? Am I even going in remotely the right direction? Thanks. Code: struct Foo {} union Bar {} class Fuz {} pragma(msg, __traits(isPOD, Foo)); pragma(msg, __traits(isPOD, Bar)); pragma(msg, __traits(isPOD, Fuz)); pragma(msg, __traits(isPOD, int)); pragma(msg, is(Foo == struct)); pragma(msg, is(Bar == struct)); pragma(msg, is(Fuz == struct)); pragma(msg, is(int == struct)); pragma(msg, is(Foo == union)); pragma(msg, is(Bar == union)); pragma(msg, is(Fuz == union)); pragma(msg, is(int == union)); Output: true true true true false false false false true false false Dpaste link: http://dpaste.dzfl.pl/921fa5156290 So to answer your question, first was pretty much correct.
Re: Reflections on isPalindrome
On Tuesday, 28 October 2014 at 16:07:38 UTC, MachineCode wrote: I'm very surprise. If they either equal or fast sometimes the compiler did great optizations or it's just a multicore processor that's helping or what else? the first version (from your post, the one using ranges) change in each iteration two pointers (in pop*() calls) and request r's length 3 times (in .empty calls) while the second doesn't, just run until an already know index (when enter in the loop) and access two index in each iteration. This without consider the amount of ifs. I don't know, maybe I just thinking in the C-way as that code would run. Yes, I'm curious about this too. I will check the assembly output later (When I have free time) to understand what is happening and why popFront/Back are faster than a loop. Matheus.
Re: dub fetch target redirection...
On 10/29/2014 1:48 PM, WhatMeWorry wrote: It looks like dub fetch... is putting all packages at %appdata% path on my windows machine. Is there a way to redirect packages to a user specified path? You're not really supposed to worry about that. It's how dub manages the packages internally. Why do you want to change it?
Re: Template constraint: T is a value type
On Wednesday, 29 October 2014 at 10:01:18 UTC, Gareth Foster wrote: Hi there, I'm looking to write a function template that operates only on value types. Basically I'm looking for an equivalent of where T: struct from C# (http://msdn.microsoft.com/en-us/library/d5x73970.aspx) I was thinking that maybe using template Foo(T) if (is(T : struct) or is(T: union)) { ... } or perhaps template Foo(T) if (!is(T : class)) { ... } would be correct, but I fear the first may be too limiting (would simple data types such as int, char etc be allowed here? I'd guess not.) and the second may be too lax. Any advice? Am I even going in remotely the right direction? Thanks. std.traits.hasIndirections any use for you?
Re: dub fetch target redirection...
https://github.com/D-Programming-Language/dub/issues/229
Equivalent in D for .p2align 4,,15 ?
I'm looking for the D inline assembler equivalent of the .p2align 4,,15 directive to optimize a loop. Here's more information: http://stackoverflow.com/questions/21546946/what-p2align-does-in-asm-code I tried searching through a turbo assembler tutorial (because D's is based on it) and found nothing except a few hints here: http://www.csn.ul.ie/~darkstar/assembler/manual/a10.txt There might be a way through segments, directives, but I'm not sure at all if D supports it. Does anyone have any idea if/how I can align my code this way or if the compiler handles it?
Re: Equivalent in D for .p2align 4,,15 ?
On Wednesday, 29 October 2014 at 17:16:23 UTC, Etienne wrote: I'm looking for the D inline assembler equivalent of the .p2align 4,,15 directive to optimize a loop. Here's more information: http://stackoverflow.com/questions/21546946/what-p2align-does-in-asm-code I tried searching through a turbo assembler tutorial (because D's is based on it) and found nothing except a few hints here: http://www.csn.ul.ie/~darkstar/assembler/manual/a10.txt There might be a way through segments, directives, but I'm not sure at all if D supports it. Does anyone have any idea if/how I can align my code this way or if the compiler handles it? As far as I understand, `.p2align 4,,15` aligns to a 16 byte boundary, and the third argument could be left out, because more than 15 bytes wouldn't ever be skipped anyway. D inline assembler has an 'align' directive [1]. Aligning to a 16 byte boundary in D: `align 16;`. [1] http://dlang.org/iasm.html -- align IntegerExpression, near the top
Re: Equivalent in D for .p2align 4,,15 ?
On 2014-10-29 1:44 PM, anonymous wrote: D inline assembler has an 'align' directive [1]. Aligning to a 16 byte boundary in D: `align 16;`. [1] http://dlang.org/iasm.html -- align IntegerExpression, near the top Of course, align directive works on instructions in asm. Thanks anonymous, that was a very simple explanation.
Re: dub fetch target redirection...
On Wednesday, 29 October 2014 at 12:47:48 UTC, Kagamin wrote: https://github.com/D-Programming-Language/dub/issues/229 Thanks. This s-ludwig guy is great.
Re: dub fetch target redirection...
On Wednesday, 29 October 2014 at 11:35:17 UTC, Mike Parker wrote: On 10/29/2014 1:48 PM, WhatMeWorry wrote: It looks like dub fetch... is putting all packages at %appdata% path on my windows machine. Is there a way to redirect packages to a user specified path? You're not really supposed to worry about that. It's how dub manages the packages internally. Why do you want to change it? I'm trying to put everything D related on a flash drive. So I can get my D fix wherever I go.
Need help: Return reference slice
Hello, I would like to create a D function that returns a slice of a string. This slice shall be a reference to a part of the string argument. Is this generally possible in D? This is the function: auto ref betweenTwoStrings(T)(inout T src, string start, string end) { long a = src.countUntil(start); if (a 0) return src; // null a += start.length; long b = src[a..$].countUntil(end); if (b 0) return src; // null b += a; return src[a..b]; } I would like to have something like that: char[] buf; // already filled array char[] partOfBuf = betweenTwoStrings(buf, START, END); partOfBuf[0] = 'a'; // THIS should also change the 'buf' variable assert(buf[0] == 'a'); Thanks for your help
Re: Need help: Return reference slice
advibm: I would like to have something like that: char[] buf; // already filled array char[] partOfBuf = betweenTwoStrings(buf, START, END); partOfBuf[0] = 'a'; // THIS should also change the 'buf' variable assert(buf[0] == 'a'); Thanks for your help To do this you don't need to return a ref slice. A slice suffices. But D strings/wstrings/dstrings are made of immutable chars, so you can't modify them. So you need to work with char[] or dchar[] or wchar[]. Bye, bearophile
Re: Need help: Return reference slice
On Wednesday, 29 October 2014 at 19:54:45 UTC, bearophile wrote: advibm: I would like to have something like that: char[] buf; // already filled array char[] partOfBuf = betweenTwoStrings(buf, START, END); partOfBuf[0] = 'a'; // THIS should also change the 'buf' variable assert(buf[0] == 'a'); Thanks for your help To do this you don't need to return a ref slice. A slice suffices. But D strings/wstrings/dstrings are made of immutable chars, so you can't modify them. So you need to work with char[] or dchar[] or wchar[]. Bye, bearophile Thank you for your fast answer. I am embarrased but my code already works as expected. I made a mistake when I wanted to change the partOfBuf variable. I wrote: partOfBuf = replacedArray; But I had to write partOfBuf[0..$] = replacedArray.
readln with buffer fails
I have this simple code: int main() { import std.stdio; char[4096] Input; readln(Input); //readln!(char)(Input); // also fails return 0; } I get these messages during compilation: test.d(39): Error: template std.stdio.readln cannot deduce function from argument types !()(char[4096]), candidates are: src\phobos\std\stdio.d(2818): std.stdio.readln(S = string)(dchar terminator = '\x0a') if (isSomeString!S) src\phobos\std\stdio.d(2851): std.stdio.readln(C)(ref C[] buf, dchar terminator = '\x0a') if (isSomeChar!C is(Unqual!C == C) !is(C == enum)) src\phobos\std\stdio.d(2858): std.stdio.readln(C, R)(ref C[] buf, R terminator) if (isSomeChar!C is(Unqual!C == C) !is(C == enum) isBidirectionalRange!R is(typeof(terminator.front == (dchar).init))) Now, I'm used to 'buffer' meaning one thing, but here it seems that buffer means something more akin to a 'sink' object, or a forced dynamic array type? Is there some way I can avoid dynamic allocations? Thanks!
Re: readln with buffer fails
You need to take a slice of the buffer: char[] buf = Input[]; readln(buf); // line now in buf The reason for this is because you need to know where the string ends. If you just passed in Input, how would you know how long the line read was?
Re: readln with buffer fails
On Wednesday, 29 October 2014 at 21:14:17 UTC, dcrepid wrote: I have this simple code: int main() { import std.stdio; char[4096] Input; readln(Input); //readln!(char)(Input); // also fails return 0; } I get these messages during compilation: test.d(39): Error: template std.stdio.readln cannot deduce function from argument types !()(char[4096]), candidates are: src\phobos\std\stdio.d(2818): std.stdio.readln(S = string)(dchar terminator = '\x0a') if (isSomeString!S) src\phobos\std\stdio.d(2851): std.stdio.readln(C)(ref C[] buf, dchar terminator = '\x0a') if (isSomeChar!C is(Unqual!C == C) !is(C == enum)) src\phobos\std\stdio.d(2858): std.stdio.readln(C, R)(ref C[] buf, R terminator) if (isSomeChar!C is(Unqual!C == C) !is(C == enum) isBidirectionalRange!R is(typeof(terminator.front == (dchar).init))) Now, I'm used to 'buffer' meaning one thing, but here it seems that buffer means something more akin to a 'sink' object, or a forced dynamic array type? Is there some way I can avoid dynamic allocations? Thanks! try this instead -- module runnable; import std.stdio; void main(string args[]) { char[] Input; Input.length = 4096; readln(Input); } -- Your original sample does not compile because `char[4096]` is a static array and does not verifies the redln() template constraints, e.g input range, forward range etc. Another option would be to slice Input: readln(Input[0..$-1]);
Re: readln with buffer fails
On Wed, 29 Oct 2014 21:14:13 + dcrepid via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Now, I'm used to 'buffer' meaning one thing, but here it seems that buffer means something more akin to a 'sink' object, or a forced dynamic array type? Is there some way I can avoid dynamic allocations? take a slice of your buffer: `Input[]`. slices aren't doing allocations, they just keeping pointer to data and data length together. signature.asc Description: PGP signature
Re: Need help: Return reference slice
On 10/29/2014 12:50 PM, advibm wrote: Hello, I would like to create a D function that returns a slice of a string. This slice shall be a reference to a part of the string argument. Is this generally possible in D? This is the function: auto ref betweenTwoStrings(T)(inout T src, string start, string end) { long a = src.countUntil(start); if (a 0) return src; // null a += start.length; long b = src[a..$].countUntil(end); if (b 0) return src; // null b += a; return src[a..b]; } I would like to have something like that: char[] buf; // already filled array char[] partOfBuf = betweenTwoStrings(buf, START, END); partOfBuf[0] = 'a'; // THIS should also change the 'buf' variable assert(buf[0] == 'a'); Thanks for your help There are pretty useful std.algorithm functions for searching: import std.algorithm; import std.traits; import std.array; C[] betweenTwoStrings(C)(C[] src, string start, string end) if (isSomeChar!C) { if (src.findSkip(start)) { auto found = src.findSplitBefore(end); // Just for readability: auto needleAndAfter = found[1]; if (!needleAndAfter.empty) { auto beforeNeedle = found[0]; return beforeNeedle; } } return (C[]).init; } void main() { char[] buf; buf ~= prefixSTARTactual partENDpostfix; char[] partOfBuf = betweenTwoStrings(buf, START, END); partOfBuf[] = 'a'; assert(buf == prefixSTARTaaaENDpostfix); } Ali
Dart bindings for D?
Hi. I have had a look around for these, but was not able to see them. It looks perhaps like dart_api.h is the main file to convert - I will have a crack at starting this unless anyone knows of any already in existence. Rationale for using Dart in combination with D is that I am not thrilled about learning or writing in Javascript, yet one has to do processing on the client in some language, and there seem very few viable alternatives for that. It would be nice to run D from front to back, but at least Dart has C-like syntax and is reasonably well thought out. Am I missing any existing bindings somewhere? Thanks. Laeeth.
Re: Dart bindings for D?
On Wed, 29 Oct 2014 22:12:30 + Laeeth Isharc via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: it's OT, but haven't you tried Adam Ruppe's script.d? it has d-like syntax with influences from javascript, written entirely in D, and has simple interoperability with D code. it's not lightning fast, though, but the code is understandable and it's fairly easy to extend the language if necessary. maybe it will fit to your tasks. you can take it in ARSD repository: https://github.com/adamdruppe/arsd what you need is jsvar.d and script.d, just two files and no external libs required. signature.asc Description: PGP signature
Re: readln with buffer fails
On Wednesday, 29 October 2014 at 21:19:25 UTC, Peter Alexander wrote: You need to take a slice of the buffer: char[] buf = Input[]; readln(buf); // line now in buf The reason for this is because you need to know where the string ends. If you just passed in Input, how would you know how long the line read was? Thanks, that solves the problem. I guess what confuses me is that Input isn't a slice, or at least not implicitly convertible to one. Also, I've tried using Input[] directly at the callsite but apparently that would be an rValue, and D doesn't do rValues yet. So here's a simple solution to reading a line using a fixed stack array: char[4096] Input; char[] InputSlice; // actual slice of input'd text (instead of full 4K) size_t NumChars; while (NumChars == 0) { // readln(buf) requires a slice. Input isn't converted to one, // and readln() requires an rvalue for a buffer: char[] buf = Input[]; NumChars = readln(buf); // Set InputSlice to range of text that was input, minus linefeed: InputSlice = chomp(buf[0 .. NumChars]); // Empty line? if (InputSlice == ) NumChars = 0; } Thanks all for your help
Re: readln with buffer fails
err, I meant rvalue *reference* above
Re: readln with buffer fails
lol, if only I could edit my posts. The comment preceding the readln() call was wrong too. This is what I have now: // readln(buf) requires a slice *Reference*. // rvalue references aren't supported by D, so readln(Input[]) fails
Re: readln with buffer fails
On Wed, 29 Oct 2014 23:10:10 +, dcrepid wrote: On Wednesday, 29 October 2014 at 21:19:25 UTC, Peter Alexander wrote: You need to take a slice of the buffer: char[] buf = Input[]; readln(buf); // line now in buf The reason for this is because you need to know where the string ends. If you just passed in Input, how would you know how long the line read was? Thanks, that solves the problem. I guess what confuses me is that Input isn't a slice, or at least not implicitly convertible to one. Also, I've tried using Input[] directly at the callsite but apparently that would be an rValue, and D doesn't do rValues yet. Part of what readln does is *modify* the slice itself, not just the pointed-to characters. In particular it alters the length member so that you know how much input was actually read. This is also why the rvalue reference shouldn't work. Remember, D chose not to repeat C's mistake of relying on null terminators.
Re: Dart bindings for D?
On 2014-10-29 18:12, Laeeth Isharc wrote: Rationale for using Dart in combination with D is that I am not thrilled about learning or writing in Javascript, yet one has to do processing on the client in some language, and there seem very few viable alternatives for that. It would be nice to run D from front to back, but at least Dart has C-like syntax and is reasonably well thought out. I actually thought this over in the past and posted my research here: http://forum.dlang.org/thread/ll38cn$ojv$1...@digitalmars.com It would be awesome to write front-end tools in D. However, there won't be much browser support unless you're backed by Google or Microsoft. What's going to replace javascript? Will it be typescript? asm.js? dart? PNaCl? The solution is obviously to compile from D to the target language. But what's the real advantage? Re-using some back-end MVC libraries? All the communication is actually done through sockets, there's never any real interaction between the back-end/front-end. Also, you realize the front-end stuff is so full of community contributions that you're actually shooting yourself in the foot if you divert away from the more popular language and methodologies. So, I settle with javascript, and I shop for libraries instead of writing anything at all. There's so much diversity in the front-end world, a few hundred lines of code at most are going to be necessary for an original piece of work. Heh.
Re: readln with buffer fails
On Wednesday, 29 October 2014 at 23:28:07 UTC, Justin Whear wrote: Part of what readln does is *modify* the slice itself, not just the pointed-to characters. In particular it alters the length member so that you know how much input was actually read. This is also why the rvalue reference shouldn't work. Remember, D chose not to repeat C's mistake of relying on null terminators. Nice, thanks for that. I wasn't aware the .length member was changed, but I just verified it myself by surrounding the call with some debug output. Sure enough, its length is 4096 before the call, and a different length after (depending on what was input).
Re: Dart bindings for D?
On Wednesday, 29 October 2014 at 22:12:32 UTC, Laeeth Isharc wrote: I will have a crack at starting this unless anyone knows of any already in existence. I haven't heard of any. Rationale for using Dart in combination with D is that I am not thrilled about learning or writing in Javascript, yet one has to do processing on the client in some language, and there seem very few viable alternatives for that. What kind of client are you doing? If you are writing a web page, you don't need any kind of script language API. JavaScript or dart or whatever talk with your server application through http requests or websockets, whereas script language APIs are meant for extending your application in the same process. For example, a text editor might have a script language to make custom functions for hotkeys.
Re: Dart bindings for D?
On Wednesday, 29 October 2014 at 22:22:39 UTC, ketmar via Digitalmars-d-learn wrote: it's not lightning fast, though, but the code is understandable and it's fairly easy to extend the language if necessary. Curious, what have you tried with it? I wanted to keep it simple but actually complicated it more than I wanted to, it is cool to know it isn't hard to use. What I really like though is that the var type works in D too, making interoperation so easy. My only disappointment is @property still doesn't work, making foo.bar()() need the double parens! you can take it in ARSD repository: https://github.com/adamdruppe/arsd what you need is jsvar.d and script.d, just two files and no external libs required. Here's an example usage: import arsd.script; void main() { // this var holds the global variables of the script engine var globals = var.emptyObject; // you can set up values or functions with plain assignment in D globals.myFunction = (int a, int b) { return a + b; }; import std.file; // run the interpret function passing script code and the variables interpret(readText(scriptcode.js), globals); // you can then access script values or functions from D too import std.stdio; writeln(globals.foo()(adr)); // and also interpret strings here. The interpret function // returns the value of the last expression writeln(interpret(myFunction(12, 24);, globals)); } Here's what my scriptcode.js looks like: // suppose the code there is: // the syntax is kinda like javascript and kinda like D // the concat operator is D style, but function decls are JS style function foo(name) { return hello, ~ name ~ you are ~ myFunction(12, 53) ~ years old; } // set a global variable too var myname = adam; kinda like a hybrid of D and JavaScript.
Pointer to constructor
Hi Folks, Quick question, how do I get pointer to a class constructor, let's say default constructor? I want to set it to AA, but couldn't find a way. Thanks! --Toshiaki
Re: Pointer to constructor
You can access constructors just like any other method if you use the __ctor name. So like MyClass.__ctor.
Re: Dart bindings for D?
On Thu, 30 Oct 2014 01:31:23 + Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Wednesday, 29 October 2014 at 22:22:39 UTC, ketmar via Digitalmars-d-learn wrote: it's not lightning fast, though, but the code is understandable and it's fairly easy to extend the language if necessary. Curious, what have you tried with it? nothing really complicated for now. simply used it in some utilities where i need something more than a config file. conditions, loops, some data processing. it looks like a mixture of D and JS, and i like it. it surely won't do as a scripting language for realtime videogame, but i'm not writing videogames now. ;-) and it works ok for adding some easily modifyable logic/processing to various software without resorting to external libraries and wrappers like LuaD (which is great too, by the way). I wanted to keep it simple but actually complicated it more than I wanted to, it is cool to know it isn't hard to use. yes, it was very straightforward. besides, i like to write scripting languages myself, so i enjoyed reading the source code too. but i did that after i used the engine several times. ;-) what it really needs is more documentation and samples, so people can just throw it into the project and be happy. and to give some language description to software users. i mostly using it by myseld, so didn't bother to write any dox though. ;-) What I really like though is that the var type works in D too, making interoperation so easy. My only disappointment is @property still doesn't work, making foo.bar()() need the double parens! it was great, but in the end i removed opDispatch from var, 'cause it confuses me. i'm not a fan of opDispatch anyway. ;-) but it was very funny to show some people D code with your jsvar and listenting how they don't want to learn just another scripting language. and then compile the code with DMD to confuse them even more. signature.asc Description: PGP signature
Re: Dart bindings for D?
On Wednesday, 29 October 2014 at 22:12:32 UTC, Laeeth Isharc wrote: I have had a look around for these, but was not able to see them. It looks perhaps like dart_api.h is the main file to convert - I will have a crack at starting this unless anyone knows of any already in existence. Dart VM is available as a standalone, which can be set up to act as a web server. But you want to integrate it into D? Rationale for using Dart in combination with D is that I am not thrilled about learning or writing in Javascript, yet one has to do processing on the client in some language, and there seem very few viable alternatives for that. Javascript is easy, but compiling to Javascript from D with dart2js is also ok if you only want to support the latest browsers. Dart makes most sense for internal web applications. It would be nice to run D from front to back, but at least Dart has C-like syntax and is reasonably well thought out. Am I missing any existing bindings somewhere? I don't think so, but integrating DartVM into D means you have to deal with two different garbage collectors or put a substantial amount of work into making D use the Dart collector.