www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there such concept of a list in D?

reply thebluepandabear <therealbluepandabear protonmail.com> writes:
In most languages there is some sort of `List<T>` type, is that 
the same for D?
Dec 09 2022
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/10/22 12:46 AM, thebluepandabear wrote:
 In most languages there is some sort of `List<T>` type, is that the same 
 for D?
D doesn't focus on interfaces, we have concepts, like ranges. Sorry, it's hard to answer your question without asking more questions: are you looking for a linked list? A list API? A specific list interface? -Steve
Dec 09 2022
parent reply thebluepandabear <therealbluepandabear protonmail.com> writes:
On Saturday, 10 December 2022 at 05:54:09 UTC, Steven 
Schveighoffer wrote:
 On 12/10/22 12:46 AM, thebluepandabear wrote:
 In most languages there is some sort of `List<T>` type, is 
 that the same for D?
D doesn't focus on interfaces, we have concepts, like ranges. Sorry, it's hard to answer your question without asking more questions: are you looking for a linked list? A list API? A specific list interface? -Steve
I was wondering more if there is an object oriented way of creating arrays, like in Java there is an `ArrayList`, in C++ there is `std::vector`, etc.
Dec 09 2022
next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/10/22 1:11 AM, thebluepandabear wrote:
 
 I was wondering more if there is an object oriented way of creating 
 arrays, like in Java there is an `ArrayList`, in C++ there is 
 `std::vector`, etc.
In D, you just use `T[]` for an array, it's similar to `std::vector<T>`. -Steve
Dec 09 2022
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/9/22 22:11, thebluepandabear wrote:

 I was wondering more if there is an object oriented way of creating
 arrays,
Every collection has its own special interface. Object orientation don't go well with collections. For example, you wouldn't want indexing operator for a linked list.
 like in Java there is an `ArrayList`, in C++ there is
 `std::vector`, etc.
They are all dynamic arrays behind the scenes. Arrays are so much better than linked lists that linked lists don't have much use outside of interviews. - Arrays use less memory - Provide constant time element access - Amortized constant time element addition - Constant time element removal in some cases (move the last element in place of the removed one) - Ability to sort to find elements in logN time later on - Arrays get special help from the CPU (e.g. cache prefetches) There isn't a single point in favor of linked lists. Ali
Dec 10 2022
next sibling parent reply areYouSureAboutThat <areYouSureAboutThat gmail.com> writes:
On Saturday, 10 December 2022 at 15:59:07 UTC, Ali Çehreli wrote:
 ... Object orientation don't go well with collections....
On what basis do you make that assertion? i.e. Which aspect of OOP programming 'don't go well with collections'? Is it encapsulation? Is it inheritance? Is it polymorphism? Is it generics? (which is an integral part of OOP these days) As evidence against your assertion (just to start with): Only when you provide related proofs, can you come up with such an axiom.
Dec 13 2022
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/13/22 14:21, areYouSureAboutThat wrote:
 On Saturday, 10 December 2022 at 15:59:07 UTC, Ali Çehreli wrote:
 ... Object orientation don't go well with collections....
On what basis do you make that assertion?
You stripped my answer.
 i.e. Which aspect of OOP programming 'don't go well with collections'?

 Is it encapsulation?

 Is it inheritance?

 Is it polymorphism?

 Is it generics? (which is an integral part of OOP these days)
I am impressed with the questions. Waiting for your answers...
 As evidence against your assertion (just to start with):




 Only when you provide related proofs, can you come up with such an axiom.
Or I can tell what I think, you counter, and we all learn. Proofs... Axioms... Pfft... Ali P.S. I apologize for everything I've done to you in the past. Don't forget: You can always forkit!
Dec 13 2022
parent areYouSureAboutThat <areYouSureAboutThat gmail.com> writes:
On Tuesday, 13 December 2022 at 22:33:02 UTC, Ali Çehreli wrote:
 Or I can tell what I think, you counter, and we all learn. 
 Proofs... Axioms... Pfft...

 Ali
"I suppose sir, you are going to explain your puzzling remarks."
Dec 13 2022
prev sibling parent reply IGotD- <nise nise.com> writes:
On Saturday, 10 December 2022 at 15:59:07 UTC, Ali Çehreli wrote:
 There isn't a single point in favor of linked lists.
