Re: a function like writeln that returns a string rather than writes to a file
On Saturday, 2 May 2020 at 02:49:04 UTC, Steven Schveighoffer wrote: On 5/1/20 10:40 PM, dan wrote: On Saturday, 2 May 2020 at 02:29:43 UTC, H. S. Teoh wrote: On Sat, May 02, 2020 at 02:22:42AM +, dan via Digitalmars-d-learn wrote: [...] [...] import std.format : format; string str = format("%s %s %s", obj1, obj2, obj3); T Thanks HS! That looks like a good move, if format will do the string conversion for me. But one thing that would be troublesome is that i would have to make sure to count up the %s so that they match the number of arguments. I would like to do without that, just like writeln does. import std.conv: text; string str = text(obj1, " ", obj2, " ", obj3); -Steve Awesome, thanks Steve. That's perfect. So the function i was looking for was text (or, i guess, std.conv.text). dan
Re: a function like writeln that returns a string rather than writes to a file
On 5/1/20 10:40 PM, dan wrote: On Saturday, 2 May 2020 at 02:29:43 UTC, H. S. Teoh wrote: On Sat, May 02, 2020 at 02:22:42AM +, dan via Digitalmars-d-learn wrote: I'm looking for a function something like writeln or write, but instead of writing to stdout, it writes to a string and returns the string. [...] import std.format : format; string str = format("%s %s %s", obj1, obj2, obj3); T Thanks HS! That looks like a good move, if format will do the string conversion for me. But one thing that would be troublesome is that i would have to make sure to count up the %s so that they match the number of arguments. I would like to do without that, just like writeln does. import std.conv: text; string str = text(obj1, " ", obj2, " ", obj3); -Steve
Re: a function like writeln that returns a string rather than writes to a file
On Saturday, 2 May 2020 at 02:29:43 UTC, H. S. Teoh wrote: On Sat, May 02, 2020 at 02:22:42AM +, dan via Digitalmars-d-learn wrote: I'm looking for a function something like writeln or write, but instead of writing to stdout, it writes to a string and returns the string. [...] import std.format : format; string str = format("%s %s %s", obj1, obj2, obj3); T Thanks HS! That looks like a good move, if format will do the string conversion for me. But one thing that would be troublesome is that i would have to make sure to count up the %s so that they match the number of arguments. I would like to do without that, just like writeln does. Anyhow, though, thanks for point out format. dan
Re: a function like writeln that returns a string rather than writes to a file
On Sat, May 02, 2020 at 02:22:42AM +, dan via Digitalmars-d-learn wrote: > I'm looking for a function something like writeln or write, but instead of > writing to stdout, it writes to a string and returns the string. [...] import std.format : format; string str = format("%s %s %s", obj1, obj2, obj3); T -- Once bitten, twice cry...
a function like writeln that returns a string rather than writes to a file
I'm looking for a function something like writeln or write, but instead of writing to stdout, it writes to a string and returns the string. So i would like something like: import std.stdio; import std.conv; string write_to_string(T...)(T values ) { string s; foreach ( value; values ) s ~= to!string( value ); return s; } But because this is such a standard type of thing to do, i'd like to use whatever the standard function is for doing it, if there is one. So . . . is there one? Like maybe some way to dress a string up as a file and pass it through the usual write/writeln apparatus? My only real requirement is that it be something really easy to do. Thanks in advance for any pointers. dan
Right ways to work without gc without betterC.
I saw docs for std.experimental.allocator and also had found automem library(https://code.dlang.org/packages/automem) for c++ style memory management via smartpointers. From GC documentation std.experimental.allocator can not be used for: NewExpression Array appending Array concatenation Array literals (except when used to initialize static data) Associative array literals Any insertion, removal, or lookups in an associative array Extracting keys or values from an associative array Taking the address of (i.e. making a delegate to) a nested function that accesses variables in an outer scope A function literal that accesses variables in an outer scope An AssertExpression that fails its condition Is that actual information? Does someone work to implement switch between gc and non-gc approach? I learned for now automem's smartpointers which based on std.experimental.allocator can be used to store structs/classes. Does automem(or std.experimental.allocator) has support for inherited classes, polymorphism like std::unique_ptr (or base class) in c++? Does Phobos support std.experimental.allocator? Are there another gotchas with "c++ like" memory management way in D? P.S. Sorry for my bad english.
Re: Aliasing current function template instance
On Friday, 1 May 2020 at 21:05:17 UTC, Adam D. Ruppe wrote: On Friday, 1 May 2020 at 20:28:58 UTC, Jean-Louis Leroy wrote: Something I have overlooked? Any ideas? There's an old rule, that I can't find in the spec anymore but I'm still pretty sure it is there, where taking the address of a template inside a template yields the current instantiation. Then you can fetch the type of that and do some reflection off. So try this in your test rig: pragma(msg, Parameters!(typeof())); This rule was in there to ease callbacks and recursive functions iirc but it can also work with you thanks to typeof turning the runtime address back into a compile time alias. It looks like it does the trick. Thanks! Trying to add support for method templates to openmethods.
Re: Aliasing current function template instance
On Friday, 1 May 2020 at 20:28:58 UTC, Jean-Louis Leroy wrote: Something I have overlooked? Any ideas? There's an old rule, that I can't find in the spec anymore but I'm still pretty sure it is there, where taking the address of a template inside a template yields the current instantiation. Then you can fetch the type of that and do some reflection off. So try this in your test rig: pragma(msg, Parameters!(typeof())); This rule was in there to ease callbacks and recursive functions iirc but it can also work with you thanks to typeof turning the runtime address back into a compile time alias.
Re: Aliasing current function template instance
On Friday, 1 May 2020 at 20:28:58 UTC, Jean-Louis Leroy wrote: Is it possible, inside a function template, to create an alias to the instantiated function? IOW the equivalent of __FUNCTION__, but yielding an alias? The closest I came is: import std.string; import std.traits; void foo(T)(lazy T) { mixin( "alias thisFunction = ", __FUNCTION__[0..__FUNCTION__.lastIndexOf('.')], ";"); pragma(msg, Parameters!thisFunction); } void main() { foo(0); foo(""); } dmd -c aliasthisfunction.d (lazy int) (lazy string) ...but (unsurprisingly) this fails in presence of overloads. I.e. if I throw in: void foo(T)(int, T); ...then I get: aliasthisfunction.d(6): Error: template `aliasthisfunction.foo` matches more than one template declaration: aliasthisfunction.d(4): `foo(T)(lazy T)` and aliasthisfunction.d(20): `foo(T)(int, T)` ... Something I have overlooked? Any ideas? This should work: alias context(alias a) = __traits(parent, a); void fun() { alias ctx = context!({})(); } {} becomes a lambda inside fun(), so it's parent is fun(). The same could be done by introducing a symbol explicitly, but that pollutes the namespace. This template works inside functions, methods, lambdas, modules, structs, classes and interfaces. -- Simen
Re: Aliasing current function template instance
On Friday, 1 May 2020 at 20:43:05 UTC, Steven Schveighoffer wrote: On 5/1/20 4:28 PM, Jean-Louis Leroy wrote: Something I have overlooked? Any ideas? This trick works. No idea who came up with it: alias thisFunction = __traits(parent, {}); -Steve I think I get the idea. Alas it doesn't work inside a function template, because it returns the template, not the instance: void foo(T)(lazy T) { alias thisFunction = __traits(parent, {}); pragma(msg, thisFunction.stringof); //pragma(msg, Parameters!thisFunction); // later } void main() { foo(0); foo(""); } prints: foo(T)(lazy T) foo(T)(lazy T) Uncommenting the line that is (more or less) my real goal: aliasthisfunction.d(7): Error: template instance `std.traits.Parameters!(foo)` does not match template declaration `Parameters(func...)` with `func = (foo(T)(lazy T))` must satisfy the following constraint: ` isCallable!func`
Re: Aliasing current function template instance
On 5/1/20 4:28 PM, Jean-Louis Leroy wrote: Something I have overlooked? Any ideas? This trick works. No idea who came up with it: alias thisFunction = __traits(parent, {}); -Steve
Aliasing current function template instance
Is it possible, inside a function template, to create an alias to the instantiated function? IOW the equivalent of __FUNCTION__, but yielding an alias? The closest I came is: import std.string; import std.traits; void foo(T)(lazy T) { mixin( "alias thisFunction = ", __FUNCTION__[0..__FUNCTION__.lastIndexOf('.')], ";"); pragma(msg, Parameters!thisFunction); } void main() { foo(0); foo(""); } dmd -c aliasthisfunction.d (lazy int) (lazy string) ...but (unsurprisingly) this fails in presence of overloads. I.e. if I throw in: void foo(T)(int, T); ...then I get: aliasthisfunction.d(6): Error: template `aliasthisfunction.foo` matches more than one template declaration: aliasthisfunction.d(4): `foo(T)(lazy T)` and aliasthisfunction.d(20): `foo(T)(int, T)` ... Something I have overlooked? Any ideas?
Re: sort a string
On 5/1/20 11:17 AM, drug wrote: 01.05.2020 18:04, notna пишет: hmmm, whích results in: Error: cannot use [] operator on expression of type dchar try this: ```D import std; void main() { string word = "Привет"; dchar[] line3 = to!(dchar[])(word.dup) // make a copy to get a range of mutable char // and convert char to dchar .sort // sort it .release; // get the sorted range assert(line3 == "Пвеирт"); } ``` Nice! Yeah, I was sloppy in my newsgroup coding, sorry. One minor nit here, the to!(dchar[])(word.dup), the dup is not necessary, you are going to end up allocating a temporary array and throwing it away. Just do word.to!(dchar[]). `to` takes care of all the formalities. -Steve
Re: Python eval() equivalent in Dlang working in Runtime?
On Friday, 1 May 2020 at 15:42:54 UTC, Baby Beaker wrote: There is a Python eval() equivalent in Dlang working in Runtime? You might find arsd's script.d interesting [1], but it's more like a blend between D and javascript. [1]https://github.com/adamdruppe/arsd/blob/d0aec8e606a90c005b9cac6fcfb2047fb61b38fa/script.d
Re: Python eval() equivalent in Dlang working in Runtime?
On Fri, May 01, 2020 at 06:37:41PM +, tsbockman via Digitalmars-d-learn wrote: > On Friday, 1 May 2020 at 18:07:54 UTC, H. S. Teoh wrote: [...] > > Actually, if you're willing to ship a working copy of dmd with your > > program, you *could* compile D code on-the-fly and dynamically load > > it as a dll/shared library. > > Good to know, but that's quite different from the eval() of > interpreted languages with respect to both semantics and performance, > so it's really another item for the "D alternatives to eval()" list. True. I will say, though, that dmd is fast enough that unless you're compiling large code snippets or doing it inside an inner loop, you might not notice the slight pause! T -- Turning your clock 15 minutes ahead won't cure lateness---you're just making time go faster!
Re: Python eval() equivalent in Dlang working in Runtime?
On Friday, 1 May 2020 at 18:07:54 UTC, H. S. Teoh wrote: On Fri, May 01, 2020 at 05:44:27PM +, tsbockman via Digitalmars-d-learn wrote: On Friday, 1 May 2020 at 15:42:54 UTC, Baby Beaker wrote: > There is a Python eval() equivalent in Dlang working in > Runtime? No, and there almost certainly never will be due to fundamental differences between the languages. Depending on your goal, the closest alternatives are using the string mixin language feature, writing a parser (std.conv or certain DUB packages can help), or embedding a scripting engine such as AngelScript or Squirrel into your program. [...] Actually, if you're willing to ship a working copy of dmd with your program, you *could* compile D code on-the-fly and dynamically load it as a dll/shared library. Good to know, but that's quite different from the eval() of interpreted languages with respect to both semantics and performance, so it's really another item for the "D alternatives to eval()" list.
Re: Python eval() equivalent in Dlang working in Runtime?
On Fri, May 01, 2020 at 05:44:27PM +, tsbockman via Digitalmars-d-learn wrote: > On Friday, 1 May 2020 at 15:42:54 UTC, Baby Beaker wrote: > > There is a Python eval() equivalent in Dlang working in Runtime? > > No, and there almost certainly never will be due to fundamental > differences between the languages. Depending on your goal, the closest > alternatives are using the string mixin language feature, writing a > parser (std.conv or certain DUB packages can help), or embedding a > scripting engine such as AngelScript or Squirrel into your program. [...] Actually, if you're willing to ship a working copy of dmd with your program, you *could* compile D code on-the-fly and dynamically load it as a dll/shared library. (I have done this before in one of my projects, which generates D code based on user input then invokes dmd to compile it into a shared lib, then loads the shared lib and runs the compiled code.) It will be restricted to function calls and static variables, though; it will not be possible to access local variables in the scope where the eval() is called (unless you explicitly pass them through the dll/.so interface, i.e., as function parameters), and it will not be possible to eval() snippets smaller than a function. T -- The most powerful one-line C program: #include "/dev/tty" -- IOCCC
Re: Python eval() equivalent in Dlang working in Runtime?
On Friday, 1 May 2020 at 15:42:54 UTC, Baby Beaker wrote: There is a Python eval() equivalent in Dlang working in Runtime? No, and there almost certainly never will be due to fundamental differences between the languages. Depending on your goal, the closest alternatives are using the string mixin language feature, writing a parser (std.conv or certain DUB packages can help), or embedding a scripting engine such as AngelScript or Squirrel into your program. What are you really trying to do?
Re: Help playing sounds using arsd.simpleaudio
On Friday, 1 November 2019 at 08:54:31 UTC, johnsmith101 wrote: On Wednesday, 30 October 2019 at 19:11:00 UTC, Adam D. Ruppe wrote: On Saturday, 26 October 2019 at 19:48:33 UTC, Murilo wrote: I play a sound the program never ends, the terminal continues to run the program and I have to end it manually. Any ideas what could be causing this? I am using it just as you had instructed. That happens if you don't call .stop() and .join() in order at the right time. Did you use the scope(exit) like I said? hello, Thank you so much for help, it helped me alot Thanks and regards.:) Loved a lot https://get-9apps.com and https://get-cartoonhd.com
Re: sort a string
On Friday, 1 May 2020 at 15:17:53 UTC, drug wrote: 01.05.2020 18:04, notna пишет: hmmm, whích results in: Error: cannot use [] operator on expression of type dchar try this: ```D import std; void main() { string word = "Привет"; dchar[] line3 = to!(dchar[])(word.dup) // make a copy to get a range of mutable char // and convert char to dchar .sort // sort it .release; // get the sorted range assert(line3 == "Пвеирт"); } ``` THANKS, this looks even cleaner :)
Python eval() equivalent in Dlang working in Runtime?
There is a Python eval() equivalent in Dlang working in Runtime?
Re: sort a string
On Friday, 1 May 2020 at 15:15:29 UTC, bachmeier wrote: On Friday, 1 May 2020 at 15:04:01 UTC, notna wrote: On Friday, 1 May 2020 at 12:29:26 UTC, Steven Schveighoffer wrote: dchar[] line3 = sort(line2.to!(dchar[])); dchar[] line3 = sort(line2.to!dchar[]).release; https://dlang.org/phobos/std_range.html#.SortedRange.release hmmm, whích results in: Error: cannot use [] operator on expression of type dchar Working program: import std.algorithm, std.conv, std.string, std.stdio; void main() { string word = "bar"; string line2 = toLower!(string)(word); dchar[] line3 = sort(line2.to!(dchar[])).release; writeln(line3); } You need to add parens. well, this makes very much sense ;) THANKS a lot, works and helped to adopt some old code
Re: sort a string
On Friday, 1 May 2020 at 15:04:01 UTC, notna wrote: On Friday, 1 May 2020 at 12:29:26 UTC, Steven Schveighoffer wrote: dchar[] line3 = sort(line2.to!(dchar[])); dchar[] line3 = sort(line2.to!dchar[]).release; https://dlang.org/phobos/std_range.html#.SortedRange.release hmmm, whích results in: Error: cannot use [] operator on expression of type dchar Working program: import std.algorithm, std.conv, std.string, std.stdio; void main() { string word = "bar"; string line2 = toLower!(string)(word); dchar[] line3 = sort(line2.to!(dchar[])).release; writeln(line3); } You need to add parens.
Re: sort a string
01.05.2020 18:04, notna пишет: hmmm, whích results in: Error: cannot use [] operator on expression of type dchar try this: ```D import std; void main() { string word = "Привет"; dchar[] line3 = to!(dchar[])(word.dup) // make a copy to get a range of mutable char // and convert char to dchar .sort // sort it .release; // get the sorted range assert(line3 == "Пвеирт"); } ```
Re: sort a string
On Friday, 1 May 2020 at 12:29:26 UTC, Steven Schveighoffer wrote: dchar[] line3 = sort(line2.to!(dchar[])); dchar[] line3 = sort(line2.to!dchar[]).release; https://dlang.org/phobos/std_range.html#.SortedRange.release hmmm, whích results in: Error: cannot use [] operator on expression of type dchar
Re: Cross product in lib mir or lubeck
On Friday, 1 May 2020 at 11:29:55 UTC, Erdem wrote: Hi, I am looking for cross product function in libmir or lubeck. But I couldn't find it. Does anyone know if it exists or not? Erdem Hi, Libmir doesn't provide cross-product function. Ilya
Re: Why libmir has to add its own algorithm functions
On Friday, 1 May 2020 at 11:31:29 UTC, Erdem wrote: As can be seen in the link below : http://mir-algorithm.libmir.org/mir_algorithm_iteration.html Libmir provides almost the same function as std. Why is benefit of doing that? Wouldn't it be better to not duplicate std stuff? Erdem Some benefits: 1. Mir API can handle elementwise ndslice (multidimensional random-access ranges created using Slice type). Phobos API handles them as random-access ranges. For example, Mir's `map` applied to matrix returns a matrix, Phobos returns a lazy range or fail to compile depending on the lambda. 2. Mir iteration API (each, all, any, and others) can handle multiple arguments at once without zipping them. It is critical for multidimensional performance. 3. Mir `zip` operation supports elementwise access by reference. It is critical in some cases. 4. some @nogc/nothrow fixes, some optimizations for move semantics, and BetterC code. 5. Mir strings lambdas have fused-multiply-add transformations enabled by default. ... and more. Sure, it would be better to do not to duplicate similar API. Originally ndslice was in the std.experimental. However, it was impractical to maintain it in the std and I have moved it to the dub package. Mir is targeting to do not use std, except maybe std.traits and std.meta. We don't care about the name conflicts with std. Ilya
Re: Why libmir has to add its own algorithm functions
On Friday, 1 May 2020 at 11:31:29 UTC, Erdem wrote: As can be seen in the link below : http://mir-algorithm.libmir.org/mir_algorithm_iteration.html Libmir provides almost the same function as std. Why is benefit of doing that? Wouldn't it be better to not duplicate std stuff? Erdem I think duplication is for betterC support (in particular, avoid the GC).
Re: sort a string
01.05.2020 15:29, Steven Schveighoffer пишет: Don't do this, use to!(dchar[]) as you have above. This will create incorrect dchars for non-ascii text. -Steve Argh, as always you're right. Funny that I never did that and sadly that I posted wrong code. Thank you, Steven, for correction of my wrong posts, I appreciate it.
Re: sort a string
On 5/1/20 4:12 AM, drug wrote: 01.05.2020 10:38, Chris Katko пишет: I'm making anagrams. According to the nextPermutation() docs, I need to 'sort by less' to get all permutations. ... Except the doc page doesn't mention how to do that, nor does std.algorithm.sort show how to sort a string. ... and the google results on the dlang forums from 2017 don't work. I've tried .byCodeUnit. , .representation. I've tried sorting on the dchar. I've tried sorting the on string. The closest I've gotten: string word = "bar"; string line2 = toLower!(string)(word); dchar[] line3 = sort(line2.to!(dchar[])); dchar[] line3 = sort(line2.to!dchar[]).release; https://dlang.org/phobos/std_range.html#.SortedRange.release "Error: cannot implicitly convert expression sort(to(line2)) of type SortedRange!(dchar[], "a < b") to dchar[]" import std; void main() { string word = "bar"; dchar[] line3 = word.dup // make a copy to get a range of mutable elements .map!"dchar(a)" // convert char to dchar Don't do this, use to!(dchar[]) as you have above. This will create incorrect dchars for non-ascii text. -Steve
Why libmir has to add its own algorithm functions
As can be seen in the link below : http://mir-algorithm.libmir.org/mir_algorithm_iteration.html Libmir provides almost the same function as std. Why is benefit of doing that? Wouldn't it be better to not duplicate std stuff? Erdem
Cross product in lib mir or lubeck
Hi, I am looking for cross product function in libmir or lubeck. But I couldn't find it. Does anyone know if it exists or not? Erdem
Re: sort a string
On Friday, 1 May 2020 at 08:17:33 UTC, norm wrote: On Friday, 1 May 2020 at 07:38:53 UTC, Chris Katko wrote: [...] You need to convert the sort output to dchar[], e.g. --- dchar[] line3 = sort(line2.to!(dchar[])).to!(dchar[]); --- Cheers, Norm That works, thanks!
Re: sort a string
On Friday, 1 May 2020 at 07:38:53 UTC, Chris Katko wrote: I'm making anagrams. According to the nextPermutation() docs, I need to 'sort by less' to get all permutations. ... Except the doc page doesn't mention how to do that, nor does std.algorithm.sort show how to sort a string. ... and the google results on the dlang forums from 2017 don't work. I've tried .byCodeUnit. , .representation. I've tried sorting on the dchar. I've tried sorting the on string. The closest I've gotten: string word = "bar"; string line2 = toLower!(string)(word); dchar[] line3 = sort(line2.to!(dchar[])); "Error: cannot implicitly convert expression sort(to(line2)) of type SortedRange!(dchar[], "a < b") to dchar[]" You need to convert the sort output to dchar[], e.g. --- dchar[] line3 = sort(line2.to!(dchar[])).to!(dchar[]); --- Cheers, Norm
Re: sort a string
01.05.2020 10:38, Chris Katko пишет: I'm making anagrams. According to the nextPermutation() docs, I need to 'sort by less' to get all permutations. ... Except the doc page doesn't mention how to do that, nor does std.algorithm.sort show how to sort a string. ... and the google results on the dlang forums from 2017 don't work. I've tried .byCodeUnit. , .representation. I've tried sorting on the dchar. I've tried sorting the on string. The closest I've gotten: string word = "bar"; string line2 = toLower!(string)(word); dchar[] line3 = sort(line2.to!(dchar[])); "Error: cannot implicitly convert expression sort(to(line2)) of type SortedRange!(dchar[], "a < b") to dchar[]" import std; void main() { string word = "bar"; dchar[] line3 = word.dup // make a copy to get a range of mutable elements .map!"dchar(a)" // convert char to dchar .array // convert range to random access range (dynamic array here) to enable sorting .sort // sort .array; // convert SortedRange to dynamic array assert(line3 == "abr"); }
sort a string
I'm making anagrams. According to the nextPermutation() docs, I need to 'sort by less' to get all permutations. ... Except the doc page doesn't mention how to do that, nor does std.algorithm.sort show how to sort a string. ... and the google results on the dlang forums from 2017 don't work. I've tried .byCodeUnit. , .representation. I've tried sorting on the dchar. I've tried sorting the on string. The closest I've gotten: string word = "bar"; string line2 = toLower!(string)(word); dchar[] line3 = sort(line2.to!(dchar[])); "Error: cannot implicitly convert expression sort(to(line2)) of type SortedRange!(dchar[], "a < b") to dchar[]"