Jump to page: 1 24  
Page
Thread overview
Ranges, constantly frustrating
Feb 11, 2014
Regan Heath
Feb 11, 2014
Jakob Ovrum
Feb 11, 2014
Regan Heath
Feb 11, 2014
Tobias Pankrath
Feb 11, 2014
Regan Heath
Feb 11, 2014
Steve Teale
Feb 11, 2014
Tobias Pankrath
Feb 11, 2014
Regan Heath
Feb 11, 2014
Tobias Pankrath
Feb 12, 2014
Regan Heath
Feb 12, 2014
Jakob Ovrum
Feb 13, 2014
Regan Heath
Feb 11, 2014
Jesse Phillips
Feb 12, 2014
Regan Heath
Feb 11, 2014
Rene Zwanenburg
Feb 11, 2014
Ali Çehreli
Feb 12, 2014
Regan Heath
Feb 12, 2014
Regan Heath
Feb 11, 2014
Jesse Phillips
Feb 12, 2014
thedeemon
Feb 12, 2014
Regan Heath
Feb 12, 2014
Jesse Phillips
Feb 13, 2014
Regan Heath
Feb 14, 2014
Jesse Phillips
Feb 14, 2014
Regan Heath
Feb 14, 2014
Jakob Ovrum
Feb 14, 2014
Regan Heath
Feb 14, 2014
bearophile
Feb 14, 2014
Regan Heath
Feb 14, 2014
bearophile
Feb 14, 2014
bearophile
Feb 14, 2014
Marc Schütz
Feb 14, 2014
bearophile
Feb 17, 2014
Regan Heath
February 11, 2014
Things like this should "just work"..

File input ...

auto range = input.byLine();
while(!range.empty)
{
  range.popFront();
  foreach (i, line; range.take(4))  //Error: cannot infer argument types
  {
    ..etc..
  }
  range.popFront();
}

Tried adding 'int' and 'char[]' or 'auto' .. no dice.

Can someone explain why this fails, and if this is a permanent or temporary limitation of D/MD.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
February 11, 2014
On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote:
> Things like this should "just work"..
>
> File input ...
>
> auto range = input.byLine();
> while(!range.empty)
> {
>   range.popFront();
>   foreach (i, line; range.take(4))  //Error: cannot infer argument types
>   {
>     ..etc..
>   }
>   range.popFront();
> }
>
> Tried adding 'int' and 'char[]' or 'auto' .. no dice.
>
> Can someone explain why this fails, and if this is a permanent or temporary limitation of D/MD.
>
> R

See this pull request[1] and the linked enhancement report.

Also note that calling `r.popFront()` without checking `r.empty` is a program error (so it's recommended to at least put in an assert).

[1] https://github.com/D-Programming-Language/phobos/pull/1866
February 11, 2014
On Tue, 11 Feb 2014 10:10:27 -0000, Regan Heath <regan@netmail.co.nz> wrote:

> Things like this should "just work"..
>
> File input ...
>
> auto range = input.byLine();
> while(!range.empty)
> {
>    range.popFront();
>    foreach (i, line; range.take(4))  //Error: cannot infer argument types
>    {
>      ..etc..
>    }
>    range.popFront();
> }
>
> Tried adding 'int' and 'char[]' or 'auto' .. no dice.
>
> Can someone explain why this fails, and if this is a permanent or temporary limitation of D/MD.

Further, the naive solution of adding .array gets you in all sorts of trouble :p  (The whole byLine buffer re-use issue).

This should be simple and easy, dare I say it trivial.. or am I just being dense here.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
February 11, 2014
> Further, the naive solution of adding .array gets you in all sorts of trouble :p  (The whole byLine buffer re-use issue).
>
> This should be simple and easy, dare I say it trivial.. or am I just being dense here.
>
> R

The second naive solution would be to use readText and splitLines.
February 11, 2014
On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote:
> Things like this should "just work"..
>
> File input ...
>
> auto range = input.byLine();
> while(!range.empty)
> {
>   range.popFront();
>   foreach (i, line; range.take(4))  //Error: cannot infer argument types
>   {
>     ..etc..
>   }
>   range.popFront();
> }
>
> Tried adding 'int' and 'char[]' or 'auto' .. no dice.
>
> Can someone explain why this fails, and if this is a permanent or temporary limitation of D/MD.
>
> R
Is foreach(i, val; aggregate) even defined if aggr is not an array or associated array? It is not in the docs: http://dlang.org/statement#ForeachStatement
February 11, 2014
On Tue, 11 Feb 2014 10:52:39 -0000, Tobias Pankrath <tobias@pankrath.net> wrote:

>> Further, the naive solution of adding .array gets you in all sorts of trouble :p  (The whole byLine buffer re-use issue).
>>
>> This should be simple and easy, dare I say it trivial.. or am I just being dense here.
>>
>> R
>
> The second naive solution would be to use readText and splitLines.

The file is huge in my case :)

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
February 11, 2014
On Tue, 11 Feb 2014 10:58:17 -0000, Tobias Pankrath <tobias@pankrath.net> wrote:

