Re: Passing a byLine as an argument to InputRange

2021-05-13 Thread Jeff via Digitalmars-d-learn
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

Re: how do I implement opSlice for retro range?

2021-05-13 Thread Jack via Digitalmars-d-learn
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?

2021-05-13 Thread Jack via Digitalmars-d-learn
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) {

Re: Is inc function part of the library ?

2021-05-13 Thread Alain De Vos via Digitalmars-d-learn
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 ?

2021-05-13 Thread kdevel via Digitalmars-d-learn
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?

2021-05-13 Thread Imperatorn via Digitalmars-d-learn
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"`) ==

Re: How use Predicate (alias pred = "a*b")?

2021-05-13 Thread Marcone via Digitalmars-d-learn
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

Re: How use Predicate (alias pred = "a*b")?

2021-05-13 Thread Marcone via Digitalmars-d-learn
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

Re: Is inc function part of the library ?

2021-05-13 Thread Imperatorn via Digitalmars-d-learn
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

Re: How use Predicate (alias pred = "a*b")?

2021-05-13 Thread Adam D. Ruppe via Digitalmars-d-learn
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.

How use Predicate (alias pred = "a*b")?

2021-05-13 Thread Marcone via Digitalmars-d-learn
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

2021-05-13 Thread Ali Çehreli via Digitalmars-d-learn
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`

Re: ref struct member function

2021-05-13 Thread Steven Schveighoffer via Digitalmars-d-learn
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

ref struct member function

2021-05-13 Thread PinDPlugga via Digitalmars-d-learn
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 /=

Re: Passing a byLine as an argument to InputRange

2021-05-13 Thread Ali Çehreli via Digitalmars-d-learn
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

2021-05-13 Thread Ali Çehreli via Digitalmars-d-learn
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

Re: Is inc function part of the library ?

2021-05-13 Thread Adam D. Ruppe via Digitalmars-d-learn
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

Re: Is inc function part of the library ?

2021-05-13 Thread kdevel via Digitalmars-d-learn
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

Re: Passing a byLine as an argument to InputRange

2021-05-13 Thread Adam D. Ruppe via Digitalmars-d-learn
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

Re: String "dequote" in phobos?

2021-05-13 Thread kdevel via Digitalmars-d-learn
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?

2021-05-13 Thread cc via Digitalmars-d-learn
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

Re: String "dequote" in phobos?

2021-05-13 Thread Anonymouse via Digitalmars-d-learn
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");

Passing a byLine as an argument to InputRange

2021-05-13 Thread Jeff via Digitalmars-d-learn
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;

Re: String "dequote" in phobos?

2021-05-13 Thread Imperatorn via Digitalmars-d-learn
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");

Re: Is inc function part of the library ?

2021-05-13 Thread drug via Digitalmars-d-learn
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?

2021-05-13 Thread Paul Backus via Digitalmars-d-learn
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");

String "dequote" in phobos?

2021-05-13 Thread cc via Digitalmars-d-learn
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");

Re: Is inc function part of the library ?

2021-05-13 Thread Adam D. Ruppe via Digitalmars-d-learn
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 ?

2021-05-13 Thread Alain De Vos via Digitalmars-d-learn
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 ?

2021-05-13 Thread Imperatorn via Digitalmars-d-learn
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;

Re: Is inc function part of the library ?

2021-05-13 Thread Alain De Vos via Digitalmars-d-learn
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

Re: Is inc function part of the library ?

2021-05-13 Thread Imperatorn via Digitalmars-d-learn
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

Re: Is inc function part of the library ?

2021-05-13 Thread Berni44 via Digitalmars-d-learn
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

Is inc function part of the library ?

2021-05-13 Thread Alain De Vos via Digitalmars-d-learn
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

Re: Is inc function part of the library ?

2021-05-13 Thread Mike Parker via Digitalmars-d-learn
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,

Re: Is inc function part of the library ?

2021-05-13 Thread Mike Parker via Digitalmars-d-learn
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

Re: How to use dub with our own package

2021-05-13 Thread Vinod K Chandran via Digitalmars-d-learn
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

2021-05-13 Thread Berni44 via Digitalmars-d-learn
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)); }