Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
February 12, 2016 How to force evaluation of range? | ||||
---|---|---|---|---|
| ||||
So I have this code and I have to add the element .each!(a => a.each!("a")); to the end in order for it to evaluate the range completely and act like I expect it too. Is there a better thing to put in the place of .each!(a => a.each!("a"));? import std.stdio; import std.path; import std.file; import std.uni; import std.range; import std.conv; import std.algorithm; void main(string[] Args){ assert(Args.length>1,"Need a path to source files"); assert(Args[1].isValidPath,"Path given is not Valid!"); dirEntries(Args[1], SpanMode.depth) .filter!(f => f.name.endsWith(".c",".h")) .tee!(a => writeln("\n",a,"\n\t","=".repeat(80).join)) .map!(a => a .File("r") .byLine .enumerate .filter!( l => l.value.byGrapheme.walkLength > 80) .tee!(a => writeln("Line: ",a.index,"\t",a.value)) ).each!(a => a.each!("a")); //Force evaluation of every item } |
February 13, 2016 Re: How to force evaluation of range? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Taylor Hillegeist | On Friday, 12 February 2016 at 20:43:24 UTC, Taylor Hillegeist wrote:
> So I have this code and I have to add the element
> .each!(a => a.each!("a"));
> to the end in order for it to evaluate the range completely and act like I expect it too. Is there a better thing to put in the place of
> .each!(a => a.each!("a"));?
>
> [...]
>
> dirEntries(Args[1], SpanMode.depth)
> .filter!(f => f.name.endsWith(".c",".h"))
> .tee!(a => writeln("\n",a,"\n\t","=".repeat(80).join))
> .map!(a => a
> .File("r")
> .byLine
> .enumerate
> .filter!( l => l.value.byGrapheme.walkLength > 80)
> .tee!(a => writeln("Line: ",a.index,"\t",a.value))
> ).each!(a => a.each!("a")); //Force evaluation of every item
>
> }
Have you tried .array? I *think* it's the commonly used way to flatten a lazy range.
|
February 13, 2016 Re: How to force evaluation of range? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Taylor Hillegeist | On Friday, 12 February 2016 at 20:43:24 UTC, Taylor Hillegeist wrote:
> So I have this code and I have to add the element
> .each!(a => a.each!("a"));
> to the end in order for it to evaluate the range completely and act like I expect it too. Is there a better thing to put in the place of
> .each!(a => a.each!("a"));?
>
> [...]
If you need the value that a range returns (i.e. the range performs "computation") then use .array
If you just want the range evaluated use walkLength
|
February 13, 2016 Re: How to force evaluation of range? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On Saturday, 13 February 2016 at 01:11:53 UTC, Nicholas Wilson wrote:
> ...
>
> If you just want the range evaluated use walkLength
It might work in this case, but in general this won't work for any range which defines .length as a member. In that case, walkLength will simply return .length of that range.
|
February 13, 2016 Re: How to force evaluation of range? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Taylor Hillegeist | On Friday, 12 February 2016 at 20:43:24 UTC, Taylor Hillegeist wrote: > So I have this code and I have to add the element > .each!(a => a.each!("a")); > to the end in order for it to evaluate the range completely and act like I expect it too. Is there a better thing to put in the place of > .each!(a => a.each!("a"));? > > ... The following combination might work: .joiner.each; http://dlang.org/phobos/std_algorithm_iteration.html#.joiner http://dlang.org/phobos/std_algorithm_iteration.html#.each |
February 13, 2016 Re: How to force evaluation of range? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Xinok | On Saturday, 13 February 2016 at 02:17:17 UTC, Xinok wrote:
> On Friday, 12 February 2016 at 20:43:24 UTC, Taylor Hillegeist wrote:
>> So I have this code and I have to add the element
>> .each!(a => a.each!("a"));
>> to the end in order for it to evaluate the range completely and act like I expect it too. Is there a better thing to put in the place of
>> .each!(a => a.each!("a"));?
>>
>> ...
>
> The following combination might work:
>
> .joiner.each;
>
> http://dlang.org/phobos/std_algorithm_iteration.html#.joiner
>
> http://dlang.org/phobos/std_algorithm_iteration.html#.each
Why not just .each; ?
|
February 13, 2016 Re: How to force evaluation of range? | ||||
---|---|---|---|---|
| ||||
Posted in reply to cym13 | On Saturday, 13 February 2016 at 03:16:09 UTC, cym13 wrote:
> On Saturday, 13 February 2016 at 02:17:17 UTC, Xinok wrote:
>> On Friday, 12 February 2016 at 20:43:24 UTC, Taylor Hillegeist wrote:
>>> So I have this code and I have to add the element
>>> .each!(a => a.each!("a"));
>>> to the end in order for it to evaluate the range completely and act like I expect it too. Is there a better thing to put in the place of
>>> .each!(a => a.each!("a"));?
>>>
>>> ...
>>
>> The following combination might work:
>>
>> .joiner.each;
>>
>> http://dlang.org/phobos/std_algorithm_iteration.html#.joiner
>>
>> http://dlang.org/phobos/std_algorithm_iteration.html#.each
>
> Why not just .each; ?
The thing he's trying to iterate over is a range of ranges. A single .each will only iterate over the outermost range so you need .joiner first to "flatten" the range, then you can use .each on that result.
|
Copyright © 1999-2021 by the D Language Foundation