ReactiveX / RxScala

RxScala – Reactive Extensions for Scala – a library for composing asynchronous and event-based programs using observable sequences
Apache License 2.0
888 stars 110 forks source link

reduce() fails if stream is empty #156

Closed bpitman closed 9 years ago

bpitman commented 9 years ago

2 items

scala> Observable.from(ListInt).reduce( + ).toBlocking.toList res39: List[Int] = List(1)

1 item

scala> Observable.from(ListInt).reduce( + ).toBlocking.toList res38: List[Int] = List(0)

0 items

scala> Observable.from(List[Int]()).reduce( + ).toBlocking.toList java.util.NoSuchElementException: Sequence contains no elements at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted(OperatorSingle.java:116) at rx.internal.operators.NotificationLite.accept(NotificationLite.java:140) at rx.internal.operators.TakeLastQueueProducer.emit(TakeLastQueueProducer.java:100) at rx.internal.operators.TakeLastQueueProducer.startEmitting(TakeLastQueueProducer.java:45) at rx.internal.operators.OperatorTakeLast$1.onCompleted(OperatorTakeLast.java:59) at rx.internal.operators.OperatorScan$2.onCompleted(OperatorScan.java:120) at rx.internal.operators.OnSubscribeFromIterable$IterableProducer.request(OnSubscribeFromIterable.java:79) at rx.internal.operators.OperatorScan$2$1.request(OperatorScan.java:148) at rx.Subscriber.setProducer(Subscriber.java:177) at rx.internal.operators.OperatorScan$2.setProducer(OperatorScan.java:138) at rx.internal.operators.OnSubscribeFromIterable.call(OnSubscribeFromIterable.java:47) at rx.internal.operators.OnSubscribeFromIterable.call(OnSubscribeFromIterable.java:33) at rx.Observable$1.call(Observable.java:144) at rx.Observable$1.call(Observable.java:136) at rx.Observable$1.call(Observable.java:144) at rx.Observable$1.call(Observable.java:136) at rx.Observable$1.call(Observable.java:144) at rx.Observable$1.call(Observable.java:136) at rx.Observable$1.call(Observable.java:144) at rx.Observable$1.call(Observable.java:136) at rx.Observable.subscribe(Observable.java:7585) at rx.internal.operators.BlockingOperatorToIterator.toIterator(BlockingOperatorToIterator.java:53) at rx.observables.BlockingObservable.getIterator(BlockingObservable.java:156) at rx.observables.BlockingObservable$2.iterator(BlockingObservable.java:425) at scala.collection.convert.Wrappers$JIterableWrapper.iterator(Wrappers.scala:54) at scala.collection.IterableLike$class.foreach(IterableLike.scala:72) at scala.collection.AbstractIterable.foreach(Iterable.scala:54) at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59) at scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:183) at scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:45) at scala.collection.TraversableLike$class.to(TraversableLike.scala:635) at scala.collection.AbstractTraversable.to(Traversable.scala:104) at scala.collection.TraversableOnce$class.toList(TraversableOnce.scala:279) at scala.collection.AbstractTraversable.toList(Traversable.scala:104) at rx.lang.scala.observables.BlockingObservable$.toList$extension(BlockingObservable.scala:233) ... 30 elided

headinthebox commented 9 years ago

Did you try reduce on a normal empty list? You cannot reduce an empty collection.

bpitman commented 9 years ago

Oh - I think I understand. Would it make sense to add an option to reduce() to specify a default value?

scala> rx.Observable.empty[Int]().reduce(0, func2((acc: Int, i: Int) => acc + i)).toBlocking.toIterable.toList res9: List[Int] = List(0)

headinthebox commented 9 years ago

Use foldLeft.