Skip to content

Instantly share code, notes, and snippets.

@dmolesUC
Created January 25, 2018 20:13
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 dmolesUC/06088562b75fdc9b3f35ffca452cf8b8 to your computer and use it in GitHub Desktop.
Save dmolesUC/06088562b75fdc9b3f35ffca452cf8b8 to your computer and use it in GitHub Desktop.
foldLeft(), Stream.reduce(), and Observable.reduce()

Vavr List:

List<String> str = List.of("A", "B", "C");
HashSet<String> res = str.foldLeft(
  HashSet.empty(), 
  (set, s) -> set.add(s)
);

Java Stream:

Stream<String> str = Stream.of("A", "B", "C");
HashSet<String> res = str.reduce(
  HashSet.empty(),
  (set, s) -> set.add(s),
  (set1, set2) -> set1.addAll(set2)
);

RxJava2 Observable:

Observable<String> str = Observable.just("A", "B", "C");
Single<HashSet<String>> res = str.reduce(
  HashSet.<String>empty(),
  (set, s) -> set.add(s)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment