std.stream.MemoryStream deprecated, range is the alternative?
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Aug 7 09:00:58 PDT 2015
On Thursday, 6 August 2015 at 17:01:32 UTC, chris wrote:
> since memorystream is deprecated how do i do something like
> this with Input and Output ranges? How can i fill up an array
> with ranges like you can do with streams?
> Thanks.
The InputRange primitives already exist for arrays, they are
located in std.array, as well as the functions to insert
elements. To achieve more advanced mutations use std.algorithm.
---
import std.stdio;
import std.array;
import std.algorithm;
byte B(T)(T t){return cast(byte) t;}
struct FillerDemo
{
private byte cnt;
byte front(){return cnt;}
void popFront(){++cnt;}
@property bool empty(){return cnt == 8;}
}
void main(string[] args)
{
auto rng = [0.B, 2.B, 4.B, 6.B, 8.B, 10.B, 12.B];
// reads then advances, destructively
byte val = rng.front;
writeln(val);
rng.popFront;
// fills with an array
insertInPlace(rng, 0, [-4.B, -2.B, 0.B]);
writeln(rng);
rng = rng.init;
// fills with a compatible range
insertInPlace(rng, 0, *new FillerDemo);
writeln(rng);
// std.algorithm
reverse(rng);
}
---
Note, if you don't know yet, that ranges are consumed. The front
is lost each time popFront() is called.
More information about the Digitalmars-d-learn
mailing list