Author Topic: Растяг № 3  (Read 1371 times)

0 Members and 1 Guest are viewing this topic.

Offline Тайльнемер

  • Posts: 12501
  • Σοι υν βυρρο. Ix bin æn ézl
« on: October 12, 2014, 11:55 »
Код с длинными строчками:

Code: [Select]
public static partial class RatioExtensions { #region Sum public static Ratio Sum(this IEnumerable<Ratio> source) { return source.Aggregate(Ratio.Zero, (p, q) => p + q); } public static Ratio Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, Ratio> selector) { return source.Select(selector).Sum(); } #endregion #region Product public static Ratio Product(this IEnumerable<Ratio> source) { return source.Aggregate(Ratio.Unit, (p, q) => p * q); } public static Ratio Product<TSource>(this IEnumerable<TSource> source, Func<TSource, Ratio> selector) { return source.Select(selector).Product(); } #endregion #region Average public static Ratio Average(this IEnumerable<Ratio> source) { if(source == null) throw new ArgumentNullException("source"); var sum = Ratio.Zero; int count = 0; foreach(var p in source) { sum += p; count++; } if(count > 0) return sum / count; throw new InvalidOperationException("Sequence contains no elements"); } public static Ratio Average<TSource>(this IEnumerable<TSource> source, Func<TSource, Ratio> selector) { return source.Select(selector).Average(); } public static Ratio? Average(this IEnumerable<Ratio?> source) { if(source == null) throw new ArgumentNullException("source"); var sum = Ratio.Zero; int count = 0; foreach(var p in source.OfType<Ratio>()) { sum += p; count++; } return count > 0 ? sum / count : (Ratio?) null; } public static Ratio? Average<TSource>(this IEnumerable<TSource> source, Func<TSource, Ratio?> selector) { return source.Select(selector).Average(); } #endregion }

 

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Note: this post will not display until it's been approved by a moderator.
Name: Email:
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:
√49 Напишите ответ строчными буквами:
«Сто одёжек, все без застёжек» — что это?: