Skip to content

Commit

Permalink
Optional Linq: Remove ElementAtOrAbsent Int64-based
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Sep 25, 2024
1 parent 88b25d7 commit 2469302
Show file tree
Hide file tree
Showing 8 changed files with 0 additions and 281 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,4 @@ public static Optional<TSource> ElementAtOrAbsent<TSource>(
InnerElementAtOrAbsent(
source ?? throw new ArgumentNullException(nameof(source)),
index);

public static Optional<TSource> ElementAtOrAbsent<TSource>(
this IEnumerable<TSource> source,
long index)
=>
InnerElementAtOrAbsent(
source ?? throw new ArgumentNullException(nameof(source)),
index);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@ IList<TSource> list
=>
list.InnerElementAtOrAbsent_IList(index),

_ =>
source.InnerElementAtOrAbsent_IEnumerable(index)
};

private static Optional<TSource> InnerElementAtOrAbsent<TSource>(
this IEnumerable<TSource> source,
long index)
=>
source switch
{
IReadOnlyList<TSource> list
=>
list.InnerElementAtOrAbsent_IReadOnlyList(index),

IList<TSource> list
=>
list.InnerElementAtOrAbsent_IList(index),

_ =>
source.InnerElementAtOrAbsent_IEnumerable(index)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,4 @@ private static Optional<TSource> InnerElementAtOrAbsent_IEnumerable<TSource>(

return default;
}

private static Optional<TSource> InnerElementAtOrAbsent_IEnumerable<TSource>(
this IEnumerable<TSource> source,
long index)
{
if (index >= 0)
{
using var enumerator = source.GetEnumerator();

if (enumerator.MoveNext())
{
var countdownIndex = index;

do
{
if (countdownIndex == 0)
{
return new(enumerator.Current);
}

countdownIndex--;
}
while (enumerator.MoveNext());
}
}

return default;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,4 @@ private static Optional<TSource> InnerElementAtOrAbsent_IList<TSource>(
int index)
=>
index >= 0 && index < source.Count ? new(source[index]) : default;

private static Optional<TSource> InnerElementAtOrAbsent_IList<TSource>(
this IList<TSource> source,
long index)
=>
index >= 0 && index < source.Count ? new(source[(int)index]) : default;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,4 @@ private static Optional<TSource> InnerElementAtOrAbsent_IReadOnlyList<TSource>(
int index)
=>
index >= 0 && index < source.Count ? new(source[index]) : default;

private static Optional<TSource> InnerElementAtOrAbsent_IReadOnlyList<TSource>(
this IReadOnlyList<TSource> source,
long index)
=>
index >= 0 && index < source.Count ? new(source[(int)index]) : default;
}

0 comments on commit 2469302

Please sign in to comment.