[OT] What are D's values?

Ali Çehreli acehreli at yahoo.com
Tue Oct 5 03:43:53 UTC 2021


On 10/4/21 5:11 PM, Paul Backus wrote:

 > One place DbI is used a lot is in `std.range`.

Even before hearing the term Design by Introspection, I was showing 
Phobos algorithms to explain 'static if's usefulness when I was 
presenting D to mostly C++ programmers.

We imagine iota, map, etc. algorithms returning result types 
implementing the InputRange interface. And it makes sense: iota 
generates elements in order, map produces results in order, etc.

However, because 'iota' is capable of implementing opIndex, map can do 
so as well. So, the whole range object is usable as a RandomAccessRange:

import std.range;
import std.algorithm;

void main() {
   auto r = 10.iota.map!(i => i * 2);

   if (r[$/2] > r[0]) {  // This works!
     // ...
   }
}

Ali

P.S. Yes, iota provides opDollar as well, and so can map, etc.



More information about the Digitalmars-d mailing list