Yes there is, there are still special cases where linked lists can be a better alternative. Especially a version with intrusive members (with next/prev pointers as members in your object) The intrusive linked list doesn't need any extra allocation apart from the object itself which means less fragmentation and small container allocations. The double linked list has O(1) insert and delete, arrays has not. The single linked list offer completely lockless variants, which is also completely unbounded. The intrusive linked list has better performance with everything, except random access. You can move/splice entire lists without copying. The linked list performs equally well regardless of number of objects or object size. The performance of arrays depend on this. As CPUs has progressed the array has become more favorable than the linked list type that is being offered by most standard libraries (the one that must allocate container objects, not intrusive). For most programming practices the array is usually the best. However, there are occasions where the linked list can be worth to be considered.
Dec 19 2022
parent reply thebluepandabear <therealbluepandabear protonmail.com> writes:
Why did my replies here to someone else get deleted?
Dec 19 2022
parent reply thebluepandabear <therealbluepandabear protonmail.com> writes:
On Monday, 19 December 2022 at 21:41:45 UTC, thebluepandabear 
wrote:
 Why did my replies here to someone else get deleted?
Myself and this other person's reply to this thread randomly got removed for no reason, I would appreciate an explanation 😕
Dec 19 2022
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/19/22 13:45, thebluepandabear wrote:
 On Monday, 19 December 2022 at 21:41:45 UTC, thebluepandabear wrote:
 Why did my replies here to someone else get deleted?
Myself and this other person's reply to this thread randomly got removed for no reason, I would appreciate an explanation 😕
Are you sure it was this thread? What were in those posts? Perhaps they were posted on another thread? I follow these newsgroups with ThunderBird, which naturally keeps local copies of the posts. I've just gone through all posts in this thread and I see no difference between ThunderBird's cache and the forum interface's cache. Note that ThunderBird does not delete any post even if a moderator removes a posting from the newsgroup. For example, when a spam gets posted, my ThunderBird will show the post even after it's been deleted from the newsgroup server. Ali
Dec 19 2022
parent reply thebluepandabear <therealbluepandabear protonmail.com> writes:
On Monday, 19 December 2022 at 22:07:15 UTC, Ali Çehreli wrote:
 On 12/19/22 13:45, thebluepandabear wrote:
 On Monday, 19 December 2022 at 21:41:45 UTC, thebluepandabear
wrote:
 Why did my replies here to someone else get deleted?
Myself and this other person's reply to this thread randomly
got removed
 for no reason, I would appreciate an explanation 😕
Are you sure it was this thread? What were in those posts? Perhaps they were posted on another thread? I follow these newsgroups with ThunderBird, which naturally keeps local copies of the posts. I've just gone through all posts in this thread and I see no difference between ThunderBird's cache and the forum interface's cache. Note that ThunderBird does not delete any post even if a moderator removes a posting from the newsgroup. For example, when a spam gets posted, my ThunderBird will show the post even after it's been deleted from the newsgroup server. Ali
Yeah I am sure it was on this thread. One of the posts was at https://forum.dlang.org/post/kzvnajixjdnlcupslwjb forum.dlang.org, it now shows 'Not Found'. I just replied to this person who was asking whether or not I was talking about C's `union` type (I was not), and his post and my reply seems to have magically disappeared.
Dec 19 2022
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/19/22 14:14, thebluepandabear wrote:

 Yeah I am sure it was on this thread. One of the posts was at
 https://forum.dlang.org/post/kzvnajixjdnlcupslwjb forum.dlang.org, it
 now shows 'Not Found'.
Then I don't know. (?) However, I realize my ThunderBird "evidence" is useless because if the disapparance happened before my ThunderBird connected since its last time, it wouldn't have a copy of the posts. (My ThunderBird does not connect automatically especially when the laptop lid is closed. :) ) Ali
Dec 19 2022
parent reply thebluepandabear <therealbluepandabear protonmail.com> writes:
No worries, hopefully a mod will explain why. I don't like when 
posts get removed for no reason :|
Dec 19 2022
next sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Monday, 19 December 2022 at 22:22:11 UTC, thebluepandabear 
wrote:
 No worries, hopefully a mod will explain why. I don't like when 
 posts get removed for no reason :|
AI surround us in forum. People want to video chat now. In the time we live in, you can't be sure what to trust! SDB 79
Dec 19 2022
prev sibling parent Mike Parker <aldacron gmail.com> writes:
On Monday, 19 December 2022 at 22:22:11 UTC, thebluepandabear 
wrote:
 No worries, hopefully a mod will explain why. I don't like when 
 posts get removed for no reason :|
