February 21, 2013
On 02/21/2013 05:14 PM, Samuel Lampa wrote:
> Sure. Please find some sample python code <snip>
Ugh, s/find/find below/
February 21, 2013
Samuel Lampa:

> python has an even more compact syntax for creating generator objects:

D doesn't have a generator syntax, but it's easy to do what you want using UFCS in D.


> gen_lines = (line.rstrip("\n") for line in open("infile.txt"))
> gen_uppercase_lines = (line.upper() for line in gen_lines)
> gen_final_lines = ("Line: " + line for line in gen_lines)
>
> # Drive the pipeline, one line at a time
> for line in gen_final_lines:
>     print line

==>

import std.stdio, std.algorithm, std.string;

void main() {
    auto gen = File("infile.txt")
               .byLine()
               .map!chomp
               .map!toUpper
               .map!(line => "Line: " ~ line);

    writefln("%-(%s\n%)", gen);
}


Bye,
bearophile
February 21, 2013
> import std.stdio, std.algorithm, std.string;
>
> void main() {
>     auto gen = File("infile.txt")
>                .byLine()
>                .map!chomp
>                .map!toUpper
>                .map!(line => "Line: " ~ line);
>
>     writefln("%-(%s\n%)", gen);
> }

Or faster:


import std.stdio, std.algorithm, std.string;

void main() {
    auto gen = File("infile.txt")
               .byLine()
               .map!(line => "Line: " ~ line.chomp.toUpper);

    writefln("%-(%s\n%)", gen);
}


Bye,
bearophile
February 21, 2013
On 02/21/2013 05:55 PM, bearophile wrote:
> import std.stdio, std.algorithm, std.string;
>
> void main() {
>     auto gen = File("infile.txt")
>                .byLine()
>                .map!(line => "Line: " ~ line.chomp.toUpper);
>
>     writefln("%-(%s\n%)", gen);
> }

Cool, thanks!

// Samuel
February 21, 2013
02/21/2013 05:19 PM, jerro wrote:
> D has a concept of [ranges], which are similar to python's iterators, but more general (python iterators are roughly equivalent to input ranges). There are no generator functions in D, but you can use fibers to get similar functionality ...<snip>

Right, will dig into the ranges then! :) Thanks for the hints!

// Samuel
March 13, 2013
Ha ha! Just saw this. :)

On 02/21/2013 07:41 AM, jerro wrote:
>> P.S. I am in the process of translating my Turkish D book to English. For
>> completeness, here are the two chapters about ranges:
>>
>> http://ddili.org/ders/d/araliklar.html
>>
>> http://ddili.org/ders/d/araliklar_baska.html
>
> Why not link to the english translation, too?
>
> http://ddili.org/ders/d.en/ranges.html

I might have made a typo there but no: My quote above was from

  On Thu, 08 Sep 2011 13:35:02 +0200

but the very first English draft of that chapter has been submitted on

  On Oct 11, 2011


http://code.google.com/p/ddili/source/list?path=/trunk/src/ders/d.en/ranges.d&start=267

:)

Ali

1 2
Next ›   Last »