Skip to content

Commit

Permalink
Modernize some Ix code.
Browse files Browse the repository at this point in the history
  • Loading branch information
bartdesmet committed May 27, 2019
1 parent 8778a4b commit 5bc815e
Show file tree
Hide file tree
Showing 26 changed files with 78 additions and 229 deletions.
36 changes: 17 additions & 19 deletions Ix.NET/Source/System.Interactive/System/Linq/Operators/Catch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,31 @@ private static IEnumerable<TSource> CatchCore<TSource>(IEnumerable<IEnumerable<T

foreach (var source in sources)
{
using (var e = source.GetEnumerator())
using var e = source.GetEnumerator();
error = null;

while (true)
{
error = null;
var c = default(TSource);

while (true)
try
{
var c = default(TSource);

try
{
if (!e.MoveNext())
break;

c = e.Current;
}
catch (Exception ex)
{
error = ex;
if (!e.MoveNext())
break;
}

yield return c;
c = e.Current;
}

if (error == null)
catch (Exception ex)
{
error = ex;
break;
}

yield return c;
}

if (error == null)
break;
}

if (error != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ public static partial class EnumerableEx
public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
{
if (sources == null)
{
throw new ArgumentNullException(nameof(sources));
}

return ConcatCore(sources);
}
Expand All @@ -33,9 +31,7 @@ public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<IEnumerable<
public static IEnumerable<TSource> Concat<TSource>(params IEnumerable<TSource>[] sources)
{
if (sources == null)
{
throw new ArgumentNullException(nameof(sources));
}

return ConcatCore(sources);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public static partial class EnumerableEx
public static IEnumerable<TResult> Create<TResult>(Func<IEnumerator<TResult>> getEnumerator)
{
if (getEnumerator == null)
{
throw new ArgumentNullException(nameof(getEnumerator));
}

return new AnonymousEnumerable<TResult>(getEnumerator);
}
Expand All @@ -37,9 +35,7 @@ public static IEnumerable<TResult> Create<TResult>(Func<IEnumerator<TResult>> ge
public static IEnumerable<T> Create<T>(Action<IYielder<T>> create)
{
if (create == null)
{
throw new ArgumentNullException(nameof(create));
}

foreach (var x in new Yielder<T>(create))
{
Expand All @@ -51,10 +47,7 @@ private sealed class AnonymousEnumerable<TResult> : IEnumerable<TResult>
{
private readonly Func<IEnumerator<TResult>> _getEnumerator;

public AnonymousEnumerable(Func<IEnumerator<TResult>> getEnumerator)
{
_getEnumerator = getEnumerator;
}
public AnonymousEnumerable(Func<IEnumerator<TResult>> getEnumerator) => _getEnumerator = getEnumerator;

public IEnumerator<TResult> GetEnumerator() => _getEnumerator();

Expand Down
39 changes: 19 additions & 20 deletions Ix.NET/Source/System.Interactive/System/Linq/Operators/Do.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,31 +108,30 @@ public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source,

private static IEnumerable<TSource> DoCore<TSource>(IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
{
using (var e = source.GetEnumerator())
using var e = source.GetEnumerator();

while (true)
{
while (true)
TSource current;
try
{
if (!e.MoveNext())
break;

current = e.Current;
}
catch (Exception ex)
{
var current = default(TSource);
try
{
if (!e.MoveNext())
break;

current = e.Current;
}
catch (Exception ex)
{
onError(ex);
throw;
}

onNext(current);

yield return current;
onError(ex);
throw;
}

onCompleted();
onNext(current);

yield return current;
}

onCompleted();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ public static partial class EnumerableEx
public static IEnumerable<TSource> Expand<TSource>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TSource>> selector)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

if (selector == null)
{
throw new ArgumentNullException(nameof(selector));
}

return ExpandCore(source, selector);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ public static partial class EnumerableEx
public static IEnumerable<TSource> Finally<TSource>(this IEnumerable<TSource> source, Action finallyAction)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

if (finallyAction == null)
{
throw new ArgumentNullException(nameof(finallyAction));
}

return FinallyCore(source, finallyAction);
}
Expand Down
12 changes: 1 addition & 11 deletions Ix.NET/Source/System.Interactive/System/Linq/Operators/For.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,11 @@ public static partial class EnumerableEx
public static IEnumerable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

if (resultSelector == null)
{
throw new ArgumentNullException(nameof(resultSelector));
}

return ForCore(source, resultSelector).Concat();
}

private static IEnumerable<IEnumerable<TResult>> ForCore<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
{
return source.Select(resultSelector);
return source.Select(resultSelector).Concat();
}
}
}
10 changes: 0 additions & 10 deletions Ix.NET/Source/System.Interactive/System/Linq/Operators/ForEach.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@ public static partial class EnumerableEx
public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

if (onNext == null)
{
throw new ArgumentNullException(nameof(onNext));
}

foreach (var item in source)
{
Expand All @@ -41,14 +36,9 @@ public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSo
public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource, int> onNext)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

if (onNext == null)
{
throw new ArgumentNullException(nameof(onNext));
}

var i = 0;
foreach (var item in source)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,11 @@ public static partial class EnumerableEx
public static IEnumerable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
{
if (condition == null)
{
throw new ArgumentNullException(nameof(condition));
}

if (iterate == null)
{
throw new ArgumentNullException(nameof(iterate));
}

if (resultSelector == null)
{
throw new ArgumentNullException(nameof(resultSelector));
}

return GenerateCore(initialState, condition, iterate, resultSelector);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public static partial class EnumerableEx
public static IEnumerable<TSource> Hide<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

return HideCore(source);
}
Expand Down
13 changes: 0 additions & 13 deletions Ix.NET/Source/System.Interactive/System/Linq/Operators/If.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,11 @@ public static partial class EnumerableEx
public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource, IEnumerable<TResult> elseSource)
{
if (condition == null)
{
throw new ArgumentNullException(nameof(condition));
}

if (thenSource == null)
{
throw new ArgumentNullException(nameof(thenSource));
}

if (elseSource == null)
{
throw new ArgumentNullException(nameof(elseSource));
}

return Defer(() => condition() ? thenSource : elseSource);
}
Expand All @@ -47,14 +39,9 @@ public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable
public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource)
{
if (condition == null)
{
throw new ArgumentNullException(nameof(condition));
}

if (thenSource == null)
{
throw new ArgumentNullException(nameof(thenSource));
}

return Defer(() => condition() ? thenSource : Enumerable.Empty<TResult>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ public static partial class EnumerableEx
public static bool IsEmpty<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

return !source.Any();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,24 @@ private static IEnumerable<TSource> OnErrorResumeNextCore<TSource>(IEnumerable<I
{
foreach (var source in sources)
{
using (var innerEnumerator = source.GetEnumerator())
using var innerEnumerator = source.GetEnumerator();

while (true)
{
while (true)
var value = default(TSource);
try
{
var value = default(TSource);
try
{
if (!innerEnumerator.MoveNext())
break;

value = innerEnumerator.Current;
}
catch
{
if (!innerEnumerator.MoveNext())
break;
}

yield return value;
value = innerEnumerator.Current;
}
catch
{
break;
}

yield return value;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public static partial class EnumerableEx
public static IBuffer<TSource> Publish<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

return new PublishedBuffer<TSource>(source.GetEnumerator());
}
Expand All @@ -54,14 +52,9 @@ public static IBuffer<TSource> Publish<TSource>(this IEnumerable<TSource> source
public static IEnumerable<TResult> Publish<TSource, TResult>(this IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

if (selector == null)
{
throw new ArgumentNullException(nameof(selector));
}

return Create(() => selector(source.Publish()).GetEnumerator());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count)
public static IEnumerable<TSource> Repeat<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

return RepeatCore(source);
}
Expand All @@ -60,9 +58,7 @@ public static IEnumerable<TSource> Repeat<TSource>(this IEnumerable<TSource> sou
public static IEnumerable<TSource> Repeat<TSource>(this IEnumerable<TSource> source, int count)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

if (count < 0)
{
Expand Down
Loading

0 comments on commit 5bc815e

Please sign in to comment.