Re: cloning array

2021-06-03 Thread Alain De Vos via Digitalmars-d-learn

```
import std.stdio;
void main(){
auto a=new int[][] (0,0);
a~=[1,2];
a~=[3,4];
auto b= a.dup;
a[0]=[5,6];
a[1][1]=7;
writeln(b);
}
```

Program above outputs [[1, 2], [3, 7]]
Which means a[1][1] and b[1][1] point to the same memory location.
But a[0] occupies a different memory location as b[0].
It is a shallow copy and is the expected behaviour.
Maybe there is need for a library function dupdeep ?


Re: cloning array

2021-06-03 Thread Alexandru Ermicioi via Digitalmars-d-learn

On Wednesday, 2 June 2021 at 15:32:38 UTC, Sean wrote:

...


You can implement deep copy using template recursion:

import std;

T[] rdup(T : U[], U)(T[] duped) {
return duped.map!(arr => arr.rdup).array;
}

T[] rdup(T)(T[] duped) {
return duped.dup;
}

void main()
{
int[][][] dupeable = [[[1], [2]], [[3]]];
auto duped = dupeable.rdup;
duped[0][0][0] = 9;
writeln("Hello D-eep copy of ", dupeable, " to ", duped);
}

Best regards,
Alexandru.


Re: wanting to try a GUI toolkit: needing some advice on which one to choose

2021-06-03 Thread Patrick Schluter via Digitalmars-d-learn

On Tuesday, 1 June 2021 at 20:56:05 UTC, someone wrote:
On Tuesday, 1 June 2021 at 16:20:19 UTC, Ola Fosheim Grøstad 
wrote:



[...]


I wasn't considering/referring to content in the browser, this 
is an entirely different arena.


[...]


Thank you! I can only agree.


Re: Schroedinger's Ranges

2021-06-03 Thread Steven Schveighoffer via Digitalmars-d-learn

On 6/3/21 9:00 AM, kdevel wrote:

a) What is the rationale behind not making byLineCopy the default?


byLine was the original implementation. byLineCopy was added later 
after the need for it became apparent.


See:

https://forum.dlang.org/post/lg4l7s$11rl$1...@digitalmars.com


THX. BTW byLineCopy defaults to immutable char. That's why one has to use

     auto tmp = File(filename).byLineCopy!(char, char);

or

     auto tmp = File(filename).byLine.map!dup;




I was going to suggest use byLineCopy!(char, char), because the second 
option with map makes a copy every time you call front.


And, my goodness, that is backwards for the template parameters. The 
terminator type should be determined by IFTI, it should never have been 
the first template parameter!


-Steve


Re: Schroedinger's Ranges

2021-06-03 Thread kdevel via Digitalmars-d-learn
a) What is the rationale behind not making byLineCopy the 
default?


byLine was the original implementation. byLineCopy was added 
later after the need for it became apparent.


See:

https://forum.dlang.org/post/lg4l7s$11rl$1...@digitalmars.com


THX. BTW byLineCopy defaults to immutable char. That's why one 
has to use


auto tmp = File(filename).byLineCopy!(char, char);

or

auto tmp = File(filename).byLine.map!dup;




Re: cloning array

2021-06-03 Thread cc via Digitalmars-d-learn

On Wednesday, 2 June 2021 at 17:50:13 UTC, Sean wrote:

On Wednesday, 2 June 2021 at 15:32:38 UTC, Sean wrote:
if so, how can I get the behavior i am searching for? Thank 
you.


My current solution, if anyone wonders : 
https://github.com/patefacio/d-help/blob/master/d-help/opmix/dup.d


You may find the `hasIndirections` template from std.traits 
useful.


```d
import std.traits : hasIndirections, ValueType;
import std.range.primitives : ElementType;
int[] a;
int[][] b;
int[string] x;
int[][string] y;
struct S { int s; }
struct T { int[] t; }

assert(!hasIndirections!( ElementType!(typeof(a)) ));
assert( hasIndirections!( ElementType!(typeof(b)) ));
assert(!hasIndirections!( ValueType!(typeof(x)) ));
assert( hasIndirections!( ValueType!(typeof(y)) ));
assert(!hasIndirections!S);
assert( hasIndirections!T);
```


Re: Schroedinger's Ranges

2021-06-03 Thread Mike Parker via Digitalmars-d-learn

On Thursday, 3 June 2021 at 10:18:25 UTC, kdevel wrote:


a) What is the rationale behind not making byLineCopy the 
default?


byLine was the original implementation. byLineCopy was added 
later after the need for it became apparent.




Re: Schroedinger's Ranges

2021-06-03 Thread Mike Parker via Digitalmars-d-learn

On Thursday, 3 June 2021 at 10:30:24 UTC, Mike Parker wrote:

On Thursday, 3 June 2021 at 10:18:25 UTC, kdevel wrote:


a) What is the rationale behind not making byLineCopy the 
default?


byLine was the original implementation. byLineCopy was added 
later after the need for it became apparent.


See:

https://forum.dlang.org/post/lg4l7s$11rl$1...@digitalmars.com


Re: Schroedinger's Ranges

2021-06-03 Thread kdevel via Digitalmars-d-learn

On Thursday, 3 June 2021 at 01:22:14 UTC, Paul Backus wrote:

auto tmp = File(filename).byLine();


`File.byLine` overwrites the previous line's data every time it 
reads a new line. If you want to store each line's data for 
later use, you need to use [`byLineCopy`][1] instead.


a) What is the rationale behind not making byLineCopy the default?

b) Does not compile:

csv.d(17): Error: function 
csv.CSVData!true.CSVData.parseCSV(char[] str) is not callable 
using argument types (string)
csv.d(17):cannot pass argument tmp.front() of type string 
to parameter char[] str
csv.d(21): Error: function 
csv.CSVData!true.CSVData.parseCSV(char[] str) is not callable 
using argument types (string)
csv.d(21):cannot pass argument e of type string to 
parameter char[] str
[...]/../../src/phobos/std/algorithm/iteration.d(525):
instantiated from here: MapResult!(__lambda2, 
ByLineCopy!(immutable(char), char))
csv.d(21):instantiated from here: 
map!(ByLineCopy!(immutable(char), char))

csv.d(40):instantiated from here: CSVData!true

c) Reminds me of the necessity to add dups here and there. And 
reminds me of "helping the compiler" [1]?


[1] 


Re: Schroedinger's Ranges

2021-06-03 Thread WebFreak001 via Digitalmars-d-learn

On Thursday, 3 June 2021 at 00:39:04 UTC, vacuum_tube wrote:
I've been trying to make a struct for CSV parsing and 
manipulating.  The code was as follows:

```
struct CSVData(bool HeaderFromFirstLine)
{
char[][] header = [];
char[][][] rest = [];

```
[...]


additionally to the other comment, you probably want to use 
`string` (`immutable(char)[]`) instead of char[] here, as you 
want your data to stay the same and not be modified after 
assignment.


If you replace them with `string` and have your code be `@safe`, 
the compiler will tell you where you try to assign your char[] 
data that may be modified and in those cases you would want to 
call `.idup` to duplicate the data to make it persistent.