I received a report of a possible troll in the forums. Looking at the posts collectively, I agreed, so deleted all of them. Whenever we delete a post, we delete subsequent posts that quote them as well. Please take any further discussion on moderation policies to a new thread in the General forum and let's take this thread back on topic. Thanks!
Dec 19 2022
prev sibling parent reply areYouSureAboutThat <areYouSureAboutThat gmail.com> writes:
On Saturday, 10 December 2022 at 06:11:18 UTC, thebluepandabear 
wrote:
 I was wondering more if there is an object oriented way of 
 creating arrays, like in Java there is an `ArrayList`, in C++ 
 there is `std::vector`, etc.
of course there is - I mean just imagine if there wasn't ;-) e.g (an note, it's called unsafeVector for a reason ;-) // ---- module test; import std; void main() { unsafeVector v = new unsafeVector(5); writefln("size of v is: %s", v.size()); writeln; writeln("v contains:"); for (int i = 0; i < v.size(); i++) { v[i] = i+1; writefln("v %s : %s", i , v[i]); } writeln; writeln(v[0..3]); writeln(v[0..$]); writeln(v[$-1]); } unittest { unsafeVector v = new unsafeVector(5); assert(v.size() == 5); // NOTE: be sure to use the interface here // as D lets you shoot yourself in the foot // by giving you direct access to sz !!! for (int i = 0; i < v.size(); i++) { v[i] = i+1; } assert(v[0..3] == [1,2,3] ); assert(v[0..$] == [1,2,3,4,5]); assert(v[$-1] == 5); v[2] = 999; assert(v[2] == 999); } class unsafeVector { private: size_t *elem; immutable size_t sz; public: this(size_t s) { assert( isIntegral!(typeof(s)) ); elem = cast(size_t*)new size_t[s]; sz = s; } auto ref opIndex(size_t i) { return elem[i]; } auto ref opSlice(size_t start, size_t end) { return elem[start .. end]; } auto ref opDollar() { return sz; } auto size() { return sz; } } // ------------
Dec 13 2022
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Tuesday, 13 December 2022 at 22:51:13 UTC, areYouSureAboutThat 
wrote:
 On Saturday, 10 December 2022 at 06:11:18 UTC, thebluepandabear 
 wrote:
 I was wondering more if there is an object oriented way of 
 creating arrays, like in Java there is an `ArrayList`, in C++ 
 there is `std::vector`, etc.
of course there is - I mean just imagine if there wasn't ;-) e.g (an note, it's called unsafeVector for a reason ;-)
We have nothing to do with unsafe stuff! Why do we waste time with unsafe things; To learn or to teach? SDB 79
Dec 13 2022
next sibling parent areYouSureAboutThat <areYouSureAboutThat gmail.com> writes:
On Tuesday, 13 December 2022 at 23:34:45 UTC, Salih Dincer wrote:
 We have nothing to do with unsafe stuff! Why do we waste time 
 with unsafe things; To learn or to teach?

 SDB 79
Really? That example I provided was just to demonstrate that 'yes' you can implement a container using OOP. I mean why couldn't you? I just used some basic encapsulation here. It's pretty easy to make it 'safe'. But if you want me to provide such an example, utilising all the tools of OOP, you need to first pay me ;-)
Dec 13 2022
prev sibling next sibling parent areYouSureAboutThat <areYouSureAboutThat gmail.com> writes:
On Tuesday, 13 December 2022 at 23:34:45 UTC, Salih Dincer wrote:
 We have nothing to do with unsafe stuff! Why do we waste time 
 with unsafe things; To learn or to teach?

 SDB 79
btw. I reject your axiom: 'We have nothing to do with unsafe stuff!' It is demonstratably not correct, given the need for safe.
Dec 13 2022
prev sibling parent areYouSureAboutThat <areYouSureAboutThat gmail.com> writes:
On Tuesday, 13 December 2022 at 23:34:45 UTC, Salih Dincer wrote:
 We have nothing to do with unsafe stuff! Why do we waste time 
 with unsafe things; To learn or to teach?

 SDB 79
trusted class unsafeVector ... now it's safe ;-)
Dec 13 2022
prev sibling next sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Saturday, 10 December 2022 at 05:46:26 UTC, thebluepandabear 
wrote:
 In most languages there is some sort of `List<T>` type, is that 
 the same for D?
