Skip to content

Instantly share code, notes, and snippets.

@josejuan
Created June 29, 2018 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josejuan/e62154faec792b3d5f1c67bb75e17f9d to your computer and use it in GitHub Desktop.
Save josejuan/e62154faec792b3d5f1c67bb75e17f9d to your computer and use it in GitHub Desktop.
package foo.bar;
import static com.htravel.fp.Parsers.maybeInt;
import static java.util.stream.Collectors.toList;
import com.htravel.fp.Either;
import java.util.stream.IntStream;
import rx.Observable;
public class Observar {
public static void main(String... args) {
makeMake("7")
.subscribe(x -> x.then(
e -> eco("Error: %s, ", e),
o -> o.subscribe(z -> eco("Success: %s, ", z))
));
}
private static void eco(String format, Object... args) {
System.err.printf(format, args);
System.err.flush();
}
private static Either<String, Observable<Integer>> make(String configuration) {
return maybeInt(configuration)
.guard(n -> n > 0 && n < 10, n -> String.format("bad '%s' configuration", n))
.map(n -> IntStream.range(0, n).boxed().collect(toList()))
.map(Observable::from);
}
private static Observable<Either<String, Observable<Integer>>> makeMake(String configuration) {
return make(configuration)
.map(o -> o.map(z -> make(Integer.toString(2 * z))))
.left2right(x -> Observable.empty()); // <<<< when Left then Right using Empty
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment