Skip to content

A better name of collect #63

@zsxwing

Description

@zsxwing

RxJava changed collect to Observable<R> collect(Func0<R> stateFactory, final Action2<R, ? super T> collector) in https://github.com/ReactiveX/RxJava/pull/1884/files#diff-c3bbc6e9e497930d46361b0b8140431cR3471 to fix ReactiveX/RxJava#1831

RxScala has the same issue and needs a similar operator too. ReactiveX/RxJava#1835 is a great fix for RxScala but has an ambiguity issue: ReactiveX/RxJava#1884

To solve this issue, here are 3 options:

  1. Add collect into RxScala to call RxJava's collect. However, RxScala has a same name method: def collect[R](pf: PartialFunction[T, R]): Observable[R]. It may confuse people because they are totally different.

  2. Change foldLeft to

  def foldLeft[R](initialValue: => R)(accumulator: (R, T) => R): Observable[R] = {
    val func = new rx.functions.Func0[Array[R]] {
      override def call(): Array[R] = Array(initialValue)
    }
    toScalaObservable[R](asJavaObservable.collect(func, new Action2[Array[R],T]{
      def call(t1: Array[R], t2: T): Unit = {
        t1(0) = accumulator(t1(0),t2)
      }
    }).map(_(0)))
  }
  1. Give another name for RxJava collect.

I prefer 3). Any suggestion for the new name?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions