BadScript 2
Loading...
Searching...
No Matches
CSharpx.EnumerableExtensions Class Reference

Classes

class  MaterializedEnumerable
 

Static Public Member Functions

static IEnumerable< TResult > Cartesian< TFirst, TSecond, TResult > (this IEnumerable< TFirst > first, IEnumerable< TSecond > second, Func< TFirst, TSecond, TResult > resultSelector)
 Returns the Cartesian product of two sequences by combining each element of the first set with each in the second and applying the user=define projection to the pair.
 
static IEnumerable< TSource > Prepend< TSource > (this IEnumerable< TSource > source, TSource value)
 Prepends a single value to a sequence.
 
static IEnumerable< T > Concat< T > (this T head, IEnumerable< T > tail)
 Returns a sequence consisting of the head element and the given tail elements.
 
static IEnumerable< T > Concat< T > (this IEnumerable< T > head, T tail)
 Returns a sequence consisting of the head elements and the given tail element.
 
static IEnumerable< T > Exclude< T > (this IEnumerable< T > sequence, int startIndex, int count)
 Excludes count elements from a sequence starting at a given index.
 
static IEnumerable< KeyValuePair< int, TSource > > Index< TSource > (this IEnumerable< TSource > source)
 Returns a sequence of KeyValuePair<TKey,TValue> where the key is the zero-based index of the value in the source sequence.
 
static IEnumerable< KeyValuePair< int, TSource > > Index< TSource > (this IEnumerable< TSource > source, int startIndex)
 Returns a sequence of KeyValuePair<TKey,TValue> where the key is the index of the value in the source sequence. An additional parameter specifies the starting index.
 
static TResult Fold< T, TResult > (this IEnumerable< T > source, Func< T, TResult > folder)
 Returns the result of applying a function to a sequence of 1 element.
 
static TResult Fold< T, TResult > (this IEnumerable< T > source, Func< T, T, TResult > folder)
 Returns the result of applying a function to a sequence of 2 elements.
 
static TResult Fold< T, TResult > (this IEnumerable< T > source, Func< T, T, T, TResult > folder)
 Returns the result of applying a function to a sequence of 3 elements.
 
static TResult Fold< T, TResult > (this IEnumerable< T > source, Func< T, T, T, T, TResult > folder)
 Returns the result of applying a function to a sequence of 4 elements.
 
static void ForEach< T > (this IEnumerable< T > source, Action< T > action)
 Immediately executes the given action on each element in the source sequence.
 
static IEnumerable< TResult > Pairwise< TSource, TResult > (this IEnumerable< TSource > source, Func< TSource, TSource, TResult > resultSelector)
 Returns a sequence resulting from applying a function to each element in the source sequence and its predecessor, with the exception of the first element which is only returned as the predecessor of the second element.
 
static string ToDelimitedString< TSource > (this IEnumerable< TSource > source)
 Creates a delimited string from a sequence of values. The delimiter used depends on the current culture of the executing thread.
 
static string ToDelimitedString< TSource > (this IEnumerable< TSource > source, string delimiter)
 Creates a delimited string from a sequence of values and a given delimiter.
 
static IEnumerable< T > Tail< T > (this IEnumerable< T > source)
 Return everything except first element and throws exception if empty.
 
static IEnumerable< T > TailNoFail< T > (this IEnumerable< T > source)
 Return everything except first element without throwing exception if empty.
 
static IEnumerable< T > Memoize< T > (this IEnumerable< T > source)
 Captures current state of a sequence.
 
static IEnumerable< T > Materialize< T > (this IEnumerable< T > source)
 Creates an immutable copy of a sequence.
 
static T Choice< T > (this IEnumerable< T > source)
 Selects a random element.
 
static IEnumerable< T > Intersperse< T > (this IEnumerable< T > source, T element)
 Takes an element and a sequence and ‘intersperses’ that element between its elements.
 
static IEnumerable< T > FlattenOnce< T > (this IEnumerable< IEnumerable< T > > source)
 Flattens a sequence by one level.
 
static IEnumerable< string > FlattenOnce (this IEnumerable< string > source)
 Reduces a sequence of strings to a sequence of parts, splitted by space, of each original string.
 
static Maybe< T > TryHead< T > (this IEnumerable< T > source)
 Safe function that returns Just(first element) or None.
 
static Maybe< IEnumerable< T > > ToMaybe< T > (this IEnumerable< T > source)
 Turns an empty sequence to Nothing, otherwise Just(sequence).
 

Static Private Member Functions

static IEnumerable< TSource > AssertCountImpl< TSource > (IEnumerable< TSource > source, int count, Func< int, int, Exception > errorSelector)
 
static IEnumerable< TSource > ExpectingCountYieldingImpl< TSource > (IEnumerable< TSource > source, int count, Func< int, int, Exception > errorSelector)
 
static IEnumerable< T > ExcludeImpl< T > (IEnumerable< T > sequence, int startIndex, int count)
 
static TResult FoldImpl< T, TResult > (IEnumerable< T > source, int count, Func< T, TResult > folder1, Func< T, T, TResult > folder2, Func< T, T, T, TResult > folder3, Func< T, T, T, T, TResult > folder4)
 
static Exception OnFolderSourceSizeError (int cmp, int count)
 
static IEnumerable< TResult > PairwiseImpl< TSource, TResult > (this IEnumerable< TSource > source, Func< TSource, TSource, TResult > resultSelector)
 
static string ToDelimitedStringImpl< T > (IEnumerable< T > source, string delimiter, Func< StringBuilder, T, StringBuilder > append)
 

Static Private Attributes

static readonly Func< int, int, Exception > OnFolderSourceSizeErrorSelector = OnFolderSourceSizeError
 

Detailed Description

Definition at line 20 of file EnumerableExtensions.cs.

Member Function Documentation

◆ AssertCountImpl< TSource >()

static IEnumerable< TSource > CSharpx.EnumerableExtensions.AssertCountImpl< TSource > ( IEnumerable< TSource >  source,
int  count,
Func< int, int, Exception >  errorSelector 
)
staticprivate

Definition at line 24 of file EnumerableExtensions.cs.

27 {
28 ICollection<TSource> collection = source as ICollection<TSource>; // Optimization for collections
29
30 if (collection != null)
31 {
32 if (collection.Count != count)
33 {
34 throw errorSelector(collection.Count.CompareTo(count), count);
35 }
36
37 return source;
38 }
39
40 return ExpectingCountYieldingImpl(source, count, errorSelector);
41 }

◆ Cartesian< TFirst, TSecond, TResult >()

static IEnumerable< TResult > CSharpx.EnumerableExtensions.Cartesian< TFirst, TSecond, TResult > ( this IEnumerable< TFirst >  first,
IEnumerable< TSecond >  second,
Func< TFirst, TSecond, TResult >  resultSelector 
)
static

Returns the Cartesian product of two sequences by combining each element of the first set with each in the second and applying the user=define projection to the pair.

Definition at line 71 of file EnumerableExtensions.cs.

75 {
76 if (first == null)
77 {
78 throw new ArgumentNullException(nameof(first));
79 }
80
81 if (second == null)
82 {
83 throw new ArgumentNullException(nameof(second));
84 }
85
86 if (resultSelector == null)
87 {
88 throw new ArgumentNullException(nameof(resultSelector));
89 }
90
91 return from element1 in first
92 from element2 in second // TODO buffer to avoid multiple enumerations
93 select resultSelector(element1, element2);
94 }

◆ Choice< T >()

static T CSharpx.EnumerableExtensions.Choice< T > ( this IEnumerable< T >  source)
static

Selects a random element.

Definition at line 478 of file EnumerableExtensions.cs.

479 {
480#if CSX_REM_CRYPTORAND
481 int index = new Random().Next(source.Count() - 1);
482#else
483 var index = new CryptoRandom().Next(source.Count() - 1);
484#endif
485 return source.ElementAt(index);
486 }

◆ Concat< T >() [1/2]

static IEnumerable< T > CSharpx.EnumerableExtensions.Concat< T > ( this IEnumerable< T >  head,
tail 
)
static

Returns a sequence consisting of the head elements and the given tail element.

Definition at line 126 of file EnumerableExtensions.cs.

127 {
128 if (head == null)
129 {
130 throw new ArgumentNullException(nameof(head));
131 }
132
133 return head.Concat(LinqEnumerable.Repeat(tail, 1));
134 }
System.Linq.Enumerable LinqEnumerable

