Re: automate tuple creation

2022-01-22 Thread forkit via Digitalmars-d-learn
On Saturday, 22 January 2022 at 01:33:16 UTC, Steven Schveighoffer wrote: That second `valuesPerRecord` is not used in the lambda, and also it's not referring to the original element, it's the name of a parameter in the lambda. Are you sure this is doing what you want? -Steve It just

Re: automate tuple creation

2022-01-21 Thread forkit via Digitalmars-d-learn
On Saturday, 22 January 2022 at 01:33:16 UTC, Steven Schveighoffer wrote: so I why watching this video by Andrei: https://www.youtube.com/watch?v=mCrVYYlFTrA In it, he talked about writing the simplest design that could possibly work Which got me thinking // module test;

Re: automate tuple creation

2022-01-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/21/22 6:24 PM, forkit wrote: On Friday, 21 January 2022 at 22:25:32 UTC, forkit wrote: I really like how alias and mixin can simplify my code even further: //--- int[][int][] CreateDataSet (const(int) recordsNeeded, const(int) valuesPerRecord) {     int[][int][] records;    

Re: automate tuple creation

2022-01-21 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 22:25:32 UTC, forkit wrote: I really like how alias and mixin can simplify my code even further: //--- int[][int][] CreateDataSet (const(int) recordsNeeded, const(int) valuesPerRecord) { int[][int][] records; records.reserve(recordsNeeded); const

Re: automate tuple creation

2022-01-21 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 21:56:33 UTC, H. S. Teoh wrote: What's the point of calling .dup here? The only reference to records is going out of scope, so why can't you just return it? The .dup is just creating extra work for nothing. T good pickup. thanks ;-) // module test;

Re: automate tuple creation

2022-01-21 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 21, 2022 at 09:43:38PM +, forkit via Digitalmars-d-learn wrote: > On Friday, 21 January 2022 at 21:01:11 UTC, forkit wrote: [...] > even better, I got rid of all those uncessary arrays ;-) > > // --- > > int[][int][] CreateDataSet > (const(int) recordsNeeded,

Re: automate tuple creation

2022-01-21 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 21:43:38 UTC, forkit wrote: oops... should be: // --- int[][int][] CreateDataSet (const(int) recordsNeeded, const(int)valuesPerRecord) { int[][int][] records; records.reserve(recordsNeeded); const int iotaStartNum = 100_000_001; foreach(i, id;

Re: automate tuple creation

2022-01-21 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 21:01:11 UTC, forkit wrote: even better, I got rid of all those uncessary arrays ;-) // --- int[][int][] CreateDataSet (const(int) recordsNeeded, const(int)valuesPerRecord) { int[][int][] records; records.reserve(recordsNeeded); foreach(i, id;

Re: automate tuple creation

2022-01-21 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 18:50:46 UTC, Steven Schveighoffer wrote: Yeah, iota is a random-access range, so you can just pass it directly, and not allocate anything. Looking at the usage, it doesn't need to be an array at all. But modifying the code to properly accept the range might

Re: automate tuple creation

2022-01-21 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 18:36:42 UTC, H. S. Teoh wrote: This is wasteful if you're not planning to use every ID in this million-entry long array. Much better to just use an AA to keep track of which IDs have already been generated instead. Of course, if you plan to use most of the

Re: automate tuple creation

2022-01-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/21/22 1:36 PM, H. S. Teoh wrote: On Fri, Jan 21, 2022 at 10:12:42AM +, forkit via Digitalmars-d-learn wrote: [...] // id needs to be 9 digits, and needs to start with 999 int[] idArray = takeExactly(iota(999*10^^6, 10^^9), recordsNeeded).array; [...] This is wasteful if

Re: automate tuple creation

2022-01-21 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 21, 2022 at 10:12:42AM +, forkit via Digitalmars-d-learn wrote: [...] > Random rnd; > static this() { rnd = Random(unpredictableSeed); } // thanks Ali Actually you don't even need to do this, unless you want precise control over the initialization of your RNG. If you don't

Re: automate tuple creation

2022-01-21 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 21, 2022 at 09:10:56AM +, forkit via Digitalmars-d-learn wrote: [...] > turns out the problem has nothing to do with appender... > > It's actually this line: > > if (!idArray.canFind(x)): > > when i comment this out in the function below, the program does what I > want in