> On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote:
>> Things like this should "just work"..
>>
>> File input ...
>>
>> auto range = input.byLine();
>> while(!range.empty)
>> {
>>   range.popFront();
>>   foreach (i, line; range.take(4))  //Error: cannot infer argument types
>>   {
>>     ..etc..
>>   }
>>   range.popFront();
>> }
>>
>> Tried adding 'int' and 'char[]' or 'auto' .. no dice.
>>
>> Can someone explain why this fails, and if this is a permanent or temporary limitation of D/MD.
>>
>> R
> Is foreach(i, val; aggregate) even defined if aggr is not an array or associated array? It is not in the docs: http://dlang.org/statement#ForeachStatement

import std.stdio;

struct S1 {
   private int[] elements = [9,8,7];
   int opApply (int delegate (ref uint, ref int) block) {
       foreach (uint i, int n ; this.elements)
           block(i, n);
       return 0;
   }
}

void main()
{
	S1 range;	
	foreach(uint i, int x; range)
	{
	  writefln("%d is %d", i, x);
	}
}

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
February 11, 2014
On Tuesday, 11 February 2014 at 13:00:19 UTC, Regan Heath wrote:
> import std.stdio;
>
> struct S1 {
>    private int[] elements = [9,8,7];
>    int opApply (int delegate (ref uint, ref int) block) {
>        foreach (uint i, int n ; this.elements)
>            block(i, n);
>        return 0;
>    }
> }
>
> void main()
> {
> 	S1 range;	
> 	foreach(uint i, int x; range)
> 	{
> 	  writefln("%d is %d", i, x);
> 	}
> }
>
> R

byLine does not use opApply
https://github.com/D-Programming-Language/phobos/blob/master/std/stdio.d#L1389
February 11, 2014
On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote:
> Things like this should "just work"..
>
> File input ...
>
> auto range = input.byLine();
> while(!range.empty)
> {
>   range.popFront();
>   foreach (i, line; range.take(4))  //Error: cannot infer argument types
>   {
>     ..etc..
>   }
>   range.popFront();
> }
>
> Tried adding 'int' and 'char[]' or 'auto' .. no dice.
>
> Can someone explain why this fails, and if this is a permanent or temporary limitation of D/MD.
>
> R

foreach (i, line; iota(size_t.max).zip(range.take(4)))
{

}
February 11, 2014
On 02/11/2014 06:25 AM, Rene Zwanenburg wrote:
> On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote:

>>   foreach (i, line; range.take(4))  //Error: cannot infer argument types
>>   {
>>     ..etc..
>>   }

> foreach (i, line; iota(size_t.max).zip(range.take(4)))
> {
>
> }

There is also the following, relying on tuples' automatic expansion in foreach:

    foreach (i, element; zip(sequence!"n", range.take(4))) {
        // ...
    }

Ali

« First   ‹ Prev
1 2 3 4