◆ Concat< T >() [2/2]

static IEnumerable< T > CSharpx.EnumerableExtensions.Concat< T > ( this T  head,
IEnumerable< T >  tail 
)
static

Returns a sequence consisting of the head element and the given tail elements.

Definition at line 113 of file EnumerableExtensions.cs.

114 {
115 if (tail == null)
116 {
117 throw new ArgumentNullException(nameof(tail));
118 }
119
120 return tail.Prepend(head);
121 }

◆ Exclude< T >()

static IEnumerable< T > CSharpx.EnumerableExtensions.Exclude< T > ( this IEnumerable< T >  sequence,
int  startIndex,
int  count 
)
static

Excludes count elements from a sequence starting at a given index.

Template Parameters
TThe type of the elements of the sequence

Definition at line 140 of file EnumerableExtensions.cs.

141 {
142 if (sequence == null)
143 {
144 throw new ArgumentNullException(nameof(sequence));
145 }
146
147 if (startIndex < 0)
148 {
149 throw new ArgumentOutOfRangeException(nameof(startIndex));
150 }
151
152 if (count < 0)
153 {
154 throw new ArgumentOutOfRangeException(nameof(count));
155 }
156
157 return ExcludeImpl(sequence, startIndex, count);
158 }

◆ ExcludeImpl< T >()

static IEnumerable< T > CSharpx.EnumerableExtensions.ExcludeImpl< T > ( IEnumerable< T >  sequence,
int  startIndex,
int  count 
)
staticprivate

Definition at line 160 of file EnumerableExtensions.cs.

161 {
162 int index = -1;
163 int endIndex = startIndex + count;
164
165 using (IEnumerator<T> iter = sequence.GetEnumerator())
166 {
167 // yield the first part of the sequence
168 while (iter.MoveNext() && ++index < startIndex)
169 {
170 yield return iter.Current;
171 }
172
173 // skip the next part (up to count elements)
174 while (++index < endIndex && iter.MoveNext()) { }
175
176 // yield the remainder of the sequence
177 while (iter.MoveNext())
178 {
179 yield return iter.Current;
180 }
181 }
182 }

◆ ExpectingCountYieldingImpl< TSource >()

static IEnumerable< TSource > CSharpx.EnumerableExtensions.ExpectingCountYieldingImpl< TSource > ( IEnumerable< TSource >  source,
int  count,
Func< int, int, Exception >  errorSelector 
)
staticprivate

Definition at line 43 of file EnumerableExtensions.cs.

46 {
47 int iterations = 0;
48
49 foreach (TSource element in source)
50 {
51 iterations++;
52
53 if (iterations > count)
54 {
55 throw errorSelector(1, count);
56 }
57
58 yield return element;
59 }
60
61 if (iterations != count)
62 {
63 throw errorSelector(-1, count);
64 }
65 }

◆ FlattenOnce()

static IEnumerable< string > CSharpx.EnumerableExtensions.FlattenOnce ( this IEnumerable< string >  source)
static

Reduces a sequence of strings to a sequence of parts, splitted by space, of each original string.

Definition at line 530 of file EnumerableExtensions.cs.

531 {
532 foreach (string element in source)
533 {
534 string[] parts = element.Split();
535
536 foreach (string part in parts)
537 {
538 yield return part;
539 }
540 }
541 }

◆ FlattenOnce< T >()

static IEnumerable< T > CSharpx.EnumerableExtensions.FlattenOnce< T > ( this IEnumerable< IEnumerable< T > >  source)
static

Flattens a sequence by one level.

Definition at line 515 of file EnumerableExtensions.cs.

516 {
517 foreach (IEnumerable<T> element in source)
518 {
519 foreach (T subelement in element)
520 {
521 yield return subelement;
522 }
523 }
524 }

◆ Fold< T, TResult >() [1/4]

static TResult CSharpx.EnumerableExtensions.Fold< T, TResult > ( this IEnumerable< T >  source,
Func< T, T, T, T, TResult >  folder 
)
static

Returns the result of applying a function to a sequence of 4 elements.

Definition at line 237 of file EnumerableExtensions.cs.

238 {
239 return FoldImpl(source, 4, null, null, null, folder);
240 }

