Thread overview
[Issue 7839] New: std.range.count() too
[Issue 7839] std.range.countFrom() too
April 06, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7839

           Summary: std.range.count() too
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2012-04-05 17:59:57 PDT ---
I'd like a count() in std.range. It's similar to the generator with the same name in the Python itertools module: http://docs.python.org/library/itertools.html#itertools.count

a count() is also useful with bigints, where you can't replace it with
iota(BigInt.max).


A bare-bones implementation that shows it basic semantics (but it's useful to
add few more methods):


struct Count(T) {
    T n;
    this(T n_) { this.n = n_; }
    const bool empty = false;
    @property T front() { return n; }
    void popFront() { /* n++; */ n += 1; }
}

// Two helper functions
Count!T count(T)(T start) { return Count!T(start); }
Count!T count(T)() { return Count!T(cast(T)0); }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 23, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7839



--- Comment #1 from bearophile_hugs@eml.cc 2012-04-23 13:54:31 PDT ---
And answer to a question by jerro in D.learn:

http://forum.dlang.org/thread/4F949E90.9060700@webdrake.net#post-evdrwckxsqieyrweuxzp:40forum.dlang.org

> Couldn't it just be iota with no parameters?

The Count range has a helper count() function similar to this, that's meant to have an argument that defaults to zero:

Count!T count(T)(T start=0) if (isIntegral!T) { return Count!T(start); }

The argument allows it to start from another starting point, and it allows you to specify the type of the numbers it yields, while in iota() without arguments it's less easy to specify the type of the numbers it yields.

Count(5) is easy to replace with iota(5, int.max), but count(BigInt(0)) is less
easy to replace with iota, because it doesn't give you a way to denote a
right-open BigInt interval. And using iota(BigInt(0), BigInt(ulong.max)) is not
that good. Currently using BigInt in iota seems to not even being supported.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 25, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7839



--- Comment #2 from bearophile_hugs@eml.cc 2012-04-24 19:43:36 PDT ---
std.algorithm has a function named count(), so this range will need a different
name. Maybe countFrom()?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 25, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7839


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|std.range.count() too       |std.range.countFrom() too


--- Comment #3 from bearophile_hugs@eml.cc 2012-04-24 19:47:13 PDT ---
Changed the name of this issue from "std.range.count() too" to
"std.range.countFrom() too".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------