These are chat archives for ReactiveX/RxJava

29th
Oct 2015
johnfoconnor
@johnfoconnor
Oct 29 2015 18:04
Im trying to figure out how to represent the following nested for loops in Rx constructs. Having trouble getting something elegant. What am i missing? here's the pseudo code http://pastebin.com/raw.php?i=G0k9ZR4N
Simon Baslé
@simonbasle
Oct 29 2015 18:12
@johnfoconnor use flatMap and Observable.range
I can give you an example in Java
private static int getLevelFor(String s) {
        return s.length();
    }

    public void forFor() {
        List<String> uids = Arrays.asList("A", "BB", "CCCCCC");

        Observable.from(uids)
                .flatMap(new Func1<String, Observable<String>>() {
                    @Override
                    public Observable<String> call(final String s) {
                        return Observable.range(1, getLevelFor(s))
                                .map(new Func1<Integer, String>() {
                                    @Override
                                    public String call(Integer integer) {
                                        return s + "," + integer;
                                    }
                                });
                    }
                })
        .toBlocking()
        .forEach(new Action1<Object>() {
            @Override
            public void call(Object o) {
                System.out.println(o);
            }
        });
}
outputs:
A,1
BB,1
BB,2
CCCCCC,1
CCCCCC,2
CCCCCC,3
CCCCCC,4
CCCCCC,5
CCCCCC,6
(of course String would have to be replaced by a pair or you actual domain object)
(and the toBlocking section is just here so that I can execute this in a standalone main or unit test)
johnfoconnor
@johnfoconnor
Oct 29 2015 23:48
@simonbasle thanks