◆ Fold< T, TResult >() [2/4]

static TResult CSharpx.EnumerableExtensions.Fold< T, TResult > ( this IEnumerable< T >  source,
Func< T, T, T, TResult >  folder 
)
static

Returns the result of applying a function to a sequence of 3 elements.

Definition at line 228 of file EnumerableExtensions.cs.

229 {
230 return FoldImpl(source, 3, null, null, folder, null);
231 }

◆ Fold< T, TResult >() [3/4]

static TResult CSharpx.EnumerableExtensions.Fold< T, TResult > ( this IEnumerable< T >  source,
Func< T, T, TResult >  folder 
)
static

Returns the result of applying a function to a sequence of 2 elements.

Definition at line 219 of file EnumerableExtensions.cs.

220 {
221 return FoldImpl(source, 2, null, folder, null, null);
222 }

◆ Fold< T, TResult >() [4/4]

static TResult CSharpx.EnumerableExtensions.Fold< T, TResult > ( this IEnumerable< T >  source,
Func< T, TResult >  folder 
)
static

Returns the result of applying a function to a sequence of 1 element.

Definition at line 210 of file EnumerableExtensions.cs.

211 {
212 return FoldImpl(source, 1, folder, null, null, null);
213 }

◆ FoldImpl< T, TResult >()

static TResult CSharpx.EnumerableExtensions.FoldImpl< T, TResult > ( IEnumerable< T >  source,
int  count,
Func< T, TResult >  folder1,
Func< T, T, TResult >  folder2,
Func< T, T, T, TResult >  folder3,
Func< T, T, T, T, TResult >  folder4 
)
staticprivate

Definition at line 242 of file EnumerableExtensions.cs.

248 {
249 if (source == null)
250 {
251 throw new ArgumentNullException(nameof(source));
252 }
253
254 if ((count == 1 && folder1 == null) ||
255 (count == 2 && folder2 == null) ||
256 (count == 3 && folder3 == null) ||
257 (count == 4 && folder4 == null))
258 {
259 // ReSharper disable NotResolvedInText
260 throw new ArgumentNullException("folder"); // ReSharper restore NotResolvedInText
261 }
262
263 T[] elements = new T[count];
264
265 foreach (KeyValuePair<int, T> e in AssertCountImpl(source.Index(),
266 count,
268 ))
269 {
270 elements[e.Key] = e.Value;
271 }
272
273 switch (count)
274 {
275 case 1:
276 return folder1(elements[0]);
277 case 2:
278 return folder2(elements[0], elements[1]);
279 case 3:
280 return folder3(elements[0], elements[1], elements[2]);
281 case 4:
282 return folder4(elements[0], elements[1], elements[2], elements[3]);
283 default:
284 throw new NotSupportedException();
285 }
286 }
static readonly Func< int, int, Exception > OnFolderSourceSizeErrorSelector

◆ ForEach< T >()

static void CSharpx.EnumerableExtensions.ForEach< T > ( this IEnumerable< T >  source,
Action< T >  action 
)
static

Immediately executes the given action on each element in the source sequence.

Definition at line 300 of file EnumerableExtensions.cs.

301 {
302 if (source == null)
303 {
304 throw new ArgumentNullException(nameof(source));
305 }
306
307 if (action == null)
308 {
309 throw new ArgumentNullException(nameof(action));
310 }
311
312 foreach (T element in source)
313 {
314 action(element);
315 }
316 }

◆ Index< TSource >() [1/2]

static IEnumerable< KeyValuePair< int, TSource > > CSharpx.EnumerableExtensions.Index< TSource > ( this IEnumerable< TSource >  source)
static

Returns a sequence of KeyValuePair<TKey,TValue> where the key is the zero-based index of the value in the source sequence.

Definition at line 189 of file EnumerableExtensions.cs.

190 {
191 return source.Index(0);
192 }

◆ Index< TSource >() [2/2]

static IEnumerable< KeyValuePair< int, TSource > > CSharpx.EnumerableExtensions.Index< TSource > ( this IEnumerable< TSource >  source,
int  startIndex 
)
static

Returns a sequence of KeyValuePair<TKey,TValue> where the key is the index of the value in the source sequence. An additional parameter specifies the starting index.

Definition at line 199 of file EnumerableExtensions.cs.

