Sum.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. using System;
  2. using System.Threading;
  3. using Cysharp.Threading.Tasks.Internal;
  4. namespace Cysharp.Threading.Tasks.Linq
  5. {
  6. public static partial class UniTaskAsyncEnumerable
  7. {
  8. public static UniTask<Int32> SumAsync(this IUniTaskAsyncEnumerable<Int32> source, CancellationToken cancellationToken = default)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return Sum.SumAsync(source, cancellationToken);
  12. }
  13. public static UniTask<Int32> SumAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32> selector, CancellationToken cancellationToken = default)
  14. {
  15. Error.ThrowArgumentNullException(source, nameof(source));
  16. Error.ThrowArgumentNullException(source, nameof(selector));
  17. return Sum.SumAsync(source, selector, cancellationToken);
  18. }
  19. public static UniTask<Int32> SumAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int32>> selector, CancellationToken cancellationToken = default)
  20. {
  21. Error.ThrowArgumentNullException(source, nameof(source));
  22. Error.ThrowArgumentNullException(source, nameof(selector));
  23. return Sum.SumAwaitAsync(source, selector, cancellationToken);
  24. }
  25. public static UniTask<Int32> SumAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int32>> selector, CancellationToken cancellationToken = default)
  26. {
  27. Error.ThrowArgumentNullException(source, nameof(source));
  28. Error.ThrowArgumentNullException(source, nameof(selector));
  29. return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken);
  30. }
  31. public static UniTask<Int64> SumAsync(this IUniTaskAsyncEnumerable<Int64> source, CancellationToken cancellationToken = default)
  32. {
  33. Error.ThrowArgumentNullException(source, nameof(source));
  34. return Sum.SumAsync(source, cancellationToken);
  35. }
  36. public static UniTask<Int64> SumAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int64> selector, CancellationToken cancellationToken = default)
  37. {
  38. Error.ThrowArgumentNullException(source, nameof(source));
  39. Error.ThrowArgumentNullException(source, nameof(selector));
  40. return Sum.SumAsync(source, selector, cancellationToken);
  41. }
  42. public static UniTask<Int64> SumAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int64>> selector, CancellationToken cancellationToken = default)
  43. {
  44. Error.ThrowArgumentNullException(source, nameof(source));
  45. Error.ThrowArgumentNullException(source, nameof(selector));
  46. return Sum.SumAwaitAsync(source, selector, cancellationToken);
  47. }
  48. public static UniTask<Int64> SumAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int64>> selector, CancellationToken cancellationToken = default)
  49. {
  50. Error.ThrowArgumentNullException(source, nameof(source));
  51. Error.ThrowArgumentNullException(source, nameof(selector));
  52. return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken);
  53. }
  54. public static UniTask<Single> SumAsync(this IUniTaskAsyncEnumerable<Single> source, CancellationToken cancellationToken = default)
  55. {
  56. Error.ThrowArgumentNullException(source, nameof(source));
  57. return Sum.SumAsync(source, cancellationToken);
  58. }
  59. public static UniTask<Single> SumAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Single> selector, CancellationToken cancellationToken = default)
  60. {
  61. Error.ThrowArgumentNullException(source, nameof(source));
  62. Error.ThrowArgumentNullException(source, nameof(selector));
  63. return Sum.SumAsync(source, selector, cancellationToken);
  64. }
  65. public static UniTask<Single> SumAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Single>> selector, CancellationToken cancellationToken = default)
  66. {
  67. Error.ThrowArgumentNullException(source, nameof(source));
  68. Error.ThrowArgumentNullException(source, nameof(selector));
  69. return Sum.SumAwaitAsync(source, selector, cancellationToken);
  70. }
  71. public static UniTask<Single> SumAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Single>> selector, CancellationToken cancellationToken = default)
  72. {
  73. Error.ThrowArgumentNullException(source, nameof(source));
  74. Error.ThrowArgumentNullException(source, nameof(selector));
  75. return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken);
  76. }
  77. public static UniTask<Double> SumAsync(this IUniTaskAsyncEnumerable<Double> source, CancellationToken cancellationToken = default)
  78. {
  79. Error.ThrowArgumentNullException(source, nameof(source));
  80. return Sum.SumAsync(source, cancellationToken);
  81. }
  82. public static UniTask<Double> SumAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Double> selector, CancellationToken cancellationToken = default)
  83. {
  84. Error.ThrowArgumentNullException(source, nameof(source));
  85. Error.ThrowArgumentNullException(source, nameof(selector));
  86. return Sum.SumAsync(source, selector, cancellationToken);
  87. }
  88. public static UniTask<Double> SumAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Double>> selector, CancellationToken cancellationToken = default)
  89. {
  90. Error.ThrowArgumentNullException(source, nameof(source));
  91. Error.ThrowArgumentNullException(source, nameof(selector));
  92. return Sum.SumAwaitAsync(source, selector, cancellationToken);
  93. }
  94. public static UniTask<Double> SumAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Double>> selector, CancellationToken cancellationToken = default)
  95. {
  96. Error.ThrowArgumentNullException(source, nameof(source));
  97. Error.ThrowArgumentNullException(source, nameof(selector));
  98. return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken);
  99. }
  100. public static UniTask<Decimal> SumAsync(this IUniTaskAsyncEnumerable<Decimal> source, CancellationToken cancellationToken = default)
  101. {
  102. Error.ThrowArgumentNullException(source, nameof(source));
  103. return Sum.SumAsync(source, cancellationToken);
  104. }
  105. public static UniTask<Decimal> SumAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Decimal> selector, CancellationToken cancellationToken = default)
  106. {
  107. Error.ThrowArgumentNullException(source, nameof(source));
  108. Error.ThrowArgumentNullException(source, nameof(selector));
  109. return Sum.SumAsync(source, selector, cancellationToken);
  110. }
  111. public static UniTask<Decimal> SumAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Decimal>> selector, CancellationToken cancellationToken = default)
  112. {
  113. Error.ThrowArgumentNullException(source, nameof(source));
  114. Error.ThrowArgumentNullException(source, nameof(selector));
  115. return Sum.SumAwaitAsync(source, selector, cancellationToken);
  116. }
  117. public static UniTask<Decimal> SumAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Decimal>> selector, CancellationToken cancellationToken = default)
  118. {
  119. Error.ThrowArgumentNullException(source, nameof(source));
  120. Error.ThrowArgumentNullException(source, nameof(selector));
  121. return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken);
  122. }
  123. public static UniTask<Int32?> SumAsync(this IUniTaskAsyncEnumerable<Int32?> source, CancellationToken cancellationToken = default)
  124. {
  125. Error.ThrowArgumentNullException(source, nameof(source));
  126. return Sum.SumAsync(source, cancellationToken);
  127. }
  128. public static UniTask<Int32?> SumAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32?> selector, CancellationToken cancellationToken = default)
  129. {
  130. Error.ThrowArgumentNullException(source, nameof(source));
  131. Error.ThrowArgumentNullException(source, nameof(selector));
  132. return Sum.SumAsync(source, selector, cancellationToken);
  133. }
  134. public static UniTask<Int32?> SumAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int32?>> selector, CancellationToken cancellationToken = default)
  135. {
  136. Error.ThrowArgumentNullException(source, nameof(source));
  137. Error.ThrowArgumentNullException(source, nameof(selector));
  138. return Sum.SumAwaitAsync(source, selector, cancellationToken);
  139. }
  140. public static UniTask<Int32?> SumAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int32?>> selector, CancellationToken cancellationToken = default)
  141. {
  142. Error.ThrowArgumentNullException(source, nameof(source));
  143. Error.ThrowArgumentNullException(source, nameof(selector));
  144. return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken);
  145. }
  146. public static UniTask<Int64?> SumAsync(this IUniTaskAsyncEnumerable<Int64?> source, CancellationToken cancellationToken = default)
  147. {
  148. Error.ThrowArgumentNullException(source, nameof(source));
  149. return Sum.SumAsync(source, cancellationToken);
  150. }
  151. public static UniTask<Int64?> SumAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int64?> selector, CancellationToken cancellationToken = default)
  152. {
  153. Error.ThrowArgumentNullException(source, nameof(source));
  154. Error.ThrowArgumentNullException(source, nameof(selector));
  155. return Sum.SumAsync(source, selector, cancellationToken);
  156. }
  157. public static UniTask<Int64?> SumAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int64?>> selector, CancellationToken cancellationToken = default)
  158. {
  159. Error.ThrowArgumentNullException(source, nameof(source));
  160. Error.ThrowArgumentNullException(source, nameof(selector));
  161. return Sum.SumAwaitAsync(source, selector, cancellationToken);
  162. }
  163. public static UniTask<Int64?> SumAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int64?>> selector, CancellationToken cancellationToken = default)
  164. {
  165. Error.ThrowArgumentNullException(source, nameof(source));
  166. Error.ThrowArgumentNullException(source, nameof(selector));
  167. return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken);
  168. }
  169. public static UniTask<Single?> SumAsync(this IUniTaskAsyncEnumerable<Single?> source, CancellationToken cancellationToken = default)
  170. {
  171. Error.ThrowArgumentNullException(source, nameof(source));
  172. return Sum.SumAsync(source, cancellationToken);
  173. }
  174. public static UniTask<Single?> SumAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Single?> selector, CancellationToken cancellationToken = default)
  175. {
  176. Error.ThrowArgumentNullException(source, nameof(source));
  177. Error.ThrowArgumentNullException(source, nameof(selector));
  178. return Sum.SumAsync(source, selector, cancellationToken);
  179. }
  180. public static UniTask<Single?> SumAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Single?>> selector, CancellationToken cancellationToken = default)
  181. {
  182. Error.ThrowArgumentNullException(source, nameof(source));
  183. Error.ThrowArgumentNullException(source, nameof(selector));
  184. return Sum.SumAwaitAsync(source, selector, cancellationToken);
  185. }
  186. public static UniTask<Single?> SumAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Single?>> selector, CancellationToken cancellationToken = default)
  187. {
  188. Error.ThrowArgumentNullException(source, nameof(source));
  189. Error.ThrowArgumentNullException(source, nameof(selector));
  190. return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken);
  191. }
  192. public static UniTask<Double?> SumAsync(this IUniTaskAsyncEnumerable<Double?> source, CancellationToken cancellationToken = default)
  193. {
  194. Error.ThrowArgumentNullException(source, nameof(source));
  195. return Sum.SumAsync(source, cancellationToken);
  196. }
  197. public static UniTask<Double?> SumAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Double?> selector, CancellationToken cancellationToken = default)
  198. {
  199. Error.ThrowArgumentNullException(source, nameof(source));
  200. Error.ThrowArgumentNullException(source, nameof(selector));
  201. return Sum.SumAsync(source, selector, cancellationToken);
  202. }
  203. public static UniTask<Double?> SumAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Double?>> selector, CancellationToken cancellationToken = default)
  204. {
  205. Error.ThrowArgumentNullException(source, nameof(source));
  206. Error.ThrowArgumentNullException(source, nameof(selector));
  207. return Sum.SumAwaitAsync(source, selector, cancellationToken);
  208. }
  209. public static UniTask<Double?> SumAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Double?>> selector, CancellationToken cancellationToken = default)
  210. {
  211. Error.ThrowArgumentNullException(source, nameof(source));
  212. Error.ThrowArgumentNullException(source, nameof(selector));
  213. return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken);
  214. }
  215. public static UniTask<Decimal?> SumAsync(this IUniTaskAsyncEnumerable<Decimal?> source, CancellationToken cancellationToken = default)
  216. {
  217. Error.ThrowArgumentNullException(source, nameof(source));
  218. return Sum.SumAsync(source, cancellationToken);
  219. }
  220. public static UniTask<Decimal?> SumAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Decimal?> selector, CancellationToken cancellationToken = default)
  221. {
  222. Error.ThrowArgumentNullException(source, nameof(source));
  223. Error.ThrowArgumentNullException(source, nameof(selector));
  224. return Sum.SumAsync(source, selector, cancellationToken);
  225. }
  226. public static UniTask<Decimal?> SumAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Decimal?>> selector, CancellationToken cancellationToken = default)
  227. {
  228. Error.ThrowArgumentNullException(source, nameof(source));
  229. Error.ThrowArgumentNullException(source, nameof(selector));
  230. return Sum.SumAwaitAsync(source, selector, cancellationToken);
  231. }
  232. public static UniTask<Decimal?> SumAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Decimal?>> selector, CancellationToken cancellationToken = default)
  233. {
  234. Error.ThrowArgumentNullException(source, nameof(source));
  235. Error.ThrowArgumentNullException(source, nameof(selector));
  236. return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken);
  237. }
  238. }
  239. internal static class Sum
  240. {
  241. public static async UniTask<Int32> SumAsync(IUniTaskAsyncEnumerable<Int32> source, CancellationToken cancellationToken)
  242. {
  243. Int32 sum = default;
  244. var e = source.GetAsyncEnumerator(cancellationToken);
  245. try
  246. {
  247. while (await e.MoveNextAsync())
  248. {
  249. sum += e.Current;
  250. }
  251. }
  252. finally
  253. {
  254. if (e != null)
  255. {
  256. await e.DisposeAsync();
  257. }
  258. }
  259. return sum;
  260. }
  261. public static async UniTask<Int32> SumAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32> selector, CancellationToken cancellationToken)
  262. {
  263. Int32 sum = default;
  264. var e = source.GetAsyncEnumerator(cancellationToken);
  265. try
  266. {
  267. while (await e.MoveNextAsync())
  268. {
  269. sum += selector(e.Current);
  270. }
  271. }
  272. finally
  273. {
  274. if (e != null)
  275. {
  276. await e.DisposeAsync();
  277. }
  278. }
  279. return sum;
  280. }
  281. public static async UniTask<Int32> SumAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int32>> selector, CancellationToken cancellationToken)
  282. {
  283. Int32 sum = default;
  284. var e = source.GetAsyncEnumerator(cancellationToken);
  285. try
  286. {
  287. while (await e.MoveNextAsync())
  288. {
  289. sum += (await selector(e.Current));
  290. }
  291. }
  292. finally
  293. {
  294. if (e != null)
  295. {
  296. await e.DisposeAsync();
  297. }
  298. }
  299. return sum;
  300. }
  301. public static async UniTask<Int32> SumAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int32>> selector, CancellationToken cancellationToken)
  302. {
  303. Int32 sum = default;
  304. var e = source.GetAsyncEnumerator(cancellationToken);
  305. try
  306. {
  307. while (await e.MoveNextAsync())
  308. {
  309. sum += (await selector(e.Current, cancellationToken));
  310. }
  311. }
  312. finally
  313. {
  314. if (e != null)
  315. {
  316. await e.DisposeAsync();
  317. }
  318. }
  319. return sum;
  320. }
  321. public static async UniTask<Int64> SumAsync(IUniTaskAsyncEnumerable<Int64> source, CancellationToken cancellationToken)
  322. {
  323. Int64 sum = default;
  324. var e = source.GetAsyncEnumerator(cancellationToken);
  325. try
  326. {
  327. while (await e.MoveNextAsync())
  328. {
  329. sum += e.Current;
  330. }
  331. }
  332. finally
  333. {
  334. if (e != null)
  335. {
  336. await e.DisposeAsync();
  337. }
  338. }
  339. return sum;
  340. }
  341. public static async UniTask<Int64> SumAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int64> selector, CancellationToken cancellationToken)
  342. {
  343. Int64 sum = default;
  344. var e = source.GetAsyncEnumerator(cancellationToken);
  345. try
  346. {
  347. while (await e.MoveNextAsync())
  348. {
  349. sum += selector(e.Current);
  350. }
  351. }
  352. finally
  353. {
  354. if (e != null)
  355. {
  356. await e.DisposeAsync();
  357. }
  358. }
  359. return sum;
  360. }
  361. public static async UniTask<Int64> SumAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int64>> selector, CancellationToken cancellationToken)
  362. {
  363. Int64 sum = default;
  364. var e = source.GetAsyncEnumerator(cancellationToken);
  365. try
  366. {
  367. while (await e.MoveNextAsync())
  368. {
  369. sum += (await selector(e.Current));
  370. }
  371. }
  372. finally
  373. {
  374. if (e != null)
  375. {
  376. await e.DisposeAsync();
  377. }
  378. }
  379. return sum;
  380. }
  381. public static async UniTask<Int64> SumAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int64>> selector, CancellationToken cancellationToken)
  382. {
  383. Int64 sum = default;
  384. var e = source.GetAsyncEnumerator(cancellationToken);
  385. try
  386. {
  387. while (await e.MoveNextAsync())
  388. {
  389. sum += (await selector(e.Current, cancellationToken));
  390. }
  391. }
  392. finally
  393. {
  394. if (e != null)
  395. {
  396. await e.DisposeAsync();
  397. }
  398. }
  399. return sum;
  400. }
  401. public static async UniTask<Single> SumAsync(IUniTaskAsyncEnumerable<Single> source, CancellationToken cancellationToken)
  402. {
  403. Single sum = default;
  404. var e = source.GetAsyncEnumerator(cancellationToken);
  405. try
  406. {
  407. while (await e.MoveNextAsync())
  408. {
  409. sum += e.Current;
  410. }
  411. }
  412. finally
  413. {
  414. if (e != null)
  415. {
  416. await e.DisposeAsync();
  417. }
  418. }
  419. return sum;
  420. }
  421. public static async UniTask<Single> SumAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Single> selector, CancellationToken cancellationToken)
  422. {
  423. Single sum = default;
  424. var e = source.GetAsyncEnumerator(cancellationToken);
  425. try
  426. {
  427. while (await e.MoveNextAsync())
  428. {
  429. sum += selector(e.Current);
  430. }
  431. }
  432. finally
  433. {
  434. if (e != null)
  435. {
  436. await e.DisposeAsync();
  437. }
  438. }
  439. return sum;
  440. }
  441. public static async UniTask<Single> SumAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Single>> selector, CancellationToken cancellationToken)
  442. {
  443. Single sum = default;
  444. var e = source.GetAsyncEnumerator(cancellationToken);
  445. try
  446. {
  447. while (await e.MoveNextAsync())
  448. {
  449. sum += (await selector(e.Current));
  450. }
  451. }
  452. finally
  453. {
  454. if (e != null)
  455. {
  456. await e.DisposeAsync();
  457. }
  458. }
  459. return sum;
  460. }
  461. public static async UniTask<Single> SumAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Single>> selector, CancellationToken cancellationToken)
  462. {
  463. Single sum = default;
  464. var e = source.GetAsyncEnumerator(cancellationToken);
  465. try
  466. {
  467. while (await e.MoveNextAsync())
  468. {
  469. sum += (await selector(e.Current, cancellationToken));
  470. }
  471. }
  472. finally
  473. {
  474. if (e != null)
  475. {
  476. await e.DisposeAsync();
  477. }
  478. }
  479. return sum;
  480. }
  481. public static async UniTask<Double> SumAsync(IUniTaskAsyncEnumerable<Double> source, CancellationToken cancellationToken)
  482. {
  483. Double sum = default;
  484. var e = source.GetAsyncEnumerator(cancellationToken);
  485. try
  486. {
  487. while (await e.MoveNextAsync())
  488. {
  489. sum += e.Current;
  490. }
  491. }
  492. finally
  493. {
  494. if (e != null)
  495. {
  496. await e.DisposeAsync();
  497. }
  498. }
  499. return sum;
  500. }
  501. public static async UniTask<Double> SumAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Double> selector, CancellationToken cancellationToken)
  502. {
  503. Double sum = default;
  504. var e = source.GetAsyncEnumerator(cancellationToken);
  505. try
  506. {
  507. while (await e.MoveNextAsync())
  508. {
  509. sum += selector(e.Current);
  510. }
  511. }
  512. finally
  513. {
  514. if (e != null)
  515. {
  516. await e.DisposeAsync();
  517. }
  518. }
  519. return sum;
  520. }
  521. public static async UniTask<Double> SumAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Double>> selector, CancellationToken cancellationToken)
  522. {
  523. Double sum = default;
  524. var e = source.GetAsyncEnumerator(cancellationToken);
  525. try
  526. {
  527. while (await e.MoveNextAsync())
  528. {
  529. sum += (await selector(e.Current));
  530. }
  531. }
  532. finally
  533. {
  534. if (e != null)
  535. {
  536. await e.DisposeAsync();
  537. }
  538. }
  539. return sum;
  540. }
  541. public static async UniTask<Double> SumAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Double>> selector, CancellationToken cancellationToken)
  542. {
  543. Double sum = default;
  544. var e = source.GetAsyncEnumerator(cancellationToken);
  545. try
  546. {
  547. while (await e.MoveNextAsync())
  548. {
  549. sum += (await selector(e.Current, cancellationToken));
  550. }
  551. }
  552. finally
  553. {
  554. if (e != null)
  555. {
  556. await e.DisposeAsync();
  557. }
  558. }
  559. return sum;
  560. }
  561. public static async UniTask<Decimal> SumAsync(IUniTaskAsyncEnumerable<Decimal> source, CancellationToken cancellationToken)
  562. {
  563. Decimal sum = default;
  564. var e = source.GetAsyncEnumerator(cancellationToken);
  565. try
  566. {
  567. while (await e.MoveNextAsync())
  568. {
  569. sum += e.Current;
  570. }
  571. }
  572. finally
  573. {
  574. if (e != null)
  575. {
  576. await e.DisposeAsync();
  577. }
  578. }
  579. return sum;
  580. }
  581. public static async UniTask<Decimal> SumAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Decimal> selector, CancellationToken cancellationToken)
  582. {
  583. Decimal sum = default;
  584. var e = source.GetAsyncEnumerator(cancellationToken);
  585. try
  586. {
  587. while (await e.MoveNextAsync())
  588. {
  589. sum += selector(e.Current);
  590. }
  591. }
  592. finally
  593. {
  594. if (e != null)
  595. {
  596. await e.DisposeAsync();
  597. }
  598. }
  599. return sum;
  600. }
  601. public static async UniTask<Decimal> SumAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Decimal>> selector, CancellationToken cancellationToken)
  602. {
  603. Decimal sum = default;
  604. var e = source.GetAsyncEnumerator(cancellationToken);
  605. try
  606. {
  607. while (await e.MoveNextAsync())
  608. {
  609. sum += (await selector(e.Current));
  610. }
  611. }
  612. finally
  613. {
  614. if (e != null)
  615. {
  616. await e.DisposeAsync();
  617. }
  618. }
  619. return sum;
  620. }
  621. public static async UniTask<Decimal> SumAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Decimal>> selector, CancellationToken cancellationToken)
  622. {
  623. Decimal sum = default;
  624. var e = source.GetAsyncEnumerator(cancellationToken);
  625. try
  626. {
  627. while (await e.MoveNextAsync())
  628. {
  629. sum += (await selector(e.Current, cancellationToken));
  630. }
  631. }
  632. finally
  633. {
  634. if (e != null)
  635. {
  636. await e.DisposeAsync();
  637. }
  638. }
  639. return sum;
  640. }
  641. public static async UniTask<Int32?> SumAsync(IUniTaskAsyncEnumerable<Int32?> source, CancellationToken cancellationToken)
  642. {
  643. Int32? sum = default;
  644. var e = source.GetAsyncEnumerator(cancellationToken);
  645. try
  646. {
  647. while (await e.MoveNextAsync())
  648. {
  649. sum += e.Current.GetValueOrDefault();
  650. }
  651. }
  652. finally
  653. {
  654. if (e != null)
  655. {
  656. await e.DisposeAsync();
  657. }
  658. }
  659. return sum;
  660. }
  661. public static async UniTask<Int32?> SumAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32?> selector, CancellationToken cancellationToken)
  662. {
  663. Int32? sum = default;
  664. var e = source.GetAsyncEnumerator(cancellationToken);
  665. try
  666. {
  667. while (await e.MoveNextAsync())
  668. {
  669. sum += selector(e.Current).GetValueOrDefault();
  670. }
  671. }
  672. finally
  673. {
  674. if (e != null)
  675. {
  676. await e.DisposeAsync();
  677. }
  678. }
  679. return sum;
  680. }
  681. public static async UniTask<Int32?> SumAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int32?>> selector, CancellationToken cancellationToken)
  682. {
  683. Int32? sum = default;
  684. var e = source.GetAsyncEnumerator(cancellationToken);
  685. try
  686. {
  687. while (await e.MoveNextAsync())
  688. {
  689. sum += (await selector(e.Current)).GetValueOrDefault();
  690. }
  691. }
  692. finally
  693. {
  694. if (e != null)
  695. {
  696. await e.DisposeAsync();
  697. }
  698. }
  699. return sum;
  700. }
  701. public static async UniTask<Int32?> SumAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int32?>> selector, CancellationToken cancellationToken)
  702. {
  703. Int32? sum = default;
  704. var e = source.GetAsyncEnumerator(cancellationToken);
  705. try
  706. {
  707. while (await e.MoveNextAsync())
  708. {
  709. sum += (await selector(e.Current, cancellationToken)).GetValueOrDefault();
  710. }
  711. }
  712. finally
  713. {
  714. if (e != null)
  715. {
  716. await e.DisposeAsync();
  717. }
  718. }
  719. return sum;
  720. }
  721. public static async UniTask<Int64?> SumAsync(IUniTaskAsyncEnumerable<Int64?> source, CancellationToken cancellationToken)
  722. {
  723. Int64? sum = default;
  724. var e = source.GetAsyncEnumerator(cancellationToken);
  725. try
  726. {
  727. while (await e.MoveNextAsync())
  728. {
  729. sum += e.Current.GetValueOrDefault();
  730. }
  731. }
  732. finally
  733. {
  734. if (e != null)
  735. {
  736. await e.DisposeAsync();
  737. }
  738. }
  739. return sum;
  740. }
  741. public static async UniTask<Int64?> SumAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int64?> selector, CancellationToken cancellationToken)
  742. {
  743. Int64? sum = default;
  744. var e = source.GetAsyncEnumerator(cancellationToken);
  745. try
  746. {
  747. while (await e.MoveNextAsync())
  748. {
  749. sum += selector(e.Current).GetValueOrDefault();
  750. }
  751. }
  752. finally
  753. {
  754. if (e != null)
  755. {
  756. await e.DisposeAsync();
  757. }
  758. }
  759. return sum;
  760. }
  761. public static async UniTask<Int64?> SumAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int64?>> selector, CancellationToken cancellationToken)
  762. {
  763. Int64? sum = default;
  764. var e = source.GetAsyncEnumerator(cancellationToken);
  765. try
  766. {
  767. while (await e.MoveNextAsync())
  768. {
  769. sum += (await selector(e.Current)).GetValueOrDefault();
  770. }
  771. }
  772. finally
  773. {
  774. if (e != null)
  775. {
  776. await e.DisposeAsync();
  777. }
  778. }
  779. return sum;
  780. }
  781. public static async UniTask<Int64?> SumAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int64?>> selector, CancellationToken cancellationToken)
  782. {
  783. Int64? sum = default;
  784. var e = source.GetAsyncEnumerator(cancellationToken);
  785. try
  786. {
  787. while (await e.MoveNextAsync())
  788. {
  789. sum += (await selector(e.Current, cancellationToken)).GetValueOrDefault();
  790. }
  791. }
  792. finally
  793. {
  794. if (e != null)
  795. {
  796. await e.DisposeAsync();
  797. }
  798. }
  799. return sum;
  800. }
  801. public static async UniTask<Single?> SumAsync(IUniTaskAsyncEnumerable<Single?> source, CancellationToken cancellationToken)
  802. {
  803. Single? sum = default;
  804. var e = source.GetAsyncEnumerator(cancellationToken);
  805. try
  806. {
  807. while (await e.MoveNextAsync())
  808. {
  809. sum += e.Current.GetValueOrDefault();
  810. }
  811. }
  812. finally
  813. {
  814. if (e != null)
  815. {
  816. await e.DisposeAsync();
  817. }
  818. }
  819. return sum;
  820. }
  821. public static async UniTask<Single?> SumAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Single?> selector, CancellationToken cancellationToken)
  822. {
  823. Single? sum = default;
  824. var e = source.GetAsyncEnumerator(cancellationToken);
  825. try
  826. {
  827. while (await e.MoveNextAsync())
  828. {
  829. sum += selector(e.Current).GetValueOrDefault();
  830. }
  831. }
  832. finally
  833. {
  834. if (e != null)
  835. {
  836. await e.DisposeAsync();
  837. }
  838. }
  839. return sum;
  840. }
  841. public static async UniTask<Single?> SumAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Single?>> selector, CancellationToken cancellationToken)
  842. {
  843. Single? sum = default;
  844. var e = source.GetAsyncEnumerator(cancellationToken);
  845. try
  846. {
  847. while (await e.MoveNextAsync())
  848. {
  849. sum += (await selector(e.Current)).GetValueOrDefault();
  850. }
  851. }
  852. finally
  853. {
  854. if (e != null)
  855. {
  856. await e.DisposeAsync();
  857. }
  858. }
  859. return sum;
  860. }
  861. public static async UniTask<Single?> SumAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Single?>> selector, CancellationToken cancellationToken)
  862. {
  863. Single? sum = default;
  864. var e = source.GetAsyncEnumerator(cancellationToken);
  865. try
  866. {
  867. while (await e.MoveNextAsync())
  868. {
  869. sum += (await selector(e.Current, cancellationToken)).GetValueOrDefault();
  870. }
  871. }
  872. finally
  873. {
  874. if (e != null)
  875. {
  876. await e.DisposeAsync();
  877. }
  878. }
  879. return sum;
  880. }
  881. public static async UniTask<Double?> SumAsync(IUniTaskAsyncEnumerable<Double?> source, CancellationToken cancellationToken)
  882. {
  883. Double? sum = default;
  884. var e = source.GetAsyncEnumerator(cancellationToken);
  885. try
  886. {
  887. while (await e.MoveNextAsync())
  888. {
  889. sum += e.Current.GetValueOrDefault();
  890. }
  891. }
  892. finally
  893. {
  894. if (e != null)
  895. {
  896. await e.DisposeAsync();
  897. }
  898. }
  899. return sum;
  900. }
  901. public static async UniTask<Double?> SumAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Double?> selector, CancellationToken cancellationToken)
  902. {
  903. Double? sum = default;
  904. var e = source.GetAsyncEnumerator(cancellationToken);
  905. try
  906. {
  907. while (await e.MoveNextAsync())
  908. {
  909. sum += selector(e.Current).GetValueOrDefault();
  910. }
  911. }
  912. finally
  913. {
  914. if (e != null)
  915. {
  916. await e.DisposeAsync();
  917. }
  918. }
  919. return sum;
  920. }
  921. public static async UniTask<Double?> SumAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Double?>> selector, CancellationToken cancellationToken)
  922. {
  923. Double? sum = default;
  924. var e = source.GetAsyncEnumerator(cancellationToken);
  925. try
  926. {
  927. while (await e.MoveNextAsync())
  928. {
  929. sum += (await selector(e.Current)).GetValueOrDefault();
  930. }
  931. }
  932. finally
  933. {
  934. if (e != null)
  935. {
  936. await e.DisposeAsync();
  937. }
  938. }
  939. return sum;
  940. }
  941. public static async UniTask<Double?> SumAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Double?>> selector, CancellationToken cancellationToken)
  942. {
  943. Double? sum = default;
  944. var e = source.GetAsyncEnumerator(cancellationToken);
  945. try
  946. {
  947. while (await e.MoveNextAsync())
  948. {
  949. sum += (await selector(e.Current, cancellationToken)).GetValueOrDefault();
  950. }
  951. }
  952. finally
  953. {
  954. if (e != null)
  955. {
  956. await e.DisposeAsync();
  957. }
  958. }
  959. return sum;
  960. }
  961. public static async UniTask<Decimal?> SumAsync(IUniTaskAsyncEnumerable<Decimal?> source, CancellationToken cancellationToken)
  962. {
  963. Decimal? sum = default;
  964. var e = source.GetAsyncEnumerator(cancellationToken);
  965. try
  966. {
  967. while (await e.MoveNextAsync())
  968. {
  969. sum += e.Current.GetValueOrDefault();
  970. }
  971. }
  972. finally
  973. {
  974. if (e != null)
  975. {
  976. await e.DisposeAsync();
  977. }
  978. }
  979. return sum;
  980. }
  981. public static async UniTask<Decimal?> SumAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Decimal?> selector, CancellationToken cancellationToken)
  982. {
  983. Decimal? sum = default;
  984. var e = source.GetAsyncEnumerator(cancellationToken);
  985. try
  986. {
  987. while (await e.MoveNextAsync())
  988. {
  989. sum += selector(e.Current).GetValueOrDefault();
  990. }
  991. }
  992. finally
  993. {
  994. if (e != null)
  995. {
  996. await e.DisposeAsync();
  997. }
  998. }
  999. return sum;
  1000. }
  1001. public static async UniTask<Decimal?> SumAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Decimal?>> selector, CancellationToken cancellationToken)
  1002. {
  1003. Decimal? sum = default;
  1004. var e = source.GetAsyncEnumerator(cancellationToken);
  1005. try
  1006. {
  1007. while (await e.MoveNextAsync())
  1008. {
  1009. sum += (await selector(e.Current)).GetValueOrDefault();
  1010. }
  1011. }
  1012. finally
  1013. {
  1014. if (e != null)
  1015. {
  1016. await e.DisposeAsync();
  1017. }
  1018. }
  1019. return sum;
  1020. }
  1021. public static async UniTask<Decimal?> SumAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Decimal?>> selector, CancellationToken cancellationToken)
  1022. {
  1023. Decimal? sum = default;
  1024. var e = source.GetAsyncEnumerator(cancellationToken);
  1025. try
  1026. {
  1027. while (await e.MoveNextAsync())
  1028. {
  1029. sum += (await selector(e.Current, cancellationToken)).GetValueOrDefault();
  1030. }
  1031. }
  1032. finally
  1033. {
  1034. if (e != null)
  1035. {
  1036. await e.DisposeAsync();
  1037. }
  1038. }
  1039. return sum;
  1040. }
  1041. }
  1042. }