The standard library has many possibilities, including linked lists. What about the news from the range... In D, the concept of range is very advanced. Now I'm going to show you 3 examples and two of them are from the standard library and the other is a nice wrap: ```d struct List(A) { A[] *arr; auto put(R)(R value) { (*arr) ~= value; } auto length() { return (*arr).length; } auto empty() { import std.range : empty; return (*arr).empty; } auto front() {    import std.range : item = front;//back;    return (*arr).item; } void popFront() {    import std.range : next = popFront;//popBack   (*arr).next; } } auto listHelper(A)(return ref A[] arr) { return List!A(&arr); } alias immutable(char[]) [] strings; void main() { strings myNames = ["El-Mevla", "Hodja", "Nasreddin", "Sivrihisar", "Shemseddin", "Nasruddin", "Nusrat"]; strings list; auto myList1 = listHelper(list); import std.range; auto myList2 = appender!strings; import std.container.array; auto myList3 = Array!string(); foreach(name; myNames) { myList1.put(name); myList2.put(name); myList3.insert(name); } void rangePrint(R)(R range) { import std.stdio; size_t i = 1; foreach(item; range) { writeln(i++, ": ", item); } writeln; } rangePrint(myList1); rangePrint(myList2); rangePrint(myList3); } ``` SDB 79
Dec 10 2022
parent reply zjh <fqbqrr 163.com> writes:
On Saturday, 10 December 2022 at 19:49:23 UTC, Salih Dincer wrote:
 SDB 79
Can the `range` be partially traversed? That is, suppose we only access the `[3,5)` elements?
Dec 10 2022
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Sunday, 11 December 2022 at 03:13:17 UTC, zjh wrote:
 On Saturday, 10 December 2022 at 19:49:23 UTC, Salih Dincer 
 wrote:
 SDB 79
Can the `range` be partially traversed? That is, suppose we only access the `[3,5)` elements?
Certainly, there are many ways to do this. For example: ```d //... myList1.skipRange(3).rangePrint; /* 1: Sivrihisar 2: Shemseddin 3: Nasruddin 4: Nusrat */ myNames[3..5].rangePrint; /* 1: Sivrihisar 2: Shemseddin */ } auto skipRange(R)(R range, size_t value) { size_t loop = value; while(!range.empty && loop--) { range.popFront(); } return range; } void rangePrint(R)(R range) { import std.stdio; size_t i = 1; foreach(item; range) { writeln(i++, ": ", item); } writeln; } ``` SDB 79
Dec 10 2022
parent zjh <fqbqrr 163.com> writes:
On Sunday, 11 December 2022 at 07:47:35 UTC, Salih Dincer wrote:

 ..
Thank you for your reply. I think if you take `random` values frequently, you'd better use `'array'`,am I right?
Dec 11 2022
prev sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Saturday, 10 December 2022 at 05:46:26 UTC, thebluepandabear 
wrote:
 In most languages there is some sort of `List<T>` type, is that 
 the same for D?
There is: https://dlang.org/phobos/std_container_dlist.html Why is it called ``DList`` and not just ``List``, i have no clue
Dec 11 2022
next sibling parent reply TTK Ciar <ttk ciar.org> writes:
On Sunday, 11 December 2022 at 17:45:20 UTC, ryuukk_ wrote:
 Why is it called ``DList`` and not just ``List``, i have no clue
Probably because it is a *D*ouble-linked List :-)
Dec 11 2022
parent ryuukk_ <ryuukk.dev gmail.com> writes:
On Monday, 12 December 2022 at 07:57:28 UTC, TTK Ciar wrote:
 On Sunday, 11 December 2022 at 17:45:20 UTC, ryuukk_ wrote:
 Why is it called ``DList`` and not just ``List``, i have no 
 clue
Probably because it is a *D*ouble-linked List :-)
oh right, thanks, i never used that module before i should have read its content before asking lol
Dec 13 2022
prev sibling parent Gregor =?UTF-8?B?TcO8Y2ts?= <gregormueckl gmx.de> writes:
On Sunday, 11 December 2022 at 17:45:20 UTC, ryuukk_ wrote:
 There is: https://dlang.org/phobos/std_container_dlist.html

 Why is it called ``DList`` and not just ``List``, i have no clue
The D is to indicate that it is a doubly-linked list. It maintains a forward and backward pointer chain to support iteration in both directions. This is in contrast to SList (std.container.slist) that only has a forward pointer chain.
Dec 12 2022