Skip to content

Instantly share code, notes, and snippets.

@mchmielarz
Created March 14, 2019 22:43
Show Gist options
  • Save mchmielarz/d7ee4c0d7fa6b9b448eee60f11e581d8 to your computer and use it in GitHub Desktop.
Save mchmielarz/d7ee4c0d7fa6b9b448eee60f11e581d8 to your computer and use it in GitHub Desktop.
for-comprehension: Comparing combining data with List.zip and for-comprehension
// List.zip/zipWith
final Option<String> option = Option.of("value");
final Either<String, String> either = Either.right("yay");
final Iterator<Integer> integers = Iterator.tabulate(5, value -> value + 1);
final List<Tuple2<Integer, String>> zipped = integers.zip(either);
final List<Tuple3<Integer, String, String>> zipped3 = zipped
.zip(option)
  .map(e -> Tuple.of(e._1._1, e._1._2, e._2));
//the same with for-comprehension
final Iterator<Tuple3<Integer, String, String>> combined = API.For(
integers,
  either,
  option
)
  .yield(Tuple::of);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment