August 07, 2022
On Sunday, 7 August 2022 at 15:34:19 UTC, pascal111 wrote:
> Everyone knows that slices are not pointers that pointers are real work, but slices are like a simple un-deep technique that is appropriate for beginners, but after that in advanced level in programming, we should use pointers to do same tasks we were doing with slices (the easy way of beginners).

I can't tell if this is a joke or not.
August 07, 2022

On Saturday, 6 August 2022 at 15:37:32 UTC, pascal111 wrote:

>

On Friday, 5 August 2022 at 04:05:08 UTC, Salih Dincer wrote:

>

On Thursday, 4 August 2022 at 22:54:42 UTC, pascal111 wrote:

>

I didn't notice that all what we needs to pop a range forward is just a slice, yes, we don't need variable here.

Ranges and Slices are not the same thing. Slicing an array is easy. This is a language possibility. For example, you need an incrementing variable for the Fibonacci Series.

SDB@79

What!!! so where's ranges?! I thought slices of any array are ranges, and understood it like that, and also there's no data type called ranges, it's like if you are talking about Ghostly data type!

A range is like an iterator in any other language (Java, C++, python3, javascript, etc), it is how D implements (lazy) generators https://en.wikipedia.org/wiki/Lazy_evaluation .

Ranges/Iterators don't necessarily have to be backed by memory, they just have to implement the interface. In D, a empty bool function that tells you whether you are at the end of the range or not; a front function to get the current value if the range is not empty; and a void function named popFront to advance to the next value if the range is not empty.

Once you have implemented this interface, you can use your "range" object with any function that accept a range; with foreach; etc.

Example of a range that is not backed by memory is a range with all the integer numbers.

struct Integers {
  private int z = 0;

  /* or make it a bool attribute that starts as false, and you set to
   * true when popFront is called while z is equal to int.min */
  public bool empty() { return false; }

  public int front() { return this.z; }

  public void popFront()
  {
    /* if (this.z == int.min) { this.empty = false; return; } */
    this.z *= -1;
    if (this.z <= 0)
      --this.z;
  }
}

void main()
{
  import std.stdio : writeln;

  /* foreach is syntax sugar for
   *   for (auto r = Integers(); !r.empty(); r.popFront()) {
   *     auto z = r.front(); /+ or  const z = r.front();  or ... +/
   *     ...
   *   }
   * that is why it only works with ranges.
   */
  foreach (const z; Integers()) {
    writeln(z);
    if (z == 5)
      break;
  }
}

output:

0
-1
1
-2
2
-3
3
-4
4
-5
5

This will iterate all the integers, and the integers are of course, not
all in memory, and don't remain in memory after they are used, since
that would require infinite memory. (in the case of a range of integers,
not infinite, because they are constrained by being int.sizeof bytes,
but you could use a bignum implemenation that is not constrained by
that and they would actually be infinite.)


The equivalent in Java is the Iterable/Iterator interface.

import java.util.Iterator;

public class Integers
  implements Iterable<Integer>
{
  public class IntegersIterator
    implements Iterator<Integer>
  {
    private int z = 0;
    private boolean first = true;

    public IntegersIterator(Integer z)
    {
      this.z = z;
    }

    @Override
    public boolean hasNext() { return true; }

    @Override
    public Integer next()
    {
      if (this.first) {
        this.first = false;
        return this.z;
      }

      this.z *= -1;
      if (this.z <= 0)
        --this.z;
      return this.z;
    }
  }

  @Override
  public IntegersIterator iterator() { return new IntegersIterator(0); }

  public static void main(String[] args)
  {
    /* syntax sugar for
     *   {
     *     final var it = newIntegers.iterator();
     *     while (it.hasNext()) {
     *       final int z = it.next();
     *       ...
     *     }
     *   }
     */
    for (final int z : new Integers()) {
      System.out.println(z);
      if (z == 5)
        break;
    }
  }
}

The equivalent in python is a generator function:

def integers():
  z = 0
  yield z
  while True:
    z *= -1
    if z <= 0:
      z -= 1
    yield z

for z in integers():
  print(z)
  if z == 5:
    break

etc

August 07, 2022
On Sunday, 7 August 2022 at 19:53:06 UTC, ag0aep6g wrote:
> On Sunday, 7 August 2022 at 15:34:19 UTC, pascal111 wrote:
>> Everyone knows that slices are not pointers that pointers are real work, but slices are like a simple un-deep technique that is appropriate for beginners, but after that in advanced level in programming, we should use pointers to do same tasks we were doing with slices (the easy way of beginners).
>
> I can't tell if this is a joke or not.

It's just an opinion.
August 07, 2022
On 8/7/22 08:34, pascal111 wrote:

> Everyone knows that slices are not pointers

D's slices are "fat pointers": In D's case, that translates to a pointer plus length.

> that pointers are real work,

Agreed. Pointers are fundamental features of CPUs.

> but slices are like a simple un-deep technique that is appropriate for
> beginners,

That is not correct. Slices are designed by a C expert to prevent horrible bugs caused by C experts. Most C experts use slices very happily.

> but after that in advanced level in programming, we should
> use pointers to do same tasks we were doing with slices (the easy way of
> beginners).

That is an old thought. Today, we see that no matter how experienced, every person needs and appreciates help to prevent bugs. There are many cases of bugs killing people, jeopardizing expensive projects, loss of personal information, etc.

Ali

August 07, 2022
On 8/6/22 22:58, Salih Dincer wrote:

> Ranges are not like that, all they do is
> generate.

You may be right. I've never seen it that way.

I've been under the following impression:

- C++'s iterators are based on an existing concept: pointers. Pointers are iterators.

- D's ranges are based on an existing concept: slices. Slices are ranges.

However, I can't find where I read that.

Ali

August 07, 2022
On Sunday, 7 August 2022 at 21:57:50 UTC, Ali Çehreli wrote:
> On 8/7/22 08:34, pascal111 wrote:
>
> > but after that in advanced level in programming, we should
> > use pointers to do same tasks we were doing with slices (the
> easy way of
> > beginners).
>
> That is an old thought. Today, we see that no matter how experienced, every person needs and appreciates help to prevent bugs. There are many cases of bugs killing people, jeopardizing expensive projects, loss of personal information, etc.
>
> Ali

I think you are right that this is an old thought, I didn't noticed that, maybe it's because I didn't study C++ and know only about C, so I applied C features on D.
1 2 3
Next ›   Last »