Re: automate tuple creation

2022-01-21 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 09:10:56 UTC, forkit wrote: ok... in the interest of corecting the code I posted previously... ... here is a version that actually works in secs (for a million records), as opposed to hours! // --- /+

Re: automate tuple creation

2022-01-21 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 08:53:26 UTC, Stanislav Blinov wrote: turns out the problem has nothing to do with appender... It's actually this line: if (!idArray.canFind(x)): when i comment this out in the function below, the program does what I want in seconds. only problem is, the

Re: automate tuple creation

2022-01-21 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 21 January 2022 at 03:50:37 UTC, forkit wrote: I might have to use a kindof stringbuilder instead, then write a massive string once to the file. You're using writeln, which goes through C I/O buffered writes. Whether you make one call or several is of little consequence - you're

Re: automate tuple creation

2022-01-20 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 04:08:33 UTC, forkit wrote: // -- void ProcessRecords (in int[][int][] recArray, const(string) fname) { auto file = File(fname, "w"); scope(exit) file.close; Appender!string bigString = appender!string; bigString.reserve(recArray.length);

Re: automate tuple creation

2022-01-20 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 03:57:01 UTC, H. S. Teoh wrote: std.array.appender is your friend. T :-) // -- void ProcessRecords (in int[][int][] recArray, const(string) fname) { auto file = File(fname, "w"); scope(exit) file.close; Appender!string bigString = appender!string;

Re: automate tuple creation

2022-01-20 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 21, 2022 at 03:50:37AM +, forkit via Digitalmars-d-learn wrote: [...] > I might have to use a kindof stringbuilder instead, then write a > massive string once to the file. [...] std.array.appender is your friend. T -- Meat: euphemism for dead animal. -- Flora

Re: automate tuple creation

2022-01-20 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 03:45:08 UTC, forkit wrote: On Friday, 21 January 2022 at 02:30:35 UTC, Ali Çehreli wrote: The bigger question is, why did 'formattedRecords' exist at all? You could have written the output directly to the file. Oh. this was intentional, as I wanted to write

Re: automate tuple creation

2022-01-20 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 02:30:35 UTC, Ali Çehreli wrote: The bigger question is, why did 'formattedRecords' exist at all? You could have written the output directly to the file. Oh. this was intentional, as I wanted to write once, and only once, to the file. The consequence of that

Re: automate tuple creation

2022-01-20 Thread Ali Çehreli via Digitalmars-d-learn
On 1/20/22 17:35, forkit wrote: > module test; > @safe Does that make just the following definition @safe or the entire module @safe? Trying... Yes, I am right. To make the module safe, use the following syntax: @safe: > idArray.reserve(recordsNeeded); [...] > idArray ~=

Re: automate tuple creation

2022-01-20 Thread forkit via Digitalmars-d-learn
On Friday, 21 January 2022 at 01:35:40 UTC, forkit wrote: oops. nasty mistake to make ;-) module test; @safe should be: module test; @safe:

Re: automate tuple creation

2022-01-20 Thread forkit via Digitalmars-d-learn
On Thursday, 20 January 2022 at 23:49:59 UTC, Ali Çehreli wrote: so here is final code, in idiomatic D, as far as I can tell ;-) curious output when using -profile=gc .. a line referring to: std.array.Appender!(immutable(char)[]).Appender.Data std.array.Appender!string.Appender.this

Re: automate tuple creation

2022-01-20 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/20/22 6:01 PM, forkit wrote: On Thursday, 20 January 2022 at 22:31:17 UTC, Steven Schveighoffer wrote: Because it would allow altering const data. I'm not sure I understand. At what point in this function is valuesArray modified, and thus preventing it being passed in with const?

Re: automate tuple creation

2022-01-20 Thread Ali Çehreli via Digitalmars-d-learn
On 1/20/22 15:10, Ali Çehreli wrote: > void foo(const int[]) {} // Idiomatic As H. S. Teoh would add at this point, that is not idiomatic but the following are (with different meanings): void foo(const(int)[]) {} // Idiomatic void foo(const(int[])) {} // Idiomatic > void

