Frage

Is it possible to get Cartesian product of two Observables in rxjava?

Something like this:

A -> 1,2,3
B -> a,b
A x B -> (1, a), (1, b), (2, a), (2, b), (3, a), (3, b)
War es hilfreich?

Lösung

What you want is to produce, for one number, as pair as you have letters. So you can easily achive this using map and flatMap operator.

    Observable<Integer> number = Observable.from(Arrays.asList(1, 2, 3));
    Observable<String> letters = Observable.from(Arrays.asList("a", "b"));

    Observable<Pair> cartesian = number.flatMap((n) -> letters.map((l) -> new Pair(n, l)));

Andere Tipps

Is "join" not what you looking for?

    Observable<Integer> a = Observable.fromArray(1, 2, 3);
    Observable<String>  b = Observable.fromArray("a", "b");

    Observable<?> left  = Observable.never();
    Observable<?> right = Observable.never();

    Observable<Pair<Integer, String>> pairs = a.join(b, (aa)->left, (bb)->right, (ai, bi)-> Pair.of(ai, bi));

Here is pseudo for Cartesian mapping.

A  ->  Pair<A,"letter">  ->  C
B  ->  Pair<B,"number">  ->  D

Merge(C,D) -> lift(new CartesianOperator) -> Result  

The Cartesian operator would store a list of numbers, and a list of characters.

In the event that a unique character is introduced. You generate responses for every combination of the unique character with your list of recorded numbers. Then you add your unique character to the charter-list, then repeat the process.

You do the same for unique numbers.

In the end you'll be able to send Something like 1,1,2,3 and a,b,b, and receive something like (a,1)(a,2)(a,3)(b,1)(b,2)(b,3)

Edit:

So here is a quick, and admittedly un-scalable, implementation that would work. It takes a pair object.

Where Value 1 is your 'a', 'b', '1', '2', or '3', and Value 2 is your type'('number' or 'character')

It returns and observable pair of characters mapped to numbers.

public class CartesianOperator implements Observable.Operator<Pair<String,String>,Pair<String,String>> {


@Override
public Subscriber<? super Pair<String, String>> call(final Subscriber<? super     Pair<String, String>> subscriber) {
return new Subscriber<Pair<String, String>>() {
  List<String> numbers;
  List<String> characters;
  @Override
  public void onCompleted() {

  }

  @Override
  public void onError(Throwable e) {

  }

  @Override
  public void onNext(Pair<String, String> stringStringPair) {

    if(stringStringPair.second == "number")
    {
      if(numbers.size() == 0)
      {
        numbers.add(stringStringPair.first);
      }
      else
      {
        if(numbers.contains(stringStringPair.first))
        {}
        else
        {
          for(String temp: characters)
          {
            //Return Every permutation of existing characters with new number
            subscriber.onNext( new Pair<String,String>(temp,stringStringPair.first));
          }
        }

      }
    }
    else // Assume vallue is a character
    {
      if(characters.size() == 0)
      {
        characters.add(stringStringPair.first);
      }
      else
      {
        if(characters.contains(stringStringPair.first))
        {}
        else
        {
          for(String temp: numbers)
          {
            //Return Every permutation of existing numbers with new character
            subscriber.onNext( new Pair<String,String>(stringStringPair.first,temp));
          }
        }
      } 
    }
  }
};
}
}

If you have no idea what a pair object is, Its essentially an object that encapsulates two other objects.

pairObject.first  

Returns the first object of my pair

pairObject.second

Returns the second object of my pair

I think its deprecated by now.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top