public static<T> AnyM<T> anyM(Stream<T> monad){ return AsAnyM.anyM(monad); } public static<T> SequenceM<T> sequenceM(Stream<T> monad){
public static <T> List<AnyM<T>> collectionToAnyMList(Collection<Collection<T>> anyM){ return anyM.stream().map(i-> AsAnyM.anyM(i)).collect(Collectors.toList()); } public static <T> List<AnyM<T>> iteratorToAnyMList(Collection<Iterator<T>> anyM){
public static <T> List<AnyM<T>> completableFutureToAnyMList(Collection<CompletableFuture<T>> anyM){ return anyM.stream().map(i-> AsAnyM.anyM(i)).collect(Collectors.toList()); } public static <T> List<AnyM<T>> iterableToAnyMList(Collection<Iterable<T>> anyM){
public static <T> List<AnyM<T>> optionalToAnyMList(Collection<Optional<T>> anyM){ return anyM.stream().map(i-> AsAnyM.anyM(i)).collect(Collectors.toList()); }
public static <T> List<AnyM<T>> iteratorToAnyMList(Collection<Iterator<T>> anyM){ return anyM.stream().map(i-> AsAnyM.anyM(i)).collect(Collectors.toList()); }
public static <T> List<AnyM<T>> streamToAnyMList(Collection<Stream<T>> anyM){ return anyM.stream().map(i-> AsAnyM.anyM(i)).collect(Collectors.toList()); }
public static <T> List<AnyM<T>> streamableToAnyMList(Collection<Streamable<T>> anyM){ return anyM.stream().map(i-> AsAnyM.anyM(i)).collect(Collectors.toList()); }
public static<T> SequenceM<T> sequenceM(Stream<T> monad){ return AsAnyM.anyM(monad).asSequence(); }
/** * <pre> * {@code * Stream<String> helloWorld = Stream.of(); Optional<HeadAndTail<String>> headAndTail = StreamUtils.headAndTailOptional(helloWorld); assertTrue(!headAndTail.isPresent()); * } * </pre> * @param stream to extract head and tail from * @return */ public final static <T> Optional<HeadAndTail<T>> headAndTailOptional(Stream<T> stream){ Iterator<T> it = stream.iterator(); if(!it.hasNext()) return Optional.empty(); return Optional.of(new HeadAndTail(it.next(),AsAnyM.anyM(stream(it)).asSequence())); }
/** * extract head and tail together * * <pre> * {@code * Stream<String> helloWorld = Stream.of("hello","world","last"); HeadAndTail<String> headAndTail = StreamUtils.headAndTail(helloWorld); String head = headAndTail.head(); assertThat(head,equalTo("hello")); SequenceM<String> tail = headAndTail.tail(); assertThat(tail.headAndTail().head(),equalTo("world")); * } * </pre> * * @return */ public final static <T> HeadAndTail<T> headAndTail(Stream<T> stream){ Iterator<T> it = stream.iterator(); return new HeadAndTail(it.next(),AsAnyM.anyM(stream(it)).asSequence()); } /**
public final static <T,R> Stream<R> flatMapAnyM(Stream<T> stream,Function<? super T,AnyM<? extends R>> fn) { return AsAnyM.anyM(stream).asSequence().flatMapAnyM(fn).stream(); }
/** * cross type flatMap, removes null entries * <pre> * {@code * assertThat(StreamUtils.flatMapOptional(Stream.of(1,2,3,null), * Optional::ofNullable) * .collect(Collectors.toList()), * equalTo(Arrays.asList(1,2,3))); * } * </pre> */ public final static <T,R> Stream<R> flatMapOptional(Stream<T> stream,Function<? super T,Optional<? extends R>> fn) { return AsAnyM.anyM(stream).asSequence().flatMapOptional(fn).stream(); } /**
/** *<pre> * {@code * assertThat(StreamUtils.flatMapCompletableFuture(Stream.of(1,2,3), * i->CompletableFuture.completedFuture(i+2)) * .collect(Collectors.toList()), * equalTo(Arrays.asList(3,4,5))); * } *</pre> */ public final static <T,R> Stream<R> flatMapCompletableFuture(Stream<T> stream,Function<? super T,CompletableFuture<? extends R>> fn) { return AsAnyM.anyM(stream).asSequence().flatMapCompletableFuture(fn).stream(); } /**
/** * <pre> * {@code * assertThat(StreamUtils.flatMapLazySeq(Stream.of(1,2,3), * i->LazySeq.of(i+2)) * .collect(Collectors.toList()), * equalTo(Arrays.asList(3,4,5))); * } * </pre> */ public final static <T,R> Stream<R> flatMapLazySeq(Stream<T> stream,Function<? super T,LazySeq<? extends R>> fn) { return AsAnyM.anyM(stream).asSequence().flatMapLazySeq(fn).stream(); }
/** * flatMap operation that allows a Collection to be returned * <pre> * {@code * assertThat(StreamUtils.flatMapCollection(Stream.of(20),i->Arrays.asList(1,2,i)) * .collect(Collectors.toList()), * equalTo(Arrays.asList(1,2,20))); * } * </pre> * */ public final static <T,R> Stream<R> flatMapCollection(Stream<T> stream,Function<? super T,Collection<? extends R>> fn) { return AsAnyM.anyM(stream).asSequence().flatMapCollection(fn).stream(); } /**
/** * <pre> * {@code * assertThat(StreamUtils.flatMapStream(Stream.of(1,2,3), * i->Stream.of(i)).collect(Collectors.toList()), * equalTo(Arrays.asList(1,2,3))); * * } * </pre> * */ public final static <T,R> Stream<R> flatMapStream(Stream<T> stream,Function<? super T,BaseStream<? extends R,?>> fn) { return AsAnyM.anyM(stream).asSequence().flatMapStream(fn).stream(); } /**
/** * flatMap operation * <pre> * {@code * assertThat(StreamUtils.flatMapSequenceM(Stream.of(1,2,3), * i->SequenceM.of(i+2)).collect(Collectors.toList()), * equalTo(Arrays.asList(3,4,5))); * } * </pre> * @param fn * @return */ public final static <T,R> Stream<R> flatMapSequenceM(Stream<T> stream,Function<? super T,SequenceM<? extends R>> fn) { return AsAnyM.anyM(stream).asSequence().flatMap(fn).stream(); } public final static <T,R> Stream<R> flatMapAnyM(Stream<T> stream,Function<? super T,AnyM<? extends R>> fn) {
/** * Perform a flatMap operation where the result will be a flattened stream of Characters * from the CharSequence returned by the supplied function. * * <pre> * {@code * List<Character> result = StreamUtils.liftAndBindCharSequence(Stream.of("input.file"), .i->"hello world") .toList(); assertThat(result,equalTo(Arrays.asList('h','e','l','l','o',' ','w','o','r','l','d'))); * } * </pre> * * @param fn * @return */ public final static <T> Stream<Character> liftAndBindCharSequence(Stream<T> stream,Function<? super T,CharSequence> fn) { return AsAnyM.anyM(stream).asSequence().liftAndBindCharSequence(fn).stream(); } /**
/** * Perform a flatMap operation where the result will be a flattened stream of Strings * from the text loaded from the supplied URLs * * <pre> * {@code * List<String> result = StreamUtils.liftAndBindURL(Stream.of("input.file") ,getClass().getClassLoader()::getResource) .collect(Collectors.toList(); assertThat(result,equalTo(Arrays.asList("hello","world"))); * * } * </pre> * * @param fn * @return */ public final static <T> Stream<String> liftAndBindURL(Stream<T> stream,Function<? super T, URL> fn) { return AsAnyM.anyM(stream).asSequence().liftAndBindURL(fn).stream(); } /**
/** * Perform a flatMap operation where the result will be a flattened stream of Strings * from the text loaded from the supplied BufferedReaders * * <pre> * List<String> result = StreamUtils.liftAndBindBufferedReader(Stream.of("input.file") .map(getClass().getClassLoader()::getResourceAsStream) .map(InputStreamReader::new) ,BufferedReader::new) .collect(Collectors.toList(); assertThat(result,equalTo(Arrays.asList("hello","world"))); * * </pre> * * * @param fn * @return */ public final static <T> Stream<String> liftAndBindBufferedReader(Stream<T> stream,Function<? super T,BufferedReader> fn) { return AsAnyM.anyM(stream).asSequence().liftAndBindBufferedReader(fn).stream(); }