Re: automate tuple creation

2022-01-20 Thread Ali Çehreli via Digitalmars-d-learn
On 1/20/22 15:01, forkit wrote: > On Thursday, 20 January 2022 at 22:31:17 UTC, Steven Schveighoffer wrote: >> >> Because it would allow altering const data. >> > > I'm not sure I understand. At what point in this function is valuesArray > modified, and thus preventing it being passed in with

Re: automate tuple creation

2022-01-20 Thread forkit via Digitalmars-d-learn
On Thursday, 20 January 2022 at 22:31:17 UTC, Steven Schveighoffer wrote: Because it would allow altering const data. I'm not sure I understand. At what point in this function is valuesArray modified, and thus preventing it being passed in with const? // --- int[][int][] CreateDataSet

Re: automate tuple creation

2022-01-20 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/20/22 5:07 PM, forkit wrote: On Thursday, 20 January 2022 at 21:16:46 UTC, forkit wrote: Cannot work out why I cannot pass valuesArray in as ref const?? get error: Error: cannot append type `const(int[])[const(int)]` to type `int[][int][]` Because it would allow altering const data.

Re: automate tuple creation

2022-01-20 Thread forkit via Digitalmars-d-learn
On Thursday, 20 January 2022 at 21:16:46 UTC, forkit wrote: Cannot work out why I cannot pass valuesArray in as ref const?? get error: Error: cannot append type `const(int[])[const(int)]` to type `int[][int][]` // -- int[][int][] CreateDataSet(ref const int[] idArray, ref

Re: automate tuple creation

2022-01-20 Thread forkit via Digitalmars-d-learn
On Thursday, 20 January 2022 at 12:40:09 UTC, Stanislav Blinov wrote: Allocating 4 megs to generate 10 numbers??? You can generate a random number between 99900 and 10. ... // id needs to be 9 digits, and needs to start with 999 x = uniform(999*10^^6, 10^^9);

Re: automate tuple creation

2022-01-20 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 20 January 2022 at 12:15:56 UTC, forkit wrote: void createUniqueIDArray(ref int[] idArray, int recordsNeeded) { idArray.reserve(recordsNeeded); debug { writefln("idArray.capacity is %s", idArray.capacity); } // id needs to be 9 digits, and needs to start with 999

Re: automate tuple creation

2022-01-20 Thread forkit via Digitalmars-d-learn
On Thursday, 20 January 2022 at 10:11:10 UTC, bauss wrote: Don't make them random then, but use an incrementor. If you can have ids that aren't integers then you could use uuids too. https://dlang.org/phobos/std_uuid.html The 'uniqueness' of id would actually be created in the database.

Re: automate tuple creation

2022-01-20 Thread bauss via Digitalmars-d-learn
On Thursday, 20 January 2022 at 04:00:59 UTC, forkit wrote: On Thursday, 20 January 2022 at 00:30:44 UTC, H. S. Teoh wrote: Do the id's have to be unique? yep... Don't make them random then, but use an incrementor. If you can have ids that aren't integers then you could use uuids too.

Re: automate tuple creation

2022-01-19 Thread forkit via Digitalmars-d-learn
On Thursday, 20 January 2022 at 04:38:39 UTC, forkit wrote: all done ;-) // --- module test; import std.stdio : writeln; import std.range : iota, isForwardRange, hasSlicing, hasLength, isInfinite; import std.array : array, Appender; import std.random : Random, unpredictableSeed, dice,

Re: automate tuple creation

2022-01-19 Thread forkit via Digitalmars-d-learn
On Thursday, 20 January 2022 at 04:00:59 UTC, forkit wrote: void makeUniqueIDs(ref uint[] arr, size_t sz) { ... } arrg! what was i thinking! ;-) // --- void makeUniqueIDs(ref uint[] arr, size_t sz) { arr.reserve(sz); // id needs to be 9 digits, and needs to start with 999

Re: automate tuple creation

2022-01-19 Thread forkit via Digitalmars-d-learn
On Thursday, 20 January 2022 at 00:30:44 UTC, H. S. Teoh wrote: Do the id's have to be unique? yep... I'm almost there ;-) // --- module test; import std.stdio : writeln; import std.range : iota, isForwardRange, hasSlicing, hasLength, isInfinite; import std.array : array, Appender;

