Re: Passing a byLine as an argument to InputRange
On Thursday, 13 May 2021 at 18:31:41 UTC, Ali Çehreli wrote: On 5/13/21 11:29 AM, Ali Çehreli wrote: create a dynamic InputRange!string object for dynamic polymorphism that InputRange provides. And that's achieved by function inputRangeObject(): Some examples: http://ddili.org/ders/d.en/ranges_more.html#ix_ranges_more.polymorphism,%20run-time Ali This book has saved my life many times! ☀
Re: Passing a byLine as an argument to InputRange
On Thursday, 13 May 2021 at 18:29:08 UTC, Ali Çehreli wrote: On 5/13/21 10:07 AM, Jeff wrote: > I have a class where I'd like to supply it with an InputRange!string. > Yet, for the life of me I can't seem to pass to it a File.byLine As Adam said, your range elements need to be converted to string e.g. with 'text' (the same as to!string). However, you must also create a dynamic InputRange!string object for dynamic polymorphism that InputRange provides. And that's achieved by function inputRangeObject(): Thank you all. I'm up and running. ;-)
Re: how do I implement opSlice for retro range?
sorry, in this code i mean b must be [5, 4] ```d auto arr = [1, 2, 3, 4, 5]; auto a = new A!int(arr); auto b = a.retro[0 .. 2]; //5,4 ```
how do I implement opSlice for retro range?
How can I implement ranges in the retro range? I'd like to do this without allocate a new array with .array from std.array, can I do that? use like this: ```d auto arr = [1, 2, 3, 4, 5]; auto a = new A!int(arr); auto b = a.retro[0 .. 2]; // 4, 5 ``` the class: ```d class A(T) { private T[] arr; this(T[] a) { arr = a; } auto opIndex() nothrow { return Range(arr); } auto retro() { return RangeRetro(arr); } protected static struct Range { T[] a; T front() { return a[0]; } T back() { return a[$ - 1]; } void popFront() { a = a[1 .. $]; } bool empty() { return a.length == 0; } } protected static struct RangeRetro { import std.range : popFront; import std.range : popBack; T[] a; T front() { return a[$ - 1]; } T back() { return a[0]; } void popBack() { a.popFront(); } void popFront() { a.popBack(); } bool empty() { return a.length == 0; } auto opSlice(size_t start, size_t end) { ??? } } } ```
Re: Is inc function part of the library ?
I have the feeling it is a pragmatic definition of pure. (isClose) Although a compiler message , which does not even be a warning, is always a nice to have.
Re: Is inc function part of the library ?
On Thursday, 13 May 2021 at 21:41:48 UTC, Imperatorn wrote: [...] [1] https://en.wikipedia.org/wiki/Pure_function Just fyi: https://dlang.org/articles/safed.html Replied to the wrong post?
Re: String "dequote" in phobos?
On Thursday, 13 May 2021 at 17:13:40 UTC, kdevel wrote: On Thursday, 13 May 2021 at 16:40:29 UTC, Imperatorn wrote: Wouldn't this just this do that? 🤔 ```d string dequote(string s) { return s[1..$-1]; } ``` 1. Your code throws Range errors if s.length < 2. 2. assert(dequote(`"fo\"o"`) == `fo"o`) fails 3. dequote(`"fo"o"`) does not throw. WI spent 2 seconds thinking about it and 18 seconds typing on my phone so I'm not so surprised it didn't work. It did "Ok" for a 20 sec attempt tho 😁
Re: How use Predicate (alias pred = "a*b")?
On Thursday, 13 May 2021 at 21:38:25 UTC, Adam D. Ruppe wrote: On Thursday, 13 May 2021 at 21:30:43 UTC, Marcone wrote: template foo(alias pred = "a*b"){ void foo(int x, int y){ writeln(x.unaryFun!pred); First, you really shouldn't use these at all. instead of a string, just pass an actual function to the thing as the predicate. but if you must use it, unaryFun has one argument, so just a. if you want a and b, two arguments, that's binaryFun. Thank you. binaryFun solved the problem.
Re: How use Predicate (alias pred = "a*b")?
On Thursday, 13 May 2021 at 21:38:25 UTC, Adam D. Ruppe wrote: On Thursday, 13 May 2021 at 21:30:43 UTC, Marcone wrote: template foo(alias pred = "a*b"){ void foo(int x, int y){ writeln(x.unaryFun!pred); First, you really shouldn't use these at all. instead of a string, just pass an actual function to the thing as the predicate. but if you must use it, unaryFun has one argument, so just a. if you want a and b, two arguments, that's binaryFun. This is just a simple example of how it works. I won't use it. However, I believe it will be very useful for meta programming.
Re: Is inc function part of the library ?
On Thursday, 13 May 2021 at 17:48:34 UTC, kdevel wrote: On Thursday, 13 May 2021 at 13:45:50 UTC, Adam D. Ruppe wrote: On Thursday, 13 May 2021 at 13:30:29 UTC, Alain De Vos wrote: [...] pure means it doesn't depend on any mutable info outside its arguments. You are only working on the arguments there so it is ok. Then D's pure does not match up with WP's definition [1] of pure, at least not 2. The function application has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams). WP quotes Bartosz Milewski: 2. A function has no side effects. Calling a function once is the same as calling it twice and discarding the result of the first call. [1] https://en.wikipedia.org/wiki/Pure_function Just fyi: https://dlang.org/articles/safed.html
Re: How use Predicate (alias pred = "a*b")?
On Thursday, 13 May 2021 at 21:30:43 UTC, Marcone wrote: template foo(alias pred = "a*b"){ void foo(int x, int y){ writeln(x.unaryFun!pred); First, you really shouldn't use these at all. instead of a string, just pass an actual function to the thing as the predicate. but if you must use it, unaryFun has one argument, so just a. if you want a and b, two arguments, that's binaryFun.
How use Predicate (alias pred = "a*b")?
import std; template foo(alias pred = "a*b"){ void foo(int x, int y){ writeln(x.unaryFun!pred); } } void main(){ foo(5, 4); } "a" works, but "b" not work. I get this error: Error: undefined identifier `b`
Re: ref struct member function
On 5/13/21 12:40 PM, Steven Schveighoffer wrote: On 5/13/21 3:21 PM, PinDPlugga wrote: This works but issues a deprecation warning: ``` onlineapp.d(30): Deprecation: returning `this` escapes a reference to parameter `this` onlineapp.d(30): perhaps annotate the parameter with `return` Fraction(1, 3) ``` I found several other ways to make this work without the warnings, but I am wondering how to make this particular case work, or if it is not a recommended way, what the proper way would be. Just follow the recommendation: ```d ref Fraction reduce() return { // annotated with return ``` This means, "I will return all or part of the parameter passed in" (the parameter being the `this` reference) -Steve I was writing this example that shows a use case for the problem (marked with BUG below): struct Fraction { auto n = 0L; auto d = 1L; // ... other bits ... ref Fraction reduce() { import std.numeric : gcd; // v = gcd(n, d); // if (v > 1) { // n /= v; // d /= v; // } return this; } // ... etc ... } ref foo() { import std.stdio : writeln; auto f = Fraction(3, 9); // BUG: Returns reference to an object on the stack return f.reduce(); } void main() { // Refers to a temporary (it would be a copy without the &) auto f = &foo(); // Do some unrelated things hoping that space for dead objects on // the stack will be reused. import std.stdio; import std.random; writeln("doing something else: ", uniform(0, 6)); writeln(f.d); // Access dead and overwritten object (does NOT print // 9 on my system) } Putting 'return' where Steve shows makes the compiler guard us against this misuse and the program thankfully does not compile. Ali
Re: ref struct member function
On 5/13/21 3:21 PM, PinDPlugga wrote: This works but issues a deprecation warning: ``` onlineapp.d(30): Deprecation: returning `this` escapes a reference to parameter `this` onlineapp.d(30): perhaps annotate the parameter with `return` Fraction(1, 3) ``` I found several other ways to make this work without the warnings, but I am wondering how to make this particular case work, or if it is not a recommended way, what the proper way would be. Just follow the recommendation: ```d ref Fraction reduce() return { // annotated with return ``` This means, "I will return all or part of the parameter passed in" (the parameter being the `this` reference) -Steve
ref struct member function
Hi I am working through Programming in D. In one exercise I have a helper function in a struct ```D struct Fraction { auto n = 0L; auto d = 1L; // ... other bits ... ref Fraction reduce() { import std.numeric : gcd; v = gcd(n, d); if (v > 1) { n /= v; d /= v; } return this; } // ... etc ... } void main() { import std.stdio : writeln; auto f = Fraction(3, 9); writeln(f.reduce()); } ``` This works but issues a deprecation warning: ``` onlineapp.d(30): Deprecation: returning `this` escapes a reference to parameter `this` onlineapp.d(30):perhaps annotate the parameter with `return` Fraction(1, 3) ``` I found several other ways to make this work without the warnings, but I am wondering how to make this particular case work, or if it is not a recommended way, what the proper way would be.
Re: Passing a byLine as an argument to InputRange
On 5/13/21 11:29 AM, Ali Çehreli wrote: create a dynamic InputRange!string object for dynamic polymorphism that InputRange provides. And that's achieved by function inputRangeObject(): Some examples: http://ddili.org/ders/d.en/ranges_more.html#ix_ranges_more.polymorphism,%20run-time Ali
Re: Passing a byLine as an argument to InputRange
On 5/13/21 10:07 AM, Jeff wrote: > I have a class where I'd like to supply it with an InputRange!string. > Yet, for the life of me I can't seem to pass to it a File.byLine As Adam said, your range elements need to be converted to string e.g. with 'text' (the same as to!string). However, you must also create a dynamic InputRange!string object for dynamic polymorphism that InputRange provides. And that's achieved by function inputRangeObject(): import std.range; import std.stdio; import std.algorithm; import std.conv; class Foo { private InputRange!string source; this(InputRange!string s) { source = s; } // do stuff with it } void main() { new Foo(inputRangeObject(File("stuff.txt").byLine.map!text)); } Ali
Re: Is inc function part of the library ?
On Thursday, 13 May 2021 at 17:48:34 UTC, kdevel wrote: Then D's pure does not match up with WP's definition [1] of pure, at least not Yeah, D's pure is actually useful without being a huge hassle. Makes it into a useful building block that can be used inside other scenarios than the purely pure does. If you want immutable, that's a separate thing in D. You can use it on its own, or combine it with D pure to get to the full formal pure definition.
Re: Is inc function part of the library ?
On Thursday, 13 May 2021 at 13:45:50 UTC, Adam D. Ruppe wrote: On Thursday, 13 May 2021 at 13:30:29 UTC, Alain De Vos wrote: Or have I a wrong understanding of pure or the compiler. pure means it doesn't depend on any mutable info outside its arguments. You are only working on the arguments there so it is ok. Then D's pure does not match up with WP's definition [1] of pure, at least not 2. The function application has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams). WP quotes Bartosz Milewski: 2. A function has no side effects. Calling a function once is the same as calling it twice and discarding the result of the first call. [1] https://en.wikipedia.org/wiki/Pure_function
Re: Passing a byLine as an argument to InputRange
On Thursday, 13 May 2021 at 17:07:51 UTC, Jeff wrote: I have a class where I'd like to supply it with an InputRange!string. Yet, for the life of me I can't seem to pass to it a File.byLine, even though the documentation states it's an InputRange. byLine is not a range of string. It is a range of mutable char[]. I think byLineCopy yields strings though, try that. And if it doesn't work mabye `InputRange!(char[])` or similar might work.
Re: String "dequote" in phobos?
On Thursday, 13 May 2021 at 16:40:29 UTC, Imperatorn wrote: Wouldn't this just this do that? 🤔 ```d string dequote(string s) { return s[1..$-1]; } ``` 1. Your code throws Range errors if s.length < 2. 2. assert(dequote(`"fo\"o"`) == `fo"o`) fails 3. dequote(`"fo"o"`) does not throw.
Re: String "dequote" in phobos?
On Thursday, 13 May 2021 at 16:40:29 UTC, Imperatorn wrote: Wouldn't this just this do that? 🤔 ```d string dequote(string s) { return s[1..$-1]; } ``` The idea would be for situations where it isn't known in advance whether the string is quoted, if it is quoted properly, and whether there are escaped quotes within the string that need to be un-escaped. Additionally some data sources may handle escaping quotes in strings differently (e.g. `\"` vs `""`)
Re: String "dequote" in phobos?
On Thursday, 13 May 2021 at 14:10:08 UTC, cc wrote: Does something to dequote (unquote? or what would you call it?) a string exist in the standard library? I didn't see one in std.string, just wondering before reinventing the wheel. Something like: ```d assert(dequote(`"foo"`) == "foo"); assert(dequote(`'foo'`) == "foo"); assert(dequote(`"foo's"`) == "foo's"); assert(dequote(`'foo "bar"'`) == `foo "bar"`); assert(dequote(`"fo\"o"`) == `fo"o`); dequote(`"fo"o"`); // bad quoting, maybe throw an exception here or something? ``` I called mine `unquoted`[1] and `unsinglequoted`. Not very imaginative, I concede. I also assumed ASCII and fearlessly sliced `line[1..$-1]`, which you may or may not be able to do. ```d private T unenclosed(char token = '"', T)(const T line) pure nothrow @nogc if (isSomeString!T) { enum escaped = "\\" ~ token; if (line.length < 2) { return line; } else if ((line[0] == token) && (line[$-1] == token)) { if ((line.length >= 3) && (line[$-2..$] == escaped)) { // End quote is escaped return line; } return line[1..$-1].unenclosed!token; } else { return line; } } pragma(inline, true) T unquoted(T)(const T line) pure nothrow @nogc { return unenclosed!'"'(line); } unittest { assert(`"Lorem ipsum sit amet"`.unquoted == "Lorem ipsum sit amet"); assert(`"Lorem ipsum sit amet"`.unquoted == "Lorem ipsum sit amet"); // Unbalanced quotes are left untouched assert(`"Lorem ipsum sit amet`.unquoted == `"Lorem ipsum sit amet`); assert(`"Lorem \"`.unquoted == `"Lorem \"`); assert("\"Lorem \\\"".unquoted == "\"Lorem \\\""); assert(`"\"`.unquoted == `"\"`); } pragma(inline, true) T unsinglequoted(T)(const T line) pure nothrow @nogc { return unenclosed!'\''(line); } // ... ``` I'm not sure it's quite correct for all valid inputs but it worked well enough for my purposes. [1]: http://lu.dpldocs.info/lu.string.unquoted.html
Passing a byLine as an argument to InputRange
I have a class where I'd like to supply it with an InputRange!string. Yet, for the life of me I can't seem to pass to it a File.byLine, even though the documentation states it's an InputRange. ``` class Foo { private InputRange!string source; this(InputRange!string s) { source = s; } // do stuff with it } new Foo(File("stuff.txt").byLine); // Error constructor Foo.this(InputRange!string) is not callable using argument types (ByLineImpl!(char, char)) ``` I have to believe this is possible. I'm thinking maybe I need to template the constructor with something like: ``` this(R)(R s) if (isInputRange!R) ``` But, then I seem to have 2 problems: 1. I haven't ensured it's an InputRange!string 2. I've just punted the problem up a level, because my member variable doesn't work. Any help appreciated. Thanks!
Re: String "dequote" in phobos?
On Thursday, 13 May 2021 at 14:10:08 UTC, cc wrote: Does something to dequote (unquote? or what would you call it?) a string exist in the standard library? I didn't see one in std.string, just wondering before reinventing the wheel. Something like: ```d assert(dequote(`"foo"`) == "foo"); assert(dequote(`'foo'`) == "foo"); assert(dequote(`"foo's"`) == "foo's"); assert(dequote(`'foo "bar"'`) == `foo "bar"`); assert(dequote(`"fo\"o"`) == `fo"o`); dequote(`"fo"o"`); // bad quoting, maybe throw an exception here or something? ``` Wouldn't this just this do that? 🤔 ```d string dequote(string s) { return s[1..$-1]; } ```
Re: Is inc function part of the library ?
13.05.2021 16:30, Alain De Vos пишет: Shouldn't the compiler error it is not pure ? Or have I a wrong understanding of pure or the compiler. The function is pure. If you call it several times passing the same argument it will return the same result. https://run.dlang.io/is/futqjP
Re: String "dequote" in phobos?
On Thursday, 13 May 2021 at 14:10:08 UTC, cc wrote: Does something to dequote (unquote? or what would you call it?) a string exist in the standard library? I didn't see one in std.string, just wondering before reinventing the wheel. Something like: ```d assert(dequote(`"foo"`) == "foo"); assert(dequote(`'foo'`) == "foo"); assert(dequote(`"foo's"`) == "foo's"); assert(dequote(`'foo "bar"'`) == `foo "bar"`); assert(dequote(`"fo\"o"`) == `fo"o`); dequote(`"fo"o"`); // bad quoting, maybe throw an exception here or something? ``` I don't think there's anything like this in the standard library. If you write your own, consider publishing it on code.dlang.org.
String "dequote" in phobos?
Does something to dequote (unquote? or what would you call it?) a string exist in the standard library? I didn't see one in std.string, just wondering before reinventing the wheel. Something like: ```d assert(dequote(`"foo"`) == "foo"); assert(dequote(`'foo'`) == "foo"); assert(dequote(`"foo's"`) == "foo's"); assert(dequote(`'foo "bar"'`) == `foo "bar"`); assert(dequote(`"fo\"o"`) == `fo"o`); dequote(`"fo"o"`); // bad quoting, maybe throw an exception here or something? ```
Re: Is inc function part of the library ?
On Thursday, 13 May 2021 at 13:30:29 UTC, Alain De Vos wrote: Or have I a wrong understanding of pure or the compiler. pure means it doesn't depend on any mutable info outside its arguments. You are only working on the arguments there so it is ok.
Re: Is inc function part of the library ?
Shouldn't the compiler error it is not pure ? Or have I a wrong understanding of pure or the compiler.
Re: Is inc function part of the library ?
On Thursday, 13 May 2021 at 12:56:49 UTC, Alain De Vos wrote: Writing an inc function is a fascinating voyage. A function on module level did not worked because it had no this context. This works: ``` void main(){ ref int inc2(ref int x) return pure nothrow @nogc @safe{ ++x; return x; } int x=0; inc2(inc2(x)); writeln(x); ``` The thing is i don't find the function pure. Take a look at https://dlang.org/phobos/core_atomic.html#.atomicOp
Re: Is inc function part of the library ?
Writing an inc function is a fascinating voyage. A function on module level did not worked because it had no this context. This works: ``` void main(){ ref int inc2(ref int x) return pure nothrow @nogc @safe{ ++x; return x; } int x=0; inc2(inc2(x)); writeln(x); ``` The thing is i don't find the function pure.
Re: Is inc function part of the library ?
On Thursday, 13 May 2021 at 11:21:58 UTC, Alain De Vos wrote: I wanted to use the inc function (increment by one) but it is not recognised. So searched google "inc dlang" but that returned nothing informative. Normally I would issue a "grep" in some files to find if a function exists. Is "inc" part as function of the library someonewhere and more important, a practical question, how can I do an extensive search in the library to functions when i know more or less their name , but don't know their exact place as module or package. Do you mean atomic increment? 🤔
Re: Is inc function part of the library ?
On Thursday, 13 May 2021 at 11:21:58 UTC, Alain De Vos wrote: [...] how can I do an extensive search in the library to functions when i know more or less their name , but don't know their exact place as module or package. An alternative to the official documentation is [dpldocs](http://dpldocs.info/experimental-docs/dpldocs.home.html) from Adam. It is inofficial (and sometimes a little bit outdated), but it has a different search algorithm and often points you to places, you may have missed using the official documentation.
Is inc function part of the library ?
I wanted to use the inc function (increment by one) but it is not recognised. So searched google "inc dlang" but that returned nothing informative. Normally I would issue a "grep" in some files to find if a function exists. Is "inc" part as function of the library someonewhere and more important, a practical question, how can I do an extensive search in the library to functions when i know more or less their name , but don't know their exact place as module or package.
Re: Is inc function part of the library ?
On Thursday, 13 May 2021 at 11:21:58 UTC, Alain De Vos wrote: important, a practical question, how can I do an extensive search in the library to functions when i know more or less their name , but don't know their exact place as module or package. In the search bar at the top of the page, enter your search term(s), select "Library" from the drop down.
Re: Is inc function part of the library ?
On Thursday, 13 May 2021 at 11:21:58 UTC, Alain De Vos wrote: I wanted to use the inc function (increment by one) but it is not recognised. So searched google "inc dlang" but that returned nothing informative. Normally I would issue a "grep" in some files to find if a function exists. Is "inc" part as function of the library someonewhere and more important, a practical question, how can I do an extensive search in the library to functions when i know more or less their name , but don't know their exact place as module or package. It's not a function. It's an operator: ```d int i = 10; ++i; ```
Re: How to use dub with our own package
On Wednesday, 12 May 2021 at 20:55:07 UTC, Christian Köstlin wrote: if you want to do a separate package later on, you only have to change a little in your project setup, code can stay the same. kind regards, Christian That's nice. :)
Re: Issue with small floating point numbers
On Thursday, 13 May 2021 at 03:03:37 UTC, Tim wrote: ``` unittest{ auto p = rotate2D([0.0, 10.0], PI_2); assert(p == [-10.0, 0.0]); } ``` I suggest ``` unittest { auto p = rotate2D([0.0, 10.0], PI_2); assert(isClose(p[0], -10.0)); assert(isClose(p[1], 0.0, 0.0, 1e-6)); } ``` In the second test, the value is compared against zero, which is somewhat special - you need to specify an acceptable distance from zero to get it right. You could also improve your result by making the `phi` a `double` value. In this case you can replace the `1e-6` above by `1e-15` which is much closer to zero.