Skip to content

Commit

Permalink
Merge pull request #216 from mobile46/master
Browse files Browse the repository at this point in the history
[FirstOrDefault] Fix bug
  • Loading branch information
reegeek committed Nov 1, 2023
2 parents 1f29e36 + 3c27f05 commit 6f00630
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
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

0 comments on commit 6f00630

Please sign in to comment.