Re: automate tuple creation

2022-01-19 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jan 20, 2022 at 12:12:56AM +, forkit via Digitalmars-d-learn wrote: [...] > createBoolAssociativeMatrix(mArrBool,3, 2); > > [ [1000:[1, 0]], [1001:[1, 1]], [1001:[1, 0]]] > > > where 1000 is some random id... Do the id's have to be unique? If not, std.random.uniform() would do the

Re: automate tuple creation

2022-01-19 Thread forkit via Digitalmars-d-learn
On Wednesday, 19 January 2022 at 21:59:15 UTC, forkit wrote: so at the moment i can get a set number of tuples, with a set number of bool values contained within each tuple. e.g. createBoolMatrix(mArrBool,3, 2); [[1, 0], [1, 1], [1, 0]] my next challenge (more for myself, but happy for

Re: automate tuple creation

2022-01-19 Thread Ali Çehreli via Digitalmars-d-learn
On 1/19/22 15:21, H. S. Teoh wrote: > On Wed, Jan 19, 2022 at 02:33:02PM -0800, Ali Çehreli via Digitalmars-d-learn wrote: > [...] >> // Returning a dynamically allocated array looks expensive >> // here. Why not use a struct or std.typecons.Tuple instead? > > Premature optimization. ;-) Not

Re: automate tuple creation

2022-01-19 Thread forkit via Digitalmars-d-learn
On Wednesday, 19 January 2022 at 22:35:58 UTC, Ali Çehreli wrote: so I combined ideas from all responses: // -- module test; import std.stdio : writeln; import std.range : iota, isForwardRange, hasSlicing, hasLength, isInfinite, array; import std.random : Random, unpredictableSeed, dice;

Re: automate tuple creation

2022-01-19 Thread forkit via Digitalmars-d-learn
On Wednesday, 19 January 2022 at 23:22:17 UTC, forkit wrote: oops // e.g: create a matrix consisting of 5 tuples, with each tuple containing 3 random bools (0 or 1) createBoolMatrix(mArrBool,5, 3);

Re: automate tuple creation

2022-01-19 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jan 19, 2022 at 02:33:02PM -0800, Ali Çehreli via Digitalmars-d-learn wrote: [...] > // Returning a dynamically allocated array looks expensive > // here. Why not use a struct or std.typecons.Tuple instead? Premature optimization. ;-) There's nothing wrong with allocating an array. If

Re: automate tuple creation

2022-01-19 Thread Ali Çehreli via Digitalmars-d-learn
On 1/19/22 14:33, Ali Çehreli wrote: > Random rnd; > > shared static this() { >rnd = Random(unpredictableSeed); > } But that's a mistake: If rnd is thread-local like that, it should be initialized in a 'static this' (not 'shared static this'). Otherwise, only the main thread's 'rnd' would

Re: automate tuple creation

2022-01-19 Thread Ali Çehreli via Digitalmars-d-learn
On 1/19/22 13:59, forkit wrote: > void createBoolMatrix(ref uint[][] m) > { > auto rnd = Random(unpredictableSeed); That works but would be unnecessarily slow and be against the idea of random number generators. The usual approach is, once you have a randomized sequence, you just

Re: automate tuple creation

2022-01-19 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jan 19, 2022 at 09:59:15PM +, forkit via Digitalmars-d-learn wrote: > so I have this code below, that creates an array of tuples. > > but instead of hardcoding 5 tuples (or hardcoding any amount of > tuples), what I really want to do is automate the creation of > how-ever-many tuples

Re: automate tuple creation

2022-01-19 Thread forkit via Digitalmars-d-learn
On Wednesday, 19 January 2022 at 21:59:15 UTC, forkit wrote: oh. that randomShuffle was unnecessary ;-)

automate tuple creation

2022-01-19 Thread forkit via Digitalmars-d-learn
so I have this code below, that creates an array of tuples. but instead of hardcoding 5 tuples (or hardcoding any amount of tuples), what I really want to do is automate the creation of how-ever-many tuples I ask for: i.e. instead of calling this: createBoolMatrix(mArrBool); I would call