Why ByChunk is not regnize by st.range.takeOne and why Section has no value

2014-11-02 Thread bioinfornatics via Digitalmars-d-learn

Dear,
Some problem to build this code: 
http://fpaste.org/147327/75948141/



$ ldc2 fasta_test.d
/usr/include/d/std/range.d(3605): Error: template std.array.save
cannot deduce function from argument types !()(ByChunk),
candidates are:
/usr/include/d/std/array.d(554):std.array.save(T)(T[] a)
fasta_test.d(46): Error: template instance
std.range.takeOne!(ByChunk) error instantiating
fasta_test.d(88):instantiated from here: Parser!(Fasta,
ByChunk)
fasta_test.d(79): Error: type Section!((words) => words[0] ==
'>', (words) => words[0] == '\x0a') has no value
fasta_test.d(50): Error: template std.algorithm.countUntil cannot
deduce function from argument types !(__lambda3)(ubyte[]),
candidates are:
/usr/include/d/std/algorithm.d(5406):
std.algorithm.countUntil(alias pred = "a == b", R, Rs...)(R
haystack, Rs needles) if (isForwardRange!R && Rs.length > 0 &&
isForwardRange!(Rs[0]) == isInputRange!(Rs[0]) &&
is(typeof(startsWith!pred(haystack, needles[0]))) && (Rs.length
== 1 || is(typeof(countUntil!pred(haystack,
needles[1..__dollar])

Thanks for your help


Re: Why ByChunk is not regnize by st.range.takeOne and why Section has no value

2014-11-02 Thread Ali Çehreli via Digitalmars-d-learn

On 11/02/2014 04:58 PM, bioinfornatics wrote:

> Dear,
> Some problem to build this code: http://fpaste.org/147327/75948141/
>
>
> $ ldc2 fasta_test.d
> /usr/include/d/std/range.d(3605): Error: template std.array.save
> cannot deduce function from argument types !()(ByChunk),
> candidates are:

I think that is due to the too permissive template constraint of takeOne 
in phobos/std/range.d. Even though it say "isInputRange!R", it clearly 
needs "isForwardRange!R" because it calls .save() on that range:


@property auto save() { return Result(_source.save, empty); }

So, the following is wrong:

  auto takeOne(R)(R source) if (isInputRange!R)

It should be:

  auto takeOne(R)(R source) if (isForwardRange!R)

If you modify range.d locally to require ForwardRange, you will see that 
your following call will get an error message because byChunk is not a 
ForwardRange:


// Take a piece of byte from current file
ubyte[] buffer =  takeOne(inputRange);

I think this issue is already reported as a part of the following bug:

  https://issues.dlang.org/show_bug.cgi?id=9724

Ali



Re: Why ByChunk is not regnize by st.range.takeOne and why Section has no value

2014-11-03 Thread bioinfornatics via Digitalmars-d-learn

On Monday, 3 November 2014 at 06:43:50 UTC, Ali Çehreli wrote:

On 11/02/2014 04:58 PM, bioinfornatics wrote:

> Dear,
> Some problem to build this code:
http://fpaste.org/147327/75948141/
>
>
> $ ldc2 fasta_test.d
> /usr/include/d/std/range.d(3605): Error: template
std.array.save
> cannot deduce function from argument types !()(ByChunk),
> candidates are:

I think that is due to the too permissive template constraint 
of takeOne in phobos/std/range.d. Even though it say 
"isInputRange!R", it clearly needs "isForwardRange!R" because 
it calls .save() on that range:


@property auto save() { return Result(_source.save, empty); 
}


So, the following is wrong:

  auto takeOne(R)(R source) if (isInputRange!R)

It should be:

  auto takeOne(R)(R source) if (isForwardRange!R)

If you modify range.d locally to require ForwardRange, you will 
see that your following call will get an error message because 
byChunk is not a ForwardRange:


// Take a piece of byte from current file
ubyte[] buffer =  takeOne(inputRange);

I think this issue is already reported as a part of the 
following bug:


  https://issues.dlang.org/show_bug.cgi?id=9724

Ali


Ok but I do not see why to use save method for a takeOne function
is possible to write this function without to use it


Re: Why ByChunk is not regnize by st.range.takeOne and why Section has no value

2014-11-03 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, November 03, 2014 21:03:51 bioinfornatics via Digitalmars-d-learn 
wrote:
> Ok but I do not see why to use save method for a takeOne function
> is possible to write this function without to use it

It looks like the idea was that beacause you know that there's only one
element, you can make it random-access range with opSlice and popBack and the
whole shebang. And that works great if the range is at least a forward range,
but it currently falls apart with an input range, because while you can easily
emulate stuff like back and opIndex by forwarding it to front, save can't be
emulated so easily. So, either front needs to be accessed and saved in the
Result struct rather than than simply having it contain the range that it's
forwarding to, or it needs to have another branch of the static if for
handling input ranges separately. Though to be honest, I'm not sure that
using takeOne makes a lot of sense over take in that case. You don't really
get a performance boost, and since you can't add on the random-access range
capabilities without making it eager and saving the result of front (which is
an iffy thing to do with some input ranges), it's really not adding anything
over plain take. I'd honestly favor just fixing takeOne's template constraint
so that it checked for forward ranges. The only reason that I see to make
takeOne work with input ranges is simply so that no one will be surprised when
it doesn't work with an input range. But that's not really adding any useful
functionality as far as I can tell. Though maybe there's something that I'm
missing.

- Jonathan M Davis



Re: Why ByChunk is not regnize by st.range.takeOne and why Section has no value

2014-11-03 Thread bioinfornatics via Digitalmars-d-learn

On Monday, 3 November 2014 at 21:03:51 UTC, bioinfornatics wrote:

On Monday, 3 November 2014 at 06:43:50 UTC, Ali Çehreli wrote:

On 11/02/2014 04:58 PM, bioinfornatics wrote:

> Dear,
> Some problem to build this code:
http://fpaste.org/147327/75948141/
>
>
> $ ldc2 fasta_test.d
> /usr/include/d/std/range.d(3605): Error: template
std.array.save
> cannot deduce function from argument types !()(ByChunk),
> candidates are:

I think that is due to the too permissive template constraint 
of takeOne in phobos/std/range.d. Even though it say 
"isInputRange!R", it clearly needs "isForwardRange!R" because 
it calls .save() on that range:


   @property auto save() { return Result(_source.save, empty); 
}


So, the following is wrong:

 auto takeOne(R)(R source) if (isInputRange!R)

It should be:

 auto takeOne(R)(R source) if (isForwardRange!R)

If you modify range.d locally to require ForwardRange, you 
will see that your following call will get an error message 
because byChunk is not a ForwardRange:


   // Take a piece of byte from current file
   ubyte[] buffer =  takeOne(inputRange);

I think this issue is already reported as a part of the 
following bug:


 https://issues.dlang.org/show_bug.cgi?id=9724

Ali


Ok but I do not see why to use save method for a takeOne 
function

is possible to write this function without to use it


Example:

auto takeOne(R)(ref R source) if (isInputRange!R)
{
 static if (hasSlicing!R)
 {
 return source[0 .. !source.empty];
 }
 else
 {
 typeof(R.front) r;
 if(!source.empty){
 r = source.front;
 source.popFront();
 }

 return r;
 }
}