// Path: mug/src/main/java/com/google/mu/util/stream/BiStream.java
// public static <T> BiStream<T, T> biStream(Collection<T> elements) {
//   return from(elements, identity(), identity());
// }
// 
// Path: mug/src/main/java/com/google/mu/function/CheckedConsumer.java
// @FunctionalInterface
// public interface CheckedConsumer<T, E extends Throwable> {
//   void accept(T input) throws E;
// 
//   /**
//    * Returns a new {@code CheckedConsumer} that also passes the input to {@code that}.
//    * For example: {@code out::writeObject.andThen(logger::info).accept("message")}.
//    */
//   default CheckedConsumer<T, E> andThen(CheckedConsumer<? super T, E> that) {
//     requireNonNull(that);
//     return input -> {
//       accept(input);
//       that.accept(input);
//     };
//   }
// }


import static com.google.mu.util.stream.BiStream.biStream;
import static java.util.Objects.requireNonNull;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Queue;
import java.util.Spliterator;
import java.util.Spliterators.AbstractSpliterator;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import com.google.mu.function.CheckedConsumer;
   * The above is equivalent to manually doing:
   *
   * <pre>{@code
   *   Iterable<Foo> foos = stream::iterator;
   *   for (Foo foo : foos) {
   *     ...
   *   }
   * }</pre>
   * except using this API eliminates the need for a named variable that escapes the scope of the
   * for-each loop. And code is more readable too.
   *
   * <p>Note that {@link #iterateThrough iterateThrough()} should be preferred whenever possible
   * due to the caveats mentioned above. This method is still useful when the loop body needs to
   * use control flows such as {@code break} or {@code return}.
   */
  public static <T> Iterable<T> iterateOnce(Stream<T> stream) {
    return stream::iterator;
  }

  /**
   * Iterates through {@code stream} sequentially and passes each element to {@code consumer}
   * with exceptions propagated. For example:
   *
   * <pre>{@code
   *   void writeAll(Stream<?> stream, ObjectOutput out) throws IOException {
   *     iterateThrough(stream, out::writeObject);
   *   }
   * }</pre>
   */
  public static <T, E extends Throwable> void iterateThrough(
// Path: mug/src/main/java/com/google/mu/util/stream/BiStream.java
// public static <T> BiStream<T, T> biStream(Collection<T> elements) {
//   return from(elements, identity(), identity());
// }
// 
// Path: mug/src/main/java/com/google/mu/function/CheckedConsumer.java
// @FunctionalInterface
// public interface CheckedConsumer<T, E extends Throwable> {
//   void accept(T input) throws E;
// 
//   /**
//    * Returns a new {@code CheckedConsumer} that also passes the input to {@code that}.
//    * For example: {@code out::writeObject.andThen(logger::info).accept("message")}.
//    */
//   default CheckedConsumer<T, E> andThen(CheckedConsumer<? super T, E> that) {
//     requireNonNull(that);
//     return input -> {
//       accept(input);
//       that.accept(input);
//     };
//   }
// }


// Path: mug/src/main/java/com/google/mu/util/stream/MoreStreams.java
import static com.google.mu.util.stream.BiStream.biStream;
import static java.util.Objects.requireNonNull;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Queue;
import java.util.Spliterator;
import java.util.Spliterators.AbstractSpliterator;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import com.google.mu.function.CheckedConsumer;

   * The above is equivalent to manually doing:
   *
   * <pre>{@code
   *   Iterable<Foo> foos = stream::iterator;
   *   for (Foo foo : foos) {
   *     ...
   *   }
   * }</pre>
   * except using this API eliminates the need for a named variable that escapes the scope of the
   * for-each loop. And code is more readable too.
   *
   * <p>Note that {@link #iterateThrough iterateThrough()} should be preferred whenever possible
   * due to the caveats mentioned above. This method is still useful when the loop body needs to
   * use control flows such as {@code break} or {@code return}.
   */
  public static <T> Iterable<T> iterateOnce(Stream<T> stream) {
    return stream::iterator;
  }

  /**
   * Iterates through {@code stream} sequentially and passes each element to {@code consumer}
   * with exceptions propagated. For example:
   *
   * <pre>{@code
   *   void writeAll(Stream<?> stream, ObjectOutput out) throws IOException {
   *     iterateThrough(stream, out::writeObject);
   *   }
   * }</pre>
   */
  public static <T, E extends Throwable> void iterateThrough(
      Stream<? extends T> stream, CheckedConsumer<? super T, E> consumer) throws E {