202 {
203 return source.Select((element, index) => new KeyValuePair<int, TSource>(startIndex + index, element));
204 }

◆ Intersperse< T >()

static IEnumerable< T > CSharpx.EnumerableExtensions.Intersperse< T > ( this IEnumerable< T >  source,
element 
)
static

Takes an element and a sequence and ‘intersperses’ that element between its elements.

Definition at line 491 of file EnumerableExtensions.cs.

492 {
493 if (element == null)
494 {
495 throw new ArgumentNullException(nameof(element));
496 }
497
498 int count = source.Count();
499 int last = count - 1;
500
501 for (int i = 0; i < count; i++)
502 {
503 yield return source.ElementAt(i);
504
505 if (i != last)
506 {
507 yield return element;
508 }
509 }
510 }

◆ Materialize< T >()

static IEnumerable< T > CSharpx.EnumerableExtensions.Materialize< T > ( this IEnumerable< T >  source)
static

Creates an immutable copy of a sequence.

Definition at line 463 of file EnumerableExtensions.cs.

464 {
465 if (source is MaterializedEnumerable<T> ||
466 source.GetType()
467 .IsArray)
468 {
469 return source;
470 }
471
472 return new MaterializedEnumerable<T>(source);
473 }

◆ Memoize< T >()

static IEnumerable< T > CSharpx.EnumerableExtensions.Memoize< T > ( this IEnumerable< T >  source)
static

Captures current state of a sequence.

Definition at line 452 of file EnumerableExtensions.cs.

453 {
454 return source.GetType()
455 .IsArray
456 ? source
457 : source.ToArray();
458 }

◆ OnFolderSourceSizeError()

static Exception CSharpx.EnumerableExtensions.OnFolderSourceSizeError ( int  cmp,
int  count 
)
staticprivate

Definition at line 288 of file EnumerableExtensions.cs.

289 {
290 string message = cmp < 0
291 ? "Sequence contains too few elements when exactly {0} {1} expected"
292 : "Sequence contains too many elements when exactly {0} {1} expected";
293
294 return new Exception(string.Format(message, count.ToString("N0"), count == 1 ? "was" : "were"));
295 }

◆ Pairwise< TSource, TResult >()

static IEnumerable< TResult > CSharpx.EnumerableExtensions.Pairwise< TSource, TResult > ( this IEnumerable< TSource >  source,
Func< TSource, TSource, TResult >  resultSelector 
)
static

Returns a sequence resulting from applying a function to each element in the source sequence and its predecessor, with the exception of the first element which is only returned as the predecessor of the second element.

Definition at line 324 of file EnumerableExtensions.cs.

326 {
327 if (source == null)
328 {
329 throw new ArgumentNullException(nameof(source));
330 }
331
332 if (resultSelector == null)
333 {
334 throw new ArgumentNullException(nameof(resultSelector));
335 }
336
337 return PairwiseImpl(source, resultSelector);
338 }

◆ PairwiseImpl< TSource, TResult >()

static IEnumerable< TResult > CSharpx.EnumerableExtensions.PairwiseImpl< TSource, TResult > ( this IEnumerable< TSource >  source,
Func< TSource, TSource, TResult >  resultSelector 
)
staticprivate

Definition at line 340 of file EnumerableExtensions.cs.

343 {
344 Debug.Assert(source != null);
345 Debug.Assert(resultSelector != null);
346
347 using (IEnumerator<TSource> e = source.GetEnumerator())
348 {
349 if (!e.MoveNext())
350 {
351 yield break;
352 }
353
354 TSource previous = e.Current;
355
356 while (e.MoveNext())
357 {
358 yield return resultSelector(previous, e.Current);
359 previous = e.Current;
360 }
361 }
362 }

◆ Prepend< TSource >()

static IEnumerable< TSource > CSharpx.EnumerableExtensions.Prepend< TSource > ( this IEnumerable< TSource >  source,
TSource  value 
)
static

Prepends a single value to a sequence.

Definition at line 99 of file EnumerableExtensions.cs.

100 {
101 if (source == null)
102 {
103 throw new ArgumentNullException(nameof(source));
104 }
105
106 return LinqEnumerable.Repeat(value, 1)
107 .Concat(source);
108 }

