Comments (8)
I like the idea of a Generate method (and I've written one before) but I don't
think
I'd specify either a condition or a number of elements to generate - using Take
and
TakeWhile do that well enough, I think.
I haven't looked at the patch yet - will try to do so tonight.
Original comment by jonathan.skeet
on 16 Feb 2009 at 7:34
from morelinq.
We can borrow from Python's (x)range here and add a Range operator:
Range(int stop)
Range(int start, int stop)
Range(int start, int stop, int step)
I can also imagine a default, zero-arguments, overload of Range that produces
the
full series of integers/longs. With this in place, one can use Select and other
existing operators to the same effect. For example:
Series.Expand(2, n => n * n, n > 1000) => (2, 4, 8, 16, 32, 64, 128, 256, 512)
becomes:
Range<int>().Select(n => n + 2)
.Select(n => n * n)
.TakeWhile(n => n < 1000)
That can be a bit mouthful and verbose so I can understand CAmmerman's point to
have
something that simplifies a very common use case. After all, many base LINQ
operators also have overloads for projection (see Max, for example) when there
is
already Select. However, I would stard with Range because that can serve as a
basis
for Expand.
Original comment by azizatif
on 16 Feb 2009 at 9:15
from morelinq.
OK, I'm silly and it had momentarily slipped my mind that Range already exists:
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx
So we are now talking about:
Range(2, int.MaxValue).Select(n => n * n).TakeWhile(n => n < 1000)
Original comment by azizatif
on 16 Feb 2009 at 9:51
from morelinq.
> but I don't think I'd specify either a condition or a number of elements to
generate
I was thinking along the lines of what azizatif was saying, about simplifying
the
common use cases. I'm not married to it however. And we could certainly have
an
overload without the terminator condition or element count. On a related note,
the
terminal check might do better if it worked like TakeWhile, rather than "take
until".
The code itself is older, and I don't remember why I chose to do it this way originally.
There might be a better name for this method than Expand, as well. It was just
the
simplest thing that applied, in my mind at the time.
Original comment by [email protected]
on 16 Feb 2009 at 1:20
from morelinq.
My example (which has since been repeated by Aziz) was not quite right.... The
n * n
should be n * 2, to generate the sequence I specified. n * n would be repeated
squaring, which would overflow quite rapidly. =)
Series.Expand(2, n => n * 2, n > 1000) => (2, 4, 8, 16, 32, 64, 128, 256, 512)
The patch I submitted has the same typo in the comments.
Original comment by [email protected]
on 16 Feb 2009 at 3:18
from morelinq.
If we're looking at Range, I have a much more competent Range class in
MiscUtil. I'm
not sure whether I want to bring that over to MoreLinq though - it's useful
outside
LINQ too, and I'd rather keep to operators.
@Comment 5: That example still isn't right - it should be:
Series.Expand(2, n => n * 2, n => n > 1000)
which I'd still prefer to see as:
Series.Expand(2, n => n * 2).TakeWhile(n => n <= 1000)
Generating a sequence and terminating it are somewhat orthogonal concepts, and
the
latter already exists in a well-known form. I think I'd find the latter easier
to
read, aside from anything else.
Original comment by jonathan.skeet
on 17 Feb 2009 at 6:31
from morelinq.
@CAmmerman: You should commit your patch. I agree with Jon, though, to keep the
terminating condition out for now. It can always be added later in version 2.
Meanwhile it does not prevent anyone from having their own overload that
combines
Expand with TakeWhile or Expand with Take to get your original three-part
Expand
version. At least, they'll be getting a tested base to build on.
Original comment by azizatif
on 17 Feb 2009 at 11:32
from morelinq.
Implemented in r38.
Original comment by azizatif
on 18 Feb 2009 at 9:31
- Changed title: Adding series generation/comprehension methods
- Changed state: Fixed
- Added labels: Milestone-Release1.0
from morelinq.
Related Issues (20)
- Usage example for SingleOrFallback HOT 7
- converting IEnumerable<TSource> into IEnumerable<IEnumerable<TSource>>? HOT 1
- Buffering HOT 1
- Please make .NET 4 compatible distribution (or allow silent modifications)
- Interleave irregular enumerables with circular strategy HOT 4
- TakeLast example incorrect HOT 5
- Patch for /MoreLinq/DistinctBy.cs HOT 2
- Implement Full Outer Join
- ExpandoObject inclusion HOT 4
- Patch for /MoreLinq/EquiZip.cs
- Change ArgumentNullException to early return. HOT 1
- MaxByOrDefault
- Scan should not throw on empty sequences
- Batch should return an IEnumerable<IList<T>> HOT 1
- Migrate to GitHub/BitBucket HOT 14
- add Memoize support HOT 1
- ForEach should optionally take an index HOT 2
- ToDataTable throws on type with public static member HOT 6
- It would be nice to have a Top operator that would merge OrderBy and Take
- F# countBy analogue HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from morelinq.