concept sequence in category .net

This is an excerpt from Manning's book Concurrency in .NET.
Partition the sequence into subsequences according to their order relative to the pivot.
The
foldfunction takes an initial value, commonly called the accumulator, which is used as a container for intermediate results. As a second argument it takes a binary expression that acts as a reduction function to apply against each element in the sequence to return the new value for the accumulator. In general, in reduction you take a binary operator—that is, a function with two arguments—and compute it over a vector or set of elements of size n, usually from left to right. Sometimes, a special seed value is used for the first operation with the first element, because there’s no previous value to use. During each step of the iteration, the binary expression takes the current element from the sequence and the accumulator value as inputs, and returns a value that overwrites the accumulator. The final result is the value of the last accumulator, as shown in figure 5.3.Figure 5.3 The
foldfunction reduces a sequence to a single value. The function(f), in this case, is multiplication and takes an initial accumulator with a value of1. For each iteration in the sequence (5, 7, 9), the function applies the calculation to the current item and accumulator. The result is then used to update the accumulator with the new value.![]()
The
foldfunction has two forms, right-fold and left-fold, depending on where the first item of the sequence to process is located. The right-fold starts from the first item in the list and iterates forward; the left-fold starts from the last item in the list and iterates backward. This section covers the right-fold because it’s most often used. For the remainder of the section, the term fold will be used in place of right-fold.
Listing 5.5 Implementing
FilterandLengthusing LINQAggregatein C#IEnumerable<T> Map<T, R>(IEnumerable<T> sequence, Func<T, R> projection){ return sequence.Aggregate(new List<R>(), (acc, item) => { #1 acc.Add(projection(item)); return acc; }); } int Max(IEnumerable<int> sequence) { #2 return sequence.Aggregate(0, (acc, item) => Math.Max(item, acc)); } IEnumerable<T> Filter<T>(IEnumerable<T> sequence, Func<T, bool> predicate){ return sequence.Aggregate(new List<T>(), (acc, item) => { #3 if (predicate(item)) acc.Add(item); return acc; }); } int Length<T>(IEnumerable<T> sequence) { #4 return sequence.Aggregate(0, (acc, _) => acc + 1); } #1 The Map function using LINQ Aggregate in C# #2 The Max function using LINQ Aggregate in C# #3 The Filter function using LINQ Aggregate in C# #4 The Length function using LINQ Aggregate in C#
The first argument
Rof thereduceFfunction has the same purpose of setting the degree of parallelism as the argumentMin the previousmapFfunction. The second argument is thereducefunction that operates on each key/values pair of the input parameter. In the case of the NuGet package example, the key is a string for the name of the package, and the sequence of values is the list of ranks associated with the package. Ultimately, the input argument is the sequence of key/value pairs, which matches the output of themapFfunction. ThereduceFfunction generates the final output.

This is an excerpt from Manning's book C# in Depth.
The code for the comparer in listing 1.6 is simpler because you’re given products to start with. No casting is necessary. Similarly, the invisible cast in the foreach loop is effectively gone now. The compiler still has to consider the conversion from the source type of the sequence to the target type of the variable, but it knows that in this case both types are Product, so it doesn’t need to emit any code for the conversion.
You’re already familiar with the concept of a sequence: it’s encapsulated by the IEnumerable and IEnumerable<T> interfaces, and we looked at those fairly closely in chapter 6 when we studied iterators. A sequence is like a conveyor belt of items—you fetch them one at a time until either you’re no longer interested or the sequence runs out of data.
The key difference between a sequence and other collection data structures, such as lists and arrays, is that when you’re reading from a sequence, you don’t generally know how many more items are waiting, and you don’t have access to arbitrary items—just the current one. Indeed, some sequences could be never-ending; you could easily have an infinite sequence of random numbers, for example. Lists and arrays can act as sequences, just as List<T> implements IEnumerable<T>, but the reverse isn’t always true. You can’t have an infinite array or list, for example.
Sequences are LINQ’s bread and butter. When you read a query expression, you should think about the sequences involved; there’s always at least one sequence to start with, and it’s usually transformed into other sequences along the way, possibly being joined with yet more sequences. LINQ query examples on the web frequently have little explanation, but when you take them apart by looking at each sequence in turn, things make a lot more sense. As well as being an aid to reading code, this approach can also help a lot when writing it. Thinking in sequences can be tricky—it’s a bit of a mental leap sometimes—but if you can get there, it’ll help you immeasurably when you’re working with LINQ.
Each arrow represents a sequence—the description is on the left side and some sample data is on the right. Each box is a step in the query expression. Initially, you have the whole family (as Person objects); then, after filtering, the sequence only contains adults (again, as Person objects); and the final result has the names of those adults as strings. Each time, you take one sequence and apply an operation to produce a new sequence. The result isn’t the strings Holly and Jon—instead, it’s an IEnumerable<string>, which, when asked for its elements one by one, will first yield Holly and then Jon.
Just to make it more obvious what’s going on, figure 11.6 shows the sequences as they’re processed.
Figure 11.6. The join from listing 11.12 in graphical form, showing two different sequences (defects and subscriptions) used as data sources
![]()
Often you’ll want to filter the sequence, and filtering before the join occurs is more efficient than filtering it afterward. At this stage, the query expression is simpler if the left sequence is the one requiring filtering. For instance, if you wanted to show only defects that are closed, you could use this query expression:
from defect in SampleData.AllDefects where defect.Status == Status.Closed join subscription in SampleData.AllSubscriptions on defect.Project equals subscription.Project select new { defect.Summary, subscription.EmailAddress }You can perform the same query with the sequences reversed, but it’s messier:
from subscription in SampleData.AllSubscriptions join defect in (from defect in SampleData.AllDefects where defect.Status == Status.Closed select defect) on subscription.Project equals defect.Project select new { defect.Summary, subscription.EmailAddress }Note how you can use one query expression inside another—the language specification describes many of the compiler translations in these terms. Nested query expressions are useful but hurt readability as well; it’s often worth looking for an alternative, or using a variable for the sequence on the right in order to make the code clearer.
You may be surprised to learn that filtering and ordering are two of the simplest operations to explain in terms of compiler translations. This is because they always return a sequence with the same element type as their input, which means you don’t need to worry about any new range variables being introduced. It also helps that you’ve seen the corresponding extension methods in chapter 10.

This is an excerpt from Manning's book Dependency Injection in .NET.
Figure 2.16. The Product class only contains the Name and UnitPrice properties, because these are the only properties needed to implement the desired application feature. The ApplyDiscountFor applies the discount (if any) for a user and returns an instance of the DiscountedProduct class. The abstract GetFeaturedProducts returns a sequence of Products.
![]()
The GetFeaturedProducts method returns a sequence of products, but the Product class is defined in the Data Access library. Any client consuming the GetFeaturedProducts method must reference the Data Access library to be able to compile.
It’s possible to change the signature of the method to return a sequence of a type defined within the Domain Model. It would also be more correct, but it doesn’t solve the problem.
Instead of feeling constrained by Castle Windsor, we should embrace its conventions and let it guide us toward a better and more consistent design. In this section, we’ll look at an example that will demonstrate how we can refactor away from ambiguity, as well as show how Castle Windsor deals with sequences, arrays, and lists.

This is an excerpt from Manning's book LINQ in Action.
We’ll start by explaining what sequences are and how they are used in LINQ queries.
In order to introduce sequences and understand why they are important, let’s dissect listing 3.1.