Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FirstOrDefault bug #216

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/StructLinq.Tests/FirstOrDefaultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public void ShouldReturnDefaultZeroAlloc()
StructEnumerable.Empty<int>().FirstOrDefault(x=>x).Should().Be(default);
}


[Fact]
public void ShouldReturnFirstElementWithFunc()
{
Expand All @@ -62,5 +61,16 @@ public void ShouldReturnFirstElementWithFuncZeroAlloc()
.Should()
.Be(6);
}

[Fact]
public void ShouldReturnDefaultValue()
{
var array = Enumerable.Range(0, 10)
.ToArray()
.ToStructEnumerable()
.FirstOrDefault(x => x == 11)
.Should()
.Be(default);
}
}
}
19 changes: 14 additions & 5 deletions src/StructLinq/First/StructCollection.First.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ public static bool TryFirst<T, TCollection, TEnumerator>(this TCollection collec
return false;
for (int i = 0; i < collection.Count; i++)
{
first = collection.Get(i);
if (predicate(first))
var current = collection.Get(i);
if (predicate(current))
{
first = current;
return true;
}
}
return false;
}
Expand All @@ -120,9 +123,12 @@ public static bool TryFirst<T, TEnumerator>(this IStructCollection<T, TEnumerato
return false;
for (int i = 0; i < collection.Count; i++)
{
first = collection.Get(i);
if (predicate(first))
var current = collection.Get(i);
if (predicate(current))
{
first = current;
return true;
}
}
return false;
}
Expand All @@ -137,9 +143,12 @@ public static bool TryFirst<T, TCollection, TEnumerator, TFunc>(this TCollection
return false;
for (int i = 0; i < collection.Count; i++)
{
first = collection.Get(i);
var current = collection.Get(i);
if (predicate.Eval(first))
{
first = current;
return true;
}
}
return false;
}
Expand Down