Bidirectional range dilemma

bearophile bearophileHUGS at lycos.com
Sun Jan 13 06:01:27 PST 2013


> Peter Alexander:

> especially since I imagine that most of the time the safety is 
> unneeded.

The lazyness of D ranges is useful, but unlike Haskell, in D you 
can't always use lazyness, sometimes you need eagerness, because 
D is strongly array-based.

A newbie programmer, or a new user of the permutations functions 
that has not studied its API, will find permutations() more handy 
and safe if it allocates a new buffer for each permutation.

You remember the discussions about File.byLine that is not 
allocating one array for each line on default, but it yields 
char[] so when you convert it to a string you usually copy the 
buffer. So its unsafety is limited. This is not true for 
permutations().

I have found that in small script-like programs I sometimes 
(about half of the time?) prefer an eager generation of 
combinations or permutations, because it's more handy. You don't 
always need max speed in the code.

Safety on default and speed on request is one of the tenets of D 
language. And it's a good thing.

Not allocating a buffer for each permutation is premature 
optimization.

In past I have had several bugs caused by generators that don't 
allocate a buffer each time. When you fix some of such bugs, you 
learn to appreciate safety on default. So my current generators 
of permutations, combinations, permutations by swapping, 
derangements, lexicographic generator, etc are safe on default. 
It makes my life simpler.

Bye,
bearophile


More information about the Digitalmars-d mailing list