Re: byChunk odd behavior?

2016-03-26 Thread Hanh via Digitalmars-d-learn
On Saturday, 26 March 2016 at 08:34:04 UTC, cym13 wrote: Sorry, it seems I completely misunderstood you goal. I thought that take() consumed its input (which mostly only shows that I really am careful about not reusing ranges). Writting a take that consume shouldn't be difficult though:

Re: byChunk odd behavior?

2016-03-26 Thread cym13 via Digitalmars-d-learn
On Saturday, 26 March 2016 at 02:28:53 UTC, Hanh wrote: On Friday, 25 March 2016 at 08:01:04 UTC, cym13 wrote: // This consume auto buffer3 = range.take(4).array; assert(buffer3 == [0, 5, 10, 15]); } Thanks for your help. However the last statement is incorrect. I

Re: byChunk odd behavior?

2016-03-25 Thread Hanh via Digitalmars-d-learn
On Friday, 25 March 2016 at 08:01:04 UTC, cym13 wrote: // This consume auto buffer3 = range.take(4).array; assert(buffer3 == [0, 5, 10, 15]); } Thanks for your help. However the last statement is incorrect. I am in fact looking for a version of 'take' that consumes

Re: byChunk odd behavior?

2016-03-25 Thread cym13 via Digitalmars-d-learn
On Thursday, 24 March 2016 at 07:52:27 UTC, Hanh wrote: On Wednesday, 23 March 2016 at 19:07:34 UTC, cym13 wrote: In Scala, 'take' consumes bytes from the iterator. So the same code would be buffer = range.take(N).toArray Then just do that! import std.range, std.array; auto buffer

Re: byChunk odd behavior?

2016-03-24 Thread Hanh via Digitalmars-d-learn
On Wednesday, 23 March 2016 at 19:07:34 UTC, cym13 wrote: In Scala, 'take' consumes bytes from the iterator. So the same code would be buffer = range.take(N).toArray Then just do that! import std.range, std.array; auto buffer = range.take(N).array; auto example = iota(0, 200,

Re: byChunk odd behavior?

2016-03-23 Thread cym13 via Digitalmars-d-learn
On Wednesday, 23 March 2016 at 03:17:05 UTC, Hanh wrote: Thanks for your help everyone. I agree that the issue is due to the misusage of an InputRange but what is the semantics of 'take' when applied to an InputRange? It seems that calling it invalidates the range; in which case what is the

Re: byChunk odd behavior?

2016-03-23 Thread Chris Wright via Digitalmars-d-learn
On Wed, 23 Mar 2016 03:17:05 +, Hanh wrote: > In Scala, 'take' consumes bytes from the iterator. So the same code > would be buffer = range.take(N).toArray import std.range, std.array; auto bytes = byteRange.takeExactly(N).array; There's also take(N), but if the range contains fewer than N

Re: byChunk odd behavior?

2016-03-22 Thread Hanh via Digitalmars-d-learn
Thanks for your help everyone. I agree that the issue is due to the misusage of an InputRange but what is the semantics of 'take' when applied to an InputRange? It seems that calling it invalidates the range; in which case what is the recommended way to get a few bytes and keep on advancing.

Re: byChunk odd behavior?

2016-03-22 Thread cy via Digitalmars-d-learn
On Tuesday, 22 March 2016 at 07:17:41 UTC, Hanh wrote: input.take(3).array; foreach (char c; input) { Never use an input range twice. So, here's how to use it twice: If it's a "forward range" you can use save() to get a copy to use later (but all the std.stdio.* ranges don't

Re: byChunk odd behavior?

2016-03-22 Thread Ali Çehreli via Digitalmars-d-learn
On 03/22/2016 12:17 AM, Hanh wrote: > Hi all, > > I'm trying to process a rather large file as an InputRange and run into > something strange with byChunk / take. > > void test() { > auto file = new File("test.txt"); > auto input = file.byChunk(2).joiner; > input.take(3).array; >

Re: byChunk odd behavior?

2016-03-22 Thread Taylor Hillegeist via Digitalmars-d-learn
On Tuesday, 22 March 2016 at 07:17:41 UTC, Hanh wrote: Hi all, I'm trying to process a rather large file as an InputRange and run into something strange with byChunk / take. void test() { auto file = new File("test.txt"); auto input = file.byChunk(2).joiner;

Re: byChunk odd behavior?

2016-03-22 Thread Hanh via Digitalmars-d-learn
On Tuesday, 22 March 2016 at 07:17:41 UTC, Hanh wrote: Hi all, I'm trying to process a rather large file as an InputRange and run into something strange with byChunk / take. void test() { auto file = new File("test.txt"); auto input = file.byChunk(2).joiner;

byChunk odd behavior?

2016-03-22 Thread Hanh via Digitalmars-d-learn
Hi all, I'm trying to process a rather large file as an InputRange and run into something strange with byChunk / take. void test() { auto file = new File("test.txt"); auto input = file.byChunk(2).joiner; input.take(3).array; foreach (char c; input) {