std.array.put doesn't put
Philippe Sigaud
philippe.sigaud at gmail.com
Tue Mar 2 05:14:20 PST 2010
On Mon, Mar 1, 2010 at 22:51, Lars T. Kyllingstad
<public at kyllingen.nospamnet> wrote:
> Paul D. Anderson wrote:
>
from std.range
> "r.put(e) puts e in the range (in a range-dependent manner) and advances
>> to the popFront position in the range. Successive calls to r.put add
>> elements to the range. put may throw to signal failure."
>>
> with std.array.put:
> It modifies a[0] and then replaces the array with the tail of the array,
>> omitting the first element.
>>
>
It seems to me array.put does what the description in std.range entails.
I had the same reaction when reading array.put for the first time. How can a
function putting values in something make them non-accessible at the same
time? But that's because for arrays the container and the associated range
(view) are the same, leading to some confusion. put puts values not in the
range as said in std.range but in the container accessed by the range. The
range itself updates in the only way a range can: by shrinking (so,
popFront), giving you a new place to put a another value.
As Lars said, a classical use would be:
Container c = new Container(values);
auto view =c.asOutputRange(); // view is an output range. Andrei suggested
'.all' as a property returning a range on all values.
foreach(elem; view) view.put(someOtherValues); // writes values in c,
consumes view.
assert(view.empty); // Now view is exhausted and c contains someOtherValues.
>> It's possible there is some arcane meaning to the word "put" that I'm not
>> aware of, but if it means "insert an element at the front of the range" then
>> std.array.put is wrongly implemented.
>>
>>
For some ranges, you can replace the front value:
range.front = someValue;
But then, how would you go putting some more?
You cannot extend a range, I think. You can create a new range chaining new
values and the old range, but that creates a new one.
auto prepended = chain(newValues, oldValues);
I think I have somewhere something inspired by zippers (
http://en.wikipedia.org/wiki/Zipper_%28data_structure%29), where a range
'remembers' previous values (stores them in an array) and you can make the
front retreat, giving access to old values anew. But it cannot modify the
sublying container, only the values stored (artificially) in the range. It's
useful for some algorithms that need to go back and forth on a few
positions.
Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100302/968677db/attachment.html>
More information about the Digitalmars-d
mailing list