Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It feels to me like the C# design is more encapsulated: a sequence runs out or does, independent of anything else. Where does it make <em>sense</em> to compare one limit with another?</p> <p>If you only want to take a few elements, then LINQ provides any number of ways of building one sequence from another, e.g.</p> <pre><code>foo.Take(10) foo.Skip(10) foo.Where(x =&gt; x.StartsWith("y")) </code></pre> <p>etc</p> <p>I think it's clearer - and more composable - to transform one sequence into another than to specify this with limits. If you want to pass an iterator to another function, but you want to restrict it to the first few elements, why should you have to <em>also</em> pass a limit? Why not just pass the transformed sequence which self-limits?</p> <p>EDIT: To address your question edit: in C# (at least with LINQ) you wouldn't modify the existing collection. You would create a new sequence from the old one. This is done lazily; it doesn't create a new copy or anything like that. For LINQ to Objects, this is performed using extension methods on <code>IEnumerable&lt;T&gt;</code>, so any sequence gains the same abilities.</p> <p>Note that this isn't restricted to traditional collections - it could be a sequence of lines read from a log file (again, lazily). You don't need any knowledge of the collection itself, just that it's a <em>sequence</em> from which one can draw items. There's also the difference between an <code>IEnumerable&lt;T&gt;</code> and an <code>IEnumerator&lt;T&gt;</code> where the first represents the sequence and the second represents an iterator over a sequence; <code>IEnumerator&lt;T&gt;</code> is rarely used explicitly in C#, or passed around.</p> <p>Now, your example of "all elements except the last 10" is a tricky one, because for a general sequence you can't tell that you're 10 elements from the end until you've reached the end. There's nothing in LINQ to Objects to do this explicitly. For anything implementing <code>ICollection</code> or <code>ICollection&lt;T&gt;</code> you could use</p> <pre><code>Bar(list.Take(list.Count - 10)) </code></pre> <p>but that's not very general. A more general solution would need to maintain a circular buffer of 10 elements, effectively reading 10 ahead of where it's yielding. I've rarely found this to be a requirement, to be honest.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload