Re: Detecting performance pitfall in array access

2020-05-18 Thread Adnan via Digitalmars-d-learn
On Sunday, 17 May 2020 at 09:41:55 UTC, Johan wrote: On Sunday, 17 May 2020 at 03:30:57 UTC, Adnan wrote: Hello, I am trying to examine what causes my similar D solution to lag behind performance. In the link, they don't have ldc or gdc but according to my machine, the dmd generated code

Re: Detecting performance pitfall in array access

2020-05-18 Thread Adnan via Digitalmars-d-learn
On Sunday, 17 May 2020 at 11:39:30 UTC, kinke wrote: As a side note, using jagged arrays for multiple dimensions should probably be avoided whenever you can. By jagged array, do you mean vector of vectors? What would be an alternative?

Detecting performance pitfall in array access

2020-05-16 Thread Adnan via Digitalmars-d-learn
Hello, I am trying to examine what causes my similar D solution to lag behind performance. In the link, they don't have ldc or gdc but according to my machine, the dmd generated code isn't really far behind ldc generated code. So here is the actual code: ulong levenshteinEditDistance(T)(in

Why does indexing a string inside of a recursive call yield a different result?

2020-05-10 Thread Adnan via Digitalmars-d-learn
In my naive implementation of edit-distance finder, I have to check whether the last characters of two strings match: ulong editDistance(const string a, const string b) { if (a.length == 0) return b.length; if (b.length == 0) return a.length; const auto delt =

Re: Adding item to a rbtree with a custom binaryFun

2020-04-13 Thread Adnan via Digitalmars-d-learn
On Monday, 13 April 2020 at 23:59:20 UTC, Adnan wrote: I want to keep an ordered set of records and the standard provides me with RedBlackTree. The record is of type Tuple!(string, uint). Here's what it looks like: import std.json : parseJSON; uint[string] wordTable; import

Adding item to a rbtree with a custom binaryFun

2020-04-13 Thread Adnan via Digitalmars-d-learn
I want to keep an ordered set of records and the standard provides me with RedBlackTree. The record is of type Tuple!(string, uint). Here's what it looks like: import std.json : parseJSON; uint[string] wordTable; import std.datetime.stopwatch : StopWatch, AutoStart;

Github vs Bugzilla for submitting a PR/issue.

2020-04-13 Thread Adnan via Digitalmars-d-learn
I'm a bit confused about D's development process. I've seen people discussing DIPs in Github. I've also seen people discuss internal issues in bugzilla. How do these to correlate? I'm not very familiar with bugzilla, last time I reported an issue about a documentation in the std, I was

Re: Github vs Bugzilla for submitting a PR/issue.

2020-04-13 Thread Adnan via Digitalmars-d-learn
On Monday, 13 April 2020 at 21:31:49 UTC, Adnan wrote: I'm a bit confused about D's development process. I've seen people discussing DIPs in Github. I've also seen people discuss internal issues in bugzilla. How do these to correlate? I'm not very familiar with bugzilla, last time I reported

Re: How to detect whethere if a JSON node exists

2020-04-12 Thread Adnan via Digitalmars-d-learn
On Monday, 13 April 2020 at 02:22:33 UTC, Adam D. Ruppe wrote: On Monday, 13 April 2020 at 02:20:39 UTC, Adnan wrote: Now in the above inner loop getStr(node["com"].str()) crashes in runtime if an array does not contain "com" node. I want to avoid that. How should I proceed? Try: if("com"

How to detect whethere if a JSON node exists

2020-04-12 Thread Adnan via Digitalmars-d-learn
In the following code, I want to process an json array (returned by the value of "posts") that might or might not have "com" key. If a "com" key does not exist, I want to ignore that item in the json array. uint[string] wordTable; const auto j = parseJSON(get(link));

Re: How does one read file line by line / upto a specific delimeter of an MmFile?

2020-03-16 Thread Adnan via Digitalmars-d-learn
On Monday, 16 March 2020 at 13:09:08 UTC, Adnan wrote: On Sunday, 15 March 2020 at 00:37:35 UTC, H. S. Teoh wrote: On Sat, Mar 14, 2020 at 10:37:37PM +, Adnan via Digitalmars-d-learn wrote: https://dlang.org/library/std/mmfile/mm_file.html doesn't seem to specify anything similar to lines

Re: How does one read file line by line / upto a specific delimeter of an MmFile?

2020-03-16 Thread Adnan via Digitalmars-d-learn
On Sunday, 15 March 2020 at 00:37:35 UTC, H. S. Teoh wrote: On Sat, Mar 14, 2020 at 10:37:37PM +, Adnan via Digitalmars-d-learn wrote: https://dlang.org/library/std/mmfile/mm_file.html doesn't seem to specify anything similar to lines() or byLine() or byLineCopy() etc. That's because

Re: Why can't the compiler properly detect the type of Array!string("something")?

2020-03-14 Thread Adnan via Digitalmars-d-learn
On Sunday, 15 March 2020 at 00:04:09 UTC, Adnan wrote: On Saturday, 14 March 2020 at 23:54:44 UTC, Adam D. Ruppe wrote: On Saturday, 14 March 2020 at 23:39:11 UTC, Adnan wrote: Full code this worked for me when i copy/pasted it... are you sure that has the error? if so what compiler version

Re: Why can't the compiler properly detect the type of Array!string("something")?

2020-03-14 Thread Adnan via Digitalmars-d-learn
On Saturday, 14 March 2020 at 23:54:44 UTC, Adam D. Ruppe wrote: On Saturday, 14 March 2020 at 23:39:11 UTC, Adnan wrote: Full code this worked for me when i copy/pasted it... are you sure that has the error? if so what compiler version you on? This is indeed very strange, godbolt says

Re: Why can't the compiler properly detect the type of Array!string("something")?

2020-03-14 Thread Adnan via Digitalmars-d-learn
On Saturday, 14 March 2020 at 23:39:11 UTC, Adnan wrote: In the following code the compiler says the type is Array!()(string): if (key in wordTable) wordTable[key] ~= word; else

Why can't the compiler properly detect the type of Array!string("something")?

2020-03-14 Thread Adnan via Digitalmars-d-learn
In the following code the compiler says the type is Array!()(string): if (key in wordTable) wordTable[key] ~= word; else wordTable[key] = Array!string(word); source/app.d(29,36):

How does one read file line by line / upto a specific delimeter of an MmFile?

2020-03-14 Thread Adnan via Digitalmars-d-learn
https://dlang.org/library/std/mmfile/mm_file.html doesn't seem to specify anything similar to lines() or byLine() or byLineCopy() etc.

Associative Array potential performance pitfall?

2020-03-12 Thread Adnan via Digitalmars-d-learn
In my machine the following D code compiled with release flag and LDC performs over 230ms while the similar Go code performs under 120ms. string smallestRepr(const string arg) { import std.format : format; const repeated = format!"%s%s"(arg, arg); string result;

akePureMalloc cannot be interpreted at compile time

2020-03-05 Thread Adnan via Digitalmars-d-learn
The following program produces an error message and it is not clear exactly what line causes this error: module maybe; @nogc: private import std.container : Array; struct MayBe(T) { Array!T data; this(T datum) { data.reserve(1);

Re: Idiomatic way to express errors without resorting to exceptions

2020-02-29 Thread Adnan via Digitalmars-d-learn
On Saturday, 29 February 2020 at 13:03:21 UTC, Sebastiaan Koppe wrote: On Saturday, 29 February 2020 at 12:50:59 UTC, Adnan wrote: * Option!T from the optional package: Has even worse problem IMO. Not only it allows None + int but also it returns a `[]`. This API is not to my liking. You could

Idiomatic way to express errors without resorting to exceptions

2020-02-29 Thread Adnan via Digitalmars-d-learn
I have a struct that has to arrays. Each of those must have the same sizes. So while constructing the array, if you pass two arrays of different sizes the constructor must return nothing. In Rust I could easily use Option. D has no answer to Optional types as far as I am concerned. Is

What's the dub command to create a library to be published in dub.pm?

2020-02-23 Thread Adnan via Digitalmars-d-learn
In Rust they have $cargo new --lib Even if dub doesn't have this, how can I change dub.json to build it as a library and not look for the main function?

Alternative to friend functions?

2020-02-18 Thread Adnan via Digitalmars-d-learn
What is the alternative to C++'s friend functions in D? module stable_matching; alias FemaleID = int; alias MaleID = int; class Person { string name; int id; } class Male : Person { this(string name = "Unnamed Male") { static int nextID = 0; this.id = nextID++;

How to declare a virtual member (not a function) in a class

2020-02-18 Thread Adnan via Digitalmars-d-learn
I have a base class that has a couple of constant member variables. These variables are abstract, they will only get defined when the derived class gets constructed. class Person { const string name; const int id; } class Male : Person { this(string name = "Unnamed Male") {

Re: Why can't I pass a const array to a function that takes scope const arrays?

2020-02-17 Thread Adnan via Digitalmars-d-learn
On Monday, 17 February 2020 at 14:34:44 UTC, Simen Kjærås wrote: On Monday, 17 February 2020 at 14:04:34 UTC, Adnan wrote: // All in all, I end up with this code: module strassens_matmul package { T[][] mulIterative(T)(const T[][] mat1, const T[][] mat2) { auto result =

Re: Why can't I pass a const array to a function that takes scope const arrays?

2020-02-17 Thread Adnan via Digitalmars-d-learn
On Monday, 17 February 2020 at 14:04:34 UTC, Adnan wrote: On Monday, 17 February 2020 at 13:44:55 UTC, Adnan wrote: [...] Okay I changed to module strassens_matmul; [...] I changed getPointPtr to following and now it works /// row and column are 0 index-based void assign(T)(ref

Re: Why can't I pass a const array to a function that takes scope const arrays?

2020-02-17 Thread Adnan via Digitalmars-d-learn
On Monday, 17 February 2020 at 13:44:55 UTC, Adnan wrote: https://ideone.com/lVi5Uy module strassens_matmul; debug { static import std; } ... Okay I changed to module strassens_matmul; debug { static import std; } package { ulong getRowSize(T)(scope const T[][] mat) {

Why can't I pass a const array to a function that takes scope const arrays?

2020-02-17 Thread Adnan via Digitalmars-d-learn
https://ideone.com/lVi5Uy module strassens_matmul; debug { static import std; } package { ulong getRowSize(T)(T[][] mat) { return mat[0].length; } ulong getColumnSize(T)(T[][] mat) { return mat.length; } T[][] createMatrix(T)(const ulong rowSize, const

Re: How to iterate over range two items at a time

2020-02-17 Thread Adnan via Digitalmars-d-learn
On Monday, 17 February 2020 at 07:50:02 UTC, Mitacha wrote: On Monday, 17 February 2020 at 05:04:02 UTC, Adnan wrote: What is the equivalent of Rust's chunks_exact()[1] method in D? I want to iterate over a spitted string two chunks at a time. [1]

How to iterate over range two items at a time

2020-02-16 Thread Adnan via Digitalmars-d-learn
What is the equivalent of Rust's chunks_exact()[1] method in D? I want to iterate over a spitted string two chunks at a time. [1] https://doc.rust-lang.org/beta/std/primitive.slice.html#method.chunks_exact

Overfflow in Assert error messages

2020-02-12 Thread Adnan via Digitalmars-d-learn
I am debugging my simple binary search (I still am): module binary_search; debug { static import std; } int indexOf(T)(const T[] list, const T key) { ulong lo = 0; ulong hi = list.length - 1; while (hi > lo) { const ulong mid = lo + (hi - lo) / 2; if (list[mid]

writeln() in static import std

2020-02-12 Thread Adnan via Digitalmars-d-learn
How can I reach stdout.writeln() using fully qualified name with static import? I have tried: std.stdio.stdout.writeln() -- fails std.writeln() -- works std.stdout.writeln -- works How does static import with std work?

Re: Printing LHS and RHS of assert expressions on failure

2020-02-11 Thread Adnan via Digitalmars-d-learn
On Tuesday, 11 February 2020 at 13:45:24 UTC, kinke wrote: On Tuesday, 11 February 2020 at 13:38:32 UTC, Adnan wrote: I just want to know is there any de-facto way of achieving this? See the `-checkaction=context` switch. Exactly what I was after. Thanks.

Printing LHS and RHS of assert expressions on failure

2020-02-11 Thread Adnan via Digitalmars-d-learn
Hi, is there any trick to print the RHS and the LHS of the assert expressions when it fails? like `assert(2 == 5)` should fail and print something like: ... assert failed [__LINE__/__MODULE__]: Left hand side: 2 is 2, Right hand side: 5 is 5 Of course, I can design a function to do so

How to refer to different sized static arrays

2020-02-08 Thread Adnan via Digitalmars-d-learn
Just a foreword, this is for learning purposes, hence I am not using the dynamic array or Array!T. I have a structure that maintains a heap allocated sized array inside. struct LifoStack(T) { T[?] data; } This `data` is manually resized and copied. Thus the size itself is not a compile

Cannot implicitly convert expression [[0, -1, 2], [4, 11, 2]] of type int[][] to const(int[2])[]

2020-01-31 Thread Adnan via Digitalmars-d-learn
https://wiki.dlang.org/Dense_multidimensional_arrays#Static_arrays describes a way to create static arrays: int[3][3] matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]; However my complains that I can't implicitly create static arrays from dynamic arrays. private T[R1][C2]

How to parse epub content

2020-01-11 Thread Adnan via Digitalmars-d-learn
How would someone approach parsing epub files in D? Is there any libraries to parse XHTML?

Mergesort not working

2019-12-29 Thread Adnan via Digitalmars-d-learn
This is not entirely a D question, but I'm not sure what about my mergesort implementation went wrong. T[] merge(T)(T[] arr1, T[] arr2) { T[] result; result.reserve(arr1.length + arr2.length); ulong arr1_idx = 0, arr2_idx = 0; while (arr1_idx < arr1.length && arr2_idx <

`in` parameters optimization

2019-12-24 Thread Adnan via Digitalmars-d-learn
Does the compiler automatically pass values by reference if possible with `in` parameters in higher level of optimization flags? I would normally use `in ref` but sometimes it's not compatible with different types.

Static linking, specifying binary and test-library folder

2019-12-23 Thread Adnan via Digitalmars-d-learn
Hello, how does one: 1. Force static linking (build with `-defaultlib` flag) 2. Specify binary file generated by the `dub buid` command 3. Specify binary file generated by the `dub test` command in the `dub.json` file?

Re: How to do IPC using Unix Domain Socket?

2019-10-10 Thread Hossain Adnan via Digitalmars-d-learn
On Thursday, 10 October 2019 at 12:30:25 UTC, Hossain Adnan wrote: Here I have a program that wants to 1. detect whether if it's the only instance 1.1. it does that by trying to create a Unix Domain Socket and trying to binding it to a specific address. 2. if a duplicate program is not

Re: How to do IPC using Unix Domain Socket?

2019-10-10 Thread Hossain Adnan via Digitalmars-d-learn
On Thursday, 10 October 2019 at 12:30:25 UTC, Hossain Adnan wrote: Here I have a program that wants to 1. detect whether if it's the only instance 1.1. it does that by trying to create a Unix Domain Socket and trying to binding it to a specific address. [...] If it helps explaining

How to do IPC using Unix Domain Socket?

2019-10-10 Thread Hossain Adnan via Digitalmars-d-learn
Here I have a program that wants to 1. detect whether if it's the only instance 1.1. it does that by trying to create a Unix Domain Socket and trying to binding it to a specific address. 2. if a duplicate program is not running, establish an UDS and then listen to the socket.

Re: How to use Dbus to detect application uniqueness in D?

2019-09-28 Thread Hossain Adnan via Digitalmars-d-learn
On Saturday, 28 September 2019 at 13:37:12 UTC, Kagamin wrote: https://ddbus.dpldocs.info/ddbus.bus.requestName.html It requires a Connection type which I cannot find in the API.

Re: Packaging and Distributing Dlang Applications with GtkD Dependency?

2019-09-27 Thread Hossain Adnan via Digitalmars-d-learn
On Wednesday, 25 September 2019 at 11:46:04 UTC, Ron Tarrant wrote: Hi y'all, I've been Googling how to do this, but coming up with nothing definitive. Are there any articles for how to do this for: Linux? For Linux there are 3 new options: 1. Appimages (https://appimage.org/): This is

How to use Dbus to detect application uniqueness in D?

2019-09-27 Thread Hossain Adnan via Digitalmars-d-learn
Hi I need to detect application uniqueness using dbus. I have a working code in Rust: fn run_as_unique_instance() { println!("First instance detected. Doing work..."); loop {} } fn run_as_nonunique_instance() { println!("Another instance is already running. Quiting...");

Code review - disjoint sets

2018-12-07 Thread Adnan via Digitalmars-d-learn
Hello. Is code-review requests welcome in this forum? If so I would like some criticisms and feedback for my disjoint sets implementation. The code is as follows: module dsets; /// dsets is an implementation of disjoint sets. It is implemented /// with a simple class. To construct it, you

Wrapping a Dub project inside Meson

2018-11-21 Thread Adnan via Digitalmars-d-learn
Does anyone have experience with using meson to wrap around a dub project? I have a typical dub project, meaning I have a dub dependency but I want to use meson for two reasons: 1. I want to distribute the application in form of a snap package (https://snapcraft.io/). Snapcraft does not

Re: Can't read a constant value in compile time?

2018-11-20 Thread Adnan via Digitalmars-d-learn
On Tuesday, 20 November 2018 at 18:54:58 UTC, Adnan wrote: Godbolt: https://godbolt.org/z/SWWOu7 When I write `something!(aNumber)()` and if a number is an immutable/enum it should be able to be read at compile time, right? Why is this different? auto fizzbuzz(uint N)() { static string

Can't read a constant value in compile time?

2018-11-20 Thread Adnan via Digitalmars-d-learn
Godbolt: https://godbolt.org/z/SWWOu7 When I write `something!(aNumber)()` and if a number is an immutable/enum it should be able to be read at compile time, right? Why is this different? auto fizzbuzz(uint N)() { static string accumulate; return fizzbuzz!N(accumulate); } auto