Skip to content

Commit 98c6d85

Browse files
committed
One argument enumerable deconstruct
1 parent 572095d commit 98c6d85

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

CodeJam.Main/CodeJam.Main.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
<Compile Update="Collections\Enumerable\EnumerableExtensions.Deconstruct.generated.cs">
129129
<AutoGen>True</AutoGen>
130130
<DependentUpon>EnumerableExtensions.Deconstruct.tt</DependentUpon>
131+
<DesignTime>True</DesignTime>
131132
</Compile>
132133
<Compile Update="Dates\DateTimeCode.generated.cs">
133134
<AutoGen>True</AutoGen>

CodeJam.Main/Collections/Enumerable/EnumerableExtensions.Deconstruct.generated.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,49 @@ partial class EnumerableExtensions
1919
private const string _listTooShortMsg = "The list is too short.";
2020
private const string _enumTooShortMsg = "The enumerable is too short.";
2121

22+
/// <summary>
23+
/// Deconstructs 1 item of the <paramref name="array"/>.
24+
/// </summary>
25+
[Pure]
26+
public static void Deconstruct<T>(
27+
[NotNull] this T[] array,
28+
out T item1)
29+
{
30+
Code.NotNull(array, nameof (array));
31+
Code.AssertArgument(array.Length >= 1, nameof (array), _arrayTooShortMsg);
32+
33+
item1 = array[0];
34+
}
35+
36+
/// <summary>
37+
/// Deconstructs 1 items of the <paramref name="list"/>.
38+
/// </summary>
39+
[Pure]
40+
public static void Deconstruct<T>(
41+
[NotNull] this IList<T> list,
42+
out T item1)
43+
{
44+
Code.NotNull(list, nameof (list));
45+
Code.AssertArgument(list.Count >= 1, nameof (list), _listTooShortMsg);
46+
47+
item1 = list[0];
48+
}
49+
50+
/// <summary>
51+
/// Deconstructs 1 items of the <paramref name="enumerable"/>.
52+
/// </summary>
53+
[Pure]
54+
public static void Deconstruct<T>(
55+
[NotNull] this IEnumerable<T> enumerable,
56+
out T item1)
57+
{
58+
Code.NotNull(enumerable, nameof (enumerable));
59+
60+
using var enumerator = enumerable.GetEnumerator();
61+
Code.AssertArgument(enumerator.MoveNext(), nameof (enumerable), _enumTooShortMsg);
62+
item1 = enumerator.Current;
63+
}
64+
2265
/// <summary>
2366
/// Deconstructs 2 items of the <paramref name="array"/>.
2467
/// </summary>

CodeJam.Main/Collections/Enumerable/EnumerableExtensions.Deconstruct.tt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ namespace CodeJam.Collections
1313
private const string _enumTooShortMsg = "The enumerable is too short.";
1414

1515
<#
16-
for (var i = 2; i <= 10; i++)
16+
for (var i = 1; i <= 10; i++)
1717
WriteDeconstructMethod(i);
1818
#>
1919
}
2020
}
2121

2222
<#+ private void WriteDeconstructMethod(int numOfParams) {#>
2323
/// <summary>
24-
/// Deconstructs <#=numOfParams#> items of the <paramref name="array"/>.
24+
/// Deconstructs <#=numOfParams#> item<#=numOfParams > 1 ? "s" : ""#> of the <paramref name="array"/>.
2525
/// </summary>
2626
[Pure]
2727
public static void Deconstruct<T>(

0 commit comments

Comments
 (0)