Skip to content

Commit

Permalink
Align semantics to Take and check parameter range
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Oct 3, 2022
1 parent 3bc166e commit 1ef96b3
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions Bonsai.Core/Reactive/Slice.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Xml.Serialization;
using System.ComponentModel;
Expand Down Expand Up @@ -44,17 +44,43 @@ public class Slice : Combinator
public override IObservable<TSource> Process<TSource>(IObservable<TSource> source)
{
var start = Start;
if (start < 0)
{
throw new ArgumentOutOfRangeException(nameof(start));
}

var step = Step;
if (step <= 0)
{
throw new ArgumentOutOfRangeException(nameof(step));
}

var stop = Stop;
return Observable.Defer(() =>
if (stop < 0)
{
throw new ArgumentOutOfRangeException(nameof(stop));
}

return (stop - start) <= 0 ? Observable.Empty<TSource>() : Observable.Create<TSource>(observer =>
{
int i = 0;
var slice = source.Where(_ =>
{
var index = i++;
return index >= start && (index - start) % step == 0;
});
return stop.HasValue ? slice.TakeWhile(_ => i < stop) : slice;
var sliceObserver = Observer.Create<TSource>(
value =>
{
var index = i++;
if (index >= start && (index - start) % step == 0)
{
observer.OnNext(value);
}
if (i >= stop)
{
observer.OnCompleted();
}
},
observer.OnError,
observer.OnCompleted);
return source.SubscribeSafe(sliceObserver);
});
}
}
Expand Down

0 comments on commit 1ef96b3

Please sign in to comment.