Impressed
Graham Fawcett
fawcett at uwindsor.ca
Fri Jul 27 08:09:37 PDT 2012
On Friday, 27 July 2012 at 13:10:46 UTC, Stuart wrote:
> On Friday, 27 July 2012 at 03:00:25 UTC, Brad Anderson wrote:
>>
>> D equivalent: iota(0, int.max, 2).map!(a => /* do something
>> with even numbers */)();
>
> I think you're missing the point. The purpose isn't to generate
> a sequence of numbers, but to illustrate how the Yield keyword
> is used in VB.NET. Sure, getting a sequence of numbers may be
> straightforward, but what about a lazy-populated list of all
> files on a computer? That can be done using Yield - and more
> importantly, WRITTEN like a normal synchronous function. Let's
> see you do that with map.
That's easy:
import std.file, std.stdio, std.algorithm;
void main()
{
static BASE_DIR = "/path/to/base";
static SIZE_CUTOFF = 100;
// 'entries' is an InputRange
auto entries = dirEntries(BASE_DIR, SpanMode.breadth);
// filter the range; map to a range of filenames
auto smallFileNames = entries
.filter!(e => e.size < SIZE_CUTOFF)
.map!(e => e.name);
// note, the filesystem hasn't been touched yet;
// we have full laziness.
foreach(name; smallFileNames)
writeln(name);
}
And check out this example, where you can process the entries in
parallel:
http://dlang.org/phobos/std_file.html#dirEntries
You should spend some time using ranges before drawing
conclusions about them.
Graham
More information about the Digitalmars-d
mailing list