All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xdean.jex.util.calc.CartesianProduct Maven / Gradle / Ivy

The newest version!
package xdean.jex.util.calc;

import java.util.Arrays;
import java.util.List;

import io.reactivex.Flowable;

public class CartesianProduct {

  public static Flowable cartesianProduct(Flowable> sources) {
    return sources.toList(). flatMapPublisher(list -> cartesian(list));
  }

  /**
   *
   * @author akarnokd@StackOverflow
   * @param sources
   * @return
   */
  public static Flowable cartesian(List> sources) {
    if (sources.size() == 0) {
      return Flowable.empty();
    }
    Flowable main = Flowable.just(new int[0]);
    for (int i = 0; i < sources.size(); i++) {
      int j = i;
      Flowable o = sources.get(i).cache();
      main = main. flatMap(v -> o.map(w -> {
        int[] arr = Arrays.copyOf(v, j + 1);
        arr[j] = w;
        return arr;
      }));
    }

    return main;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy