<commit_before>package com.bio4j.angulillos;

import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.Iterator;

public class conversions {
  
  public static <O> Stream<O> stream(Iterable<O> iterable) {

    return stream(iterable.spliterator());
  }

  public static <O> Stream<O> stream(Spliterator<O> spliterator) {

    return StreamSupport.stream(spliterator, false);
  }

  public static <O> Stream<O> stream(Iterator<O> iterator) {

    return stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED));
  }

  public static <O> Stream<O> flatten(Stream<Stream<O>> streamstream) {

    return streamstream.flatMap(x -> x);
  }

  public static <O> Optional<O> flatten(Optional<Optional<O>> optopt) {

    return optopt.flatMap(x -> x);
  }  

  public static <O> Optional<Stream<O>> any(Stream<Optional<O>> stream) {

    Stream<O> filtered = stream.filter(Optional::isPresent).map(Optional::get);

    Iterator<O> it_filtered = filtered.iterator();

    if ( it_filtered.hasNext() ) {

      return Optional.of(stream(it_filtered));

    } else {

      return Optional.empty();
    }
  }
}<commit_msg>add a option to stream method<commit_after>package com.bio4j.angulillos;

import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.Iterator;

/*
normally you'd do `import static com.bio4j.angulillos.conversions.*;`
*/
public class conversions {
  
  public static <O> Stream<O> stream(Iterable<O> iterable) {

    return stream(iterable.spliterator());
  }

  public static <O> Stream<O> stream(Spliterator<O> spliterator) {

    return StreamSupport.stream(spliterator, false);
  }

  public static <O> Stream<O> stream(Iterator<O> iterator) {

    return stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED));
  }

  public static <T> Stream<T> stream(Optional<T> opt) {

    return opt.map( Stream::of ).orElse( Stream.empty() );
  }

  public static <O> Stream<O> flatten(Stream<Stream<O>> streamstream) {

    return streamstream.flatMap(x -> x);
  }

  public static <O> Optional<O> flatten(Optional<Optional<O>> optopt) {

    return optopt.flatMap(x -> x);
  }  

  /*
  This method takes a stream of options and returns an option which is none if **all** options where none, some of a stream with the somes values otherwise.
  */
  // TODO any is not a great name; other options? sequence? thosePresent? somes?
  public static <O> Optional<Stream<O>> any(Stream<Optional<O>> stream) {

    Stream<O> filtered = stream.filter(Optional::isPresent).map(Optional::get);

    Iterator<O> it_filtered = filtered.iterator();

    if ( it_filtered.hasNext() ) {

      return Optional.of(stream(it_filtered));

    } else {

      return Optional.empty();
    }
  }
}
{'fraction_non_alphanumeric': 0.13053770713561041, 'fraction_numerical': 0.0010145417653026716, 'mean_word_length': 4.198594024604569, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '524815', 'n_tokens_mistral': 981, 'n_tokens_neox': 923, 'n_words': 222}