Re: A slice consisting of non-consecutive elements of an array?

2022-01-11 Thread vit via Digitalmars-d-learn
On Wednesday, 12 January 2022 at 06:58:47 UTC, vit wrote: On Wednesday, 12 January 2022 at 06:43:40 UTC, forkit wrote: On Wednesday, 12 January 2022 at 06:16:49 UTC, vit wrote: Yes std.algorithm : filter. ```d import std.stdio : writeln; import std.algorithm : filter; void main()@safe{

Re: A slice consisting of non-consecutive elements of an array?

2022-01-11 Thread vit via Digitalmars-d-learn
On Wednesday, 12 January 2022 at 06:43:40 UTC, forkit wrote: On Wednesday, 12 January 2022 at 06:16:49 UTC, vit wrote: Yes std.algorithm : filter. ```d import std.stdio : writeln; import std.algorithm : filter; void main()@safe{ auto a = ["one", "one", "two", "one", "two", "one",

Re: A slice consisting of non-consecutive elements of an array?

2022-01-11 Thread forkit via Digitalmars-d-learn
On Wednesday, 12 January 2022 at 06:16:49 UTC, vit wrote: Yes std.algorithm : filter. ```d import std.stdio : writeln; import std.algorithm : filter; void main()@safe{ auto a = ["one", "one", "two", "one", "two", "one", "one", "two"]; writeln(a); writeln(a.filter

Re: A slice consisting of non-consecutive elements of an array?

2022-01-11 Thread vit via Digitalmars-d-learn
On Wednesday, 12 January 2022 at 05:27:08 UTC, forkit wrote: I am familiar with the concept of a slice in D. However, a slice is a consecutive slice, is in not? (e.g) [4..$-1] I would like a slice (or a view, or whatever name you wanna call it), of particular elements within an array that ma

A slice consisting of non-consecutive elements of an array?

2022-01-11 Thread forkit via Digitalmars-d-learn
I am familiar with the concept of a slice in D. However, a slice is a consecutive slice, is in not? (e.g) [4..$-1] I would like a slice (or a view, or whatever name you wanna call it), of particular elements within an array that may not be consecutive. e.g. [4-7,8,10,13-16] Consider below:

Re: @safe question

2022-01-11 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 12 January 2022 at 00:45:23 UTC, H. S. Teoh wrote: IMNSHO, that @trusted lambda thing is an anti-pattern that should be avoided, needless to say already promoted. It's papering over a problem that ought to be fixed instead of being pushed under the rug. There's nothing wrong wi

Re: @safe question

2022-01-11 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jan 12, 2022 at 12:24:14AM +, forkit via Digitalmars-d-learn wrote: > On Tuesday, 11 January 2022 at 21:50:00 UTC, Paul Backus wrote: > > .. > > If you know a particular bit of code is memory safe, but the compiler > > can't prove it, you can mark that code as @trusted. For example: > >

Re: @safe question

2022-01-11 Thread forkit via Digitalmars-d-learn
On Tuesday, 11 January 2022 at 21:50:00 UTC, Paul Backus wrote: .. If you know a particular bit of code is memory safe, but the compiler can't prove it, you can mark that code as @trusted. For example: () @trusted { pointers ~= &str; )(); This example uses an immediately-invoked function

Re: @safe question

2022-01-11 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 11 January 2022 at 21:38:58 UTC, forkit wrote: On Tuesday, 11 January 2022 at 14:54:51 UTC, Paul Backus wrote: .. If you compile with -preview=dip1000, the compiler will actually keep track of which pointers point to stack memory, and will allow your original code. But -preview=dip

Re: @safe question

2022-01-11 Thread forkit via Digitalmars-d-learn
On Tuesday, 11 January 2022 at 14:54:51 UTC, Paul Backus wrote: .. If you compile with -preview=dip1000, the compiler will actually keep track of which pointers point to stack memory, and will allow your original code. But -preview=dip1000 is still somewhat experimental, and the documentation

Re: @safe question

2022-01-11 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 11 January 2022 at 10:57:28 UTC, forkit wrote: On Monday, 10 January 2022 at 03:21:46 UTC, Paul Backus wrote: Taking the address of a local variable is forbidden in @safe code. Even though str is a ref variable that points to a heap-allocated string, it is still considered a local

Re: @safe question

2022-01-11 Thread forkit via Digitalmars-d-learn
On Monday, 10 January 2022 at 03:21:46 UTC, Paul Backus wrote: Taking the address of a local variable is forbidden in @safe code. Even though str is a ref variable that points to a heap-allocated string, it is still considered a local variable because it is declared inside the body of a funct