Min.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Threading.Tasks;
  5. namespace System.Reactive.Linq
  6. {
  7. partial class AsyncObserver
  8. {
  9. public static IAsyncObserver<int> MinInt32(this IAsyncObserver<int> observer)
  10. {
  11. if (observer == null)
  12. throw new ArgumentNullException(nameof(observer));
  13. var min = 0;
  14. var found = false;
  15. return Create<int>(
  16. x =>
  17. {
  18. if (found)
  19. {
  20. if (x < min)
  21. {
  22. min = x;
  23. }
  24. }
  25. else
  26. {
  27. min = x;
  28. found = true;
  29. }
  30. return Task.CompletedTask;
  31. },
  32. observer.OnErrorAsync,
  33. async () =>
  34. {
  35. if (!found)
  36. {
  37. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  38. }
  39. else
  40. {
  41. await observer.OnNextAsync(min).ConfigureAwait(false);
  42. await observer.OnCompletedAsync().ConfigureAwait(false);
  43. }
  44. }
  45. );
  46. }
  47. public static IAsyncObserver<long> MinInt64(this IAsyncObserver<long> observer)
  48. {
  49. if (observer == null)
  50. throw new ArgumentNullException(nameof(observer));
  51. var min = 0L;
  52. var found = false;
  53. return Create<long>(
  54. x =>
  55. {
  56. if (found)
  57. {
  58. if (x < min)
  59. {
  60. min = x;
  61. }
  62. }
  63. else
  64. {
  65. min = x;
  66. found = true;
  67. }
  68. return Task.CompletedTask;
  69. },
  70. observer.OnErrorAsync,
  71. async () =>
  72. {
  73. if (!found)
  74. {
  75. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  76. }
  77. else
  78. {
  79. await observer.OnNextAsync(min).ConfigureAwait(false);
  80. await observer.OnCompletedAsync().ConfigureAwait(false);
  81. }
  82. }
  83. );
  84. }
  85. public static IAsyncObserver<float> MinSingle(this IAsyncObserver<float> observer)
  86. {
  87. if (observer == null)
  88. throw new ArgumentNullException(nameof(observer));
  89. var min = 0.0f;
  90. var found = false;
  91. return Create<float>(
  92. x =>
  93. {
  94. if (found)
  95. {
  96. if (x < min || double.IsNaN(x))
  97. {
  98. min = x;
  99. }
  100. }
  101. else
  102. {
  103. min = x;
  104. found = true;
  105. }
  106. return Task.CompletedTask;
  107. },
  108. observer.OnErrorAsync,
  109. async () =>
  110. {
  111. if (!found)
  112. {
  113. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  114. }
  115. else
  116. {
  117. await observer.OnNextAsync(min).ConfigureAwait(false);
  118. await observer.OnCompletedAsync().ConfigureAwait(false);
  119. }
  120. }
  121. );
  122. }
  123. public static IAsyncObserver<double> MinDouble(this IAsyncObserver<double> observer)
  124. {
  125. if (observer == null)
  126. throw new ArgumentNullException(nameof(observer));
  127. var min = 0.0;
  128. var found = false;
  129. return Create<double>(
  130. x =>
  131. {
  132. if (found)
  133. {
  134. if (x < min || double.IsNaN(x))
  135. {
  136. min = x;
  137. }
  138. }
  139. else
  140. {
  141. min = x;
  142. found = true;
  143. }
  144. return Task.CompletedTask;
  145. },
  146. observer.OnErrorAsync,
  147. async () =>
  148. {
  149. if (!found)
  150. {
  151. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  152. }
  153. else
  154. {
  155. await observer.OnNextAsync(min).ConfigureAwait(false);
  156. await observer.OnCompletedAsync().ConfigureAwait(false);
  157. }
  158. }
  159. );
  160. }
  161. public static IAsyncObserver<decimal> MinDecimal(this IAsyncObserver<decimal> observer)
  162. {
  163. if (observer == null)
  164. throw new ArgumentNullException(nameof(observer));
  165. var min = 0m;
  166. var found = false;
  167. return Create<decimal>(
  168. x =>
  169. {
  170. if (found)
  171. {
  172. if (x < min)
  173. {
  174. min = x;
  175. }
  176. }
  177. else
  178. {
  179. min = x;
  180. found = true;
  181. }
  182. return Task.CompletedTask;
  183. },
  184. observer.OnErrorAsync,
  185. async () =>
  186. {
  187. if (!found)
  188. {
  189. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  190. }
  191. else
  192. {
  193. await observer.OnNextAsync(min).ConfigureAwait(false);
  194. await observer.OnCompletedAsync().ConfigureAwait(false);
  195. }
  196. }
  197. );
  198. }
  199. public static IAsyncObserver<int?> MinNullableInt32(this IAsyncObserver<int?> observer)
  200. {
  201. if (observer == null)
  202. throw new ArgumentNullException(nameof(observer));
  203. var min = default(int?);
  204. return Create<int?>(
  205. x =>
  206. {
  207. if (min == null || x < min)
  208. {
  209. min = x;
  210. }
  211. return Task.CompletedTask;
  212. },
  213. observer.OnErrorAsync,
  214. async () =>
  215. {
  216. await observer.OnNextAsync(min).ConfigureAwait(false);
  217. await observer.OnCompletedAsync().ConfigureAwait(false);
  218. }
  219. );
  220. }
  221. public static IAsyncObserver<long?> MinNullableInt64(this IAsyncObserver<long?> observer)
  222. {
  223. if (observer == null)
  224. throw new ArgumentNullException(nameof(observer));
  225. var min = default(long?);
  226. return Create<long?>(
  227. x =>
  228. {
  229. if (min == null || x < min)
  230. {
  231. min = x;
  232. }
  233. return Task.CompletedTask;
  234. },
  235. observer.OnErrorAsync,
  236. async () =>
  237. {
  238. await observer.OnNextAsync(min).ConfigureAwait(false);
  239. await observer.OnCompletedAsync().ConfigureAwait(false);
  240. }
  241. );
  242. }
  243. public static IAsyncObserver<float?> MinNullableSingle(this IAsyncObserver<float?> observer)
  244. {
  245. if (observer == null)
  246. throw new ArgumentNullException(nameof(observer));
  247. var min = default(float?);
  248. return Create<float?>(
  249. x =>
  250. {
  251. if (x != null && (min == null || x < min || double.IsNaN(x.Value)))
  252. {
  253. min = x;
  254. }
  255. return Task.CompletedTask;
  256. },
  257. observer.OnErrorAsync,
  258. async () =>
  259. {
  260. await observer.OnNextAsync(min).ConfigureAwait(false);
  261. await observer.OnCompletedAsync().ConfigureAwait(false);
  262. }
  263. );
  264. }
  265. public static IAsyncObserver<double?> MinNullableDouble(this IAsyncObserver<double?> observer)
  266. {
  267. if (observer == null)
  268. throw new ArgumentNullException(nameof(observer));
  269. var min = default(double?);
  270. return Create<double?>(
  271. x =>
  272. {
  273. if (x != null && (min == null || x < min || double.IsNaN(x.Value)))
  274. {
  275. min = x;
  276. }
  277. return Task.CompletedTask;
  278. },
  279. observer.OnErrorAsync,
  280. async () =>
  281. {
  282. await observer.OnNextAsync(min).ConfigureAwait(false);
  283. await observer.OnCompletedAsync().ConfigureAwait(false);
  284. }
  285. );
  286. }
  287. public static IAsyncObserver<decimal?> MinNullableDecimal(this IAsyncObserver<decimal?> observer)
  288. {
  289. if (observer == null)
  290. throw new ArgumentNullException(nameof(observer));
  291. var min = default(decimal?);
  292. return Create<decimal?>(
  293. x =>
  294. {
  295. if (min == null || x < min)
  296. {
  297. min = x;
  298. }
  299. return Task.CompletedTask;
  300. },
  301. observer.OnErrorAsync,
  302. async () =>
  303. {
  304. await observer.OnNextAsync(min).ConfigureAwait(false);
  305. await observer.OnCompletedAsync().ConfigureAwait(false);
  306. }
  307. );
  308. }
  309. }
  310. }