January 20, 2023

On Friday, 20 January 2023 at 04:46:07 UTC, Ali Çehreli wrote:

>

Different instantiations of templates are distinct types. For example, if I called 'alternate' with two 'long' values, both alternate!int (as instantiated by the code above) and alternate!long would have different MyResult struct types.

In general, the ranges are compatible with each other because they use the empty, front, popFront interface. In the example below, different types (one of which is double) but the same ranges can be combined with chain(). However, it is necessary to convert it to array because of the opCmp() compatibility from algorithms such as sorting.

  import std.algorithm : sort;
  import std.conv      : to;
  import std.range;
  import std.stdio;


  enum limit = 5;
  enum step = limit / 10.0;/*
  enum step = 1; //*/

void main()
{
  TypeInfo rangeType;	
	
  auto a = iota(limit);
  auto b = iota(step, limit, step);
	
  /* <- toggle comment, please add -> /
  auto ab = chain(a, b);
  rangeType = typeid(ab);/*/
  auto arrA = a.array.to!(double[]);
  auto arrB = b.array;
  auto ab = chain(arrA, arrB);
  rangeType = typeid(ab.sort);//*/
		
  ab.writeln(": ", rangeType);
} /*
current print:
==============
[0, 0.5, 1, 1, 1.5, 2, 2, 2.5, 3, 3, 3.5, 4, 4, 4.5]: std.range.SortedRange!(std.range.chain!(double[], double[]).chain(double[], double[]).Result, "a < b", 0).SortedRange

other print:
============
[0, 1, 2, 3, 4, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]: std.range.chain!(std.range.iota!(int, int).iota(int, int).Result, std.range.iota!(double, int, double).iota(double, int, double).Result).chain(std.range.iota!(int, int).iota(int, int).Result, std.range.iota!(double, int, double).iota(double, int, double).Result).Result

SDB@79

January 20, 2023
On Friday, 20 January 2023 at 03:39:48 UTC, H. S. Teoh wrote:
> On Fri, Jan 20, 2023 at 03:34:43AM +0000, Ruby The Roobster via Digitalmars-d-learn wrote:
>> On Friday, 20 January 2023 at 03:30:56 UTC, Steven Schveighoffer wrote:
>> > On 1/19/23 10:11 PM, Ruby The Roobster wrote:
>> > ...
>> > 
>> > The point is to be a range over the original input, evaluated lazily. Using this building block, you can create an array, or use some other algorithm, or whatever you want. All without allocating more space to hold an array.
> [...]
>> I get the point that it is supposed to be lazy.  But why are these basic cases not implemented?  I shouldn't have to go write a wrapper for something as simple as casting this type to the original type. This is one of the things that one expects the standard library to do for you.
>
> There's no need to write any wrappers.  Just tack `.array` to the end of your pipeline, and you're good to go.
>
>
> T

Thank you.  I didn't know that there was such a property `.array`.
January 20, 2023
On Fri, Jan 20, 2023 at 12:49:54PM +0000, Ruby The Roobster via Digitalmars-d-learn wrote: [...]
> Thank you.  I didn't know that there was such a property `.array`.

It's not a property, it's a Phobos function from std.array.


T

-- 
INTEL = Only half of "intelligence".
1 2
Next ›   Last »