/Olov
What is your thinking behind including the endpoint?
Cheers,
Peter
Good question. My first implementation didn't do that actually, but
followed the Pythonic xrange style of excluding the stop endpoint. In
Python, this is nice for a number of reasons such as being consistent
with slices and supporting the for i in xrange(len(c)) pattern. [The
latter has been superseded by for i, v in enumerate(c) to some
extent.]
Things changed when I wrote the tests. When a Range is used to
actually generate ranges of discrete values, I found the closed
notation much easier (i.e. including both endpoints). Especially so
when supporting negative ranges and steps other than 1. So I changed
it.
On the other hand, when a Range is used to represent indices of a
sequence, be it for iterating over everything or for representing a
slice, then I think half-open makes more sense (excluding stop
endpoint). For now i just put in the indices function to handle the
simplest use-case as some kind of compromise.
Since Range isn't in corelibs I opted for the first use-case, but I
don't have a strong preference. If we would like to push Range (or
something Range'ish) further down then I think half-open is the way to
go for the Pythonic reasons.
/Olov
I agree that Python/C#-style yield is nice, convenient and makes it
easier to create SICP-like lazy streams, but it's not of particular
use for Range. One could play with the thought of making Range.filter
or Range.map lazy instead of returning Lists, but it's not very useful
given the definition of Range; a sequence parametrized by start, stop
and step. Also do note that you can implement lazy collections today
already, it's just that you need to explicitly perform the same state
bookkeeping that yield would do for you.
Having said all that, I'd +1 a proposal of landing something yield'ish
in Dart before you finished saying "await!",
/Olov
Iterable<int> squares() {
int i = 0;
return IterableThing(()=>i*i++);
}
You need to have an Iterable object that knows to apply the closure, but
that seems pretty light compared to special syntax.
Not in my example implementation but you should be able to create your
own DisjointRange based on it without a whole lot of new code.
/Olov
groovy does some reasonable (imho) things with ranges
http://groovy.codehaus.org/Collections
thanks
---
co-chair http://ocjug.org/
I think a "step" parameter would also be useful.
Maybe something like:
/**
* Literal syntax:
* x..y (x to y)
* x..<y (x to y - 1)
* x.. (x to infinity)
* ..x (x to -infinity)
*/
interface Range extends List<int> {
/**
* first must not be null
* last defaults to infinity if null
* step must be > 0, steps toward last even if `last < first`
*/
Range(int first, [int last, int step/* = 1*/]);
int first; // should be in List instead
int step;
}
Cheers,
Sean Eagan
groovy also has:
To remove the first character of a string we can use Groovy's range
index notation.
def name = 'GROOVY'
def substring = name[1..-1]
assert substring == 'ROOVY'
To remove the last character of a string we can use Groovy's range
index notation.
def name = 'GROOVY'
def substring = name[0..-2]
assert substring == 'GROOV'
thanks
>On Tue, Jan 17, 2012 at 2:23 PM, Ray Tayek <rta...@ca.rr.com> wrote:
> >
> > At 08:53 AM 1/14/2012, Ray Tayek wrote:
> >>
> >> On Nov 21 2011, 2:42 am, Olov Lassus <olov.las...@gmail.com> wrote:
> >> > Ranges has been mentioned on misc before but I figured that it would
> >> > be a nice example to write about anyways. ...
> >
> >
> > groovy does some reasonable (imho) things with ranges
> http://groovy.codehaus.org/Collections ...
---
co-chair http://ocjug.org/