Jump to page: 1 2 3
Thread overview
Is there such concept of a list in D?
Dec 10, 2022
thebluepandabear
Dec 10, 2022
thebluepandabear
Dec 10, 2022
Ali Çehreli
Dec 13, 2022
Ali Çehreli
Dec 19, 2022
IGotD-
Dec 19, 2022
thebluepandabear
Dec 19, 2022
thebluepandabear
Dec 19, 2022
Ali Çehreli
Dec 19, 2022
thebluepandabear
Dec 19, 2022
Ali Çehreli
Dec 19, 2022
thebluepandabear
Dec 20, 2022
Salih Dincer
Dec 20, 2022
Mike Parker
Dec 13, 2022
Salih Dincer
Dec 10, 2022
Salih Dincer
Dec 11, 2022
zjh
Dec 11, 2022
Salih Dincer
Dec 11, 2022
zjh
Dec 11, 2022
ryuukk_
Dec 12, 2022
TTK Ciar
Dec 13, 2022
ryuukk_
Dec 12, 2022
Gregor Mückl
December 10, 2022

In most languages there is some sort of List<T> type, is that the same for D?

December 10, 2022

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

December 10, 2022

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.

December 10, 2022

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

December 10, 2022
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

December 10, 2022

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:

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

December 11, 2022

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?

December 11, 2022

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:

  //...
  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

December 11, 2022

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?

December 11, 2022

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

« First   ‹ Prev
1 2 3