◆ Tail< T >()

static IEnumerable< T > CSharpx.EnumerableExtensions.Tail< T > ( this IEnumerable< T >  source)
static

Return everything except first element and throws exception if empty.

Definition at line 414 of file EnumerableExtensions.cs.

415 {
416 using (IEnumerator<T> e = source.GetEnumerator())
417 {
418 if (e.MoveNext())
419 {
420 while (e.MoveNext())
421 {
422 yield return e.Current;
423 }
424 }
425 else
426 {
427 throw new ArgumentException("Source sequence cannot be empty", nameof(source));
428 }
429 }
430 }

◆ TailNoFail< T >()

static IEnumerable< T > CSharpx.EnumerableExtensions.TailNoFail< T > ( this IEnumerable< T >  source)
static

Return everything except first element without throwing exception if empty.

Definition at line 435 of file EnumerableExtensions.cs.

436 {
437 using (IEnumerator<T> e = source.GetEnumerator())
438 {
439 if (e.MoveNext())
440 {
441 while (e.MoveNext())
442 {
443 yield return e.Current;
444 }
445 }
446 }
447 }

◆ ToDelimitedString< TSource >() [1/2]

static string CSharpx.EnumerableExtensions.ToDelimitedString< TSource > ( this IEnumerable< TSource >  source)
static

Creates a delimited string from a sequence of values. The delimiter used depends on the current culture of the executing thread.

Definition at line 368 of file EnumerableExtensions.cs.

369 {
370 return ToDelimitedString(source, null);
371 }

◆ ToDelimitedString< TSource >() [2/2]

static string CSharpx.EnumerableExtensions.ToDelimitedString< TSource > ( this IEnumerable< TSource >  source,
string  delimiter 
)
static

Creates a delimited string from a sequence of values and a given delimiter.

Definition at line 377 of file EnumerableExtensions.cs.

378 {
379 if (source == null)
380 {
381 throw new ArgumentNullException(nameof(source));
382 }
383
384 return ToDelimitedStringImpl(source, delimiter, (sb, e) => sb.Append(e));
385 }

◆ ToDelimitedStringImpl< T >()

static string CSharpx.EnumerableExtensions.ToDelimitedStringImpl< T > ( IEnumerable< T >  source,
string  delimiter,
Func< StringBuilder, T, StringBuilder >  append 
)
staticprivate

Definition at line 387 of file EnumerableExtensions.cs.

390 {
391 Debug.Assert(source != null);
392 Debug.Assert(append != null);
393
394 delimiter = delimiter ?? CultureInfo.CurrentCulture.TextInfo.ListSeparator;
395 StringBuilder sb = new StringBuilder();
396 int i = 0;
397
398 foreach (T value in source)
399 {
400 if (i++ > 0)
401 {
402 sb.Append(delimiter);
403 }
404
405 append(sb, value);
406 }
407
408 return sb.ToString();
409 }

◆ ToMaybe< T >()

static Maybe< IEnumerable< T > > CSharpx.EnumerableExtensions.ToMaybe< T > ( this IEnumerable< T >  source)
static

Turns an empty sequence to Nothing, otherwise Just(sequence).

Definition at line 588 of file EnumerableExtensions.cs.

589 {
590 using (IEnumerator<T> e = source.GetEnumerator())
591 {
592 return e.MoveNext()
593 ? Maybe.Just(source)
594 : Maybe.Nothing<IEnumerable<T>>();
595 }
596 }

◆ TryHead< T >()

static Maybe< T > CSharpx.EnumerableExtensions.TryHead< T > ( this IEnumerable< T >  source)
static

Safe function that returns Just(first element) or None.

Definition at line 575 of file EnumerableExtensions.cs.

576 {
577 using (IEnumerator<T> e = source.GetEnumerator())
578 {
579 return e.MoveNext()
580 ? Maybe.Just(e.Current)
581 : Maybe.Nothing<T>();
582 }
583 }

Member Data Documentation

◆ OnFolderSourceSizeErrorSelector

readonly Func<int, int, Exception> CSharpx.EnumerableExtensions.OnFolderSourceSizeErrorSelector = OnFolderSourceSizeError
staticprivate

Definition at line 22 of file EnumerableExtensions.cs.


The documentation for this class was generated from the following file: