[Issue 5645] New: std.range.drop(), std.range.slice()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Feb 23 04:33:25 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5645

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


--- Comment #0 from bearophile_hugs at eml.cc 2011-02-23 04:30:40 PST ---
std.range may find useful a function named "drop", to skip the first n items of
a lazy iterable (it may call popFrontN if present, or empty/popFront
otherwise). 

Example: it allows to take the nth item of a lazy iterable:

import std.stdio, std.array, std.range;
void main() {
    auto fib = recurrence!("a[n-1] + a[n-2]")(1, 1);
    writeln(drop(fib, 9).front);
}


Instead of using something worse like:

import std.stdio, std.array, std.range;
void main() {
    auto fib = recurrence!("a[n-1] + a[n-2]")(1, 1);
    writeln(array(take(fib, 10)).back);
}


"drop" is present in Haskell too:
http://www.cse.unsw.edu.au/~en1000/haskell/inbuilt.html#take
(Haskell also has this syntax:  list !! n   to take exactly the n-th item of a
lazy list.)


In Python, module itertools, there is also a quite useful lazy slicing function
named islice(), that's more general than drop():
http://docs.python.org/library/itertools.html#itertools.islice

>>> from itertools import islice
>>> r = (x*x for x in xrange(10)) # lazy
>>> list(islice(r, 5, 8))
[25, 36, 49]

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


More information about the Digitalmars-d-bugs mailing list