@@ -365,7 +365,7 @@ public final Stream<A> removeAll(final F<A, Boolean> f) {
365365 */
366366 public static <A , B > F <B , Stream <A >> sequence_ (final Stream <F <B , A >> fs ) {
367367 return fs .foldRight ((baf , p1 ) -> Function .bind (baf , p1 ._1 (), Function .curry ((a , stream ) -> cons (a , p (stream )))), Function
368- .<B , Stream <A >>constant (Stream .<A >nil ()));
368+ .<B , Stream <A >>constant (Stream .<A >nil ()));
369369 }
370370
371371 /**
@@ -527,6 +527,36 @@ public final <B> Stream<B> sequence(final Stream<B> bs) {
527527 return bind (c );
528528 }
529529
530+ /**
531+ * Sequence through the Stream monad.
532+ *
533+ * @param io The IO stream to sequence.
534+ * @return The stream of IOs after sequencing.
535+ */
536+ public static <A > Stream <IO <A >> sequence (final IO <Stream <A >> io ) {
537+ return IOFunctions .runSafe (io ).map (a -> IOFunctions .unit (a ));
538+ }
539+
540+ /**
541+ * Sequence through the Stream monad.
542+ *
543+ * @param p The lazy stream to sequence.
544+ * @return The stream of (pre-calculated) lazy values after sequencing.
545+ */
546+ public static <A > Stream <P1 <A >> sequence (final P1 <Stream <A >> p ) {
547+ return p ._1 ().map (a -> P .p (a ));
548+ }
549+
550+ /**
551+ * Sequence through the Stream monad.
552+ *
553+ * @param o The optional stream to sequence.
554+ * @return The stream of options after sequencing.
555+ */
556+ public static <A > Stream <Option <A >> sequence (final Option <Stream <A >> o ) {
557+ return o .isNone () ? Stream .nil () : o .some ().map (a -> Option .some (a ));
558+ }
559+
530560 /**
531561 * Performs function application within a stream (applicative functor pattern).
532562 *
@@ -1082,6 +1112,28 @@ public Stream<A> _1() {
10821112 Stream .<A >nil ();
10831113 }
10841114
1115+ /**
1116+ * Traversable instance of Stream for IO.
1117+ *
1118+ * @return traversed value
1119+ */
1120+ public final <B > IO <Stream <B >> traverseIO (F <A , IO <B >> f ) {
1121+ return this .foldRight1 ((a , acc ) ->
1122+ IOFunctions .bind (acc , (Stream <B > bs ) ->
1123+ IOFunctions .map (f .f (a ), b ->
1124+ bs .cons (b ))), IOFunctions .unit (Stream .<B >nil ()));
1125+
1126+ }
1127+
1128+ /**
1129+ * Traversable instance of Stream for Option.
1130+ *
1131+ * @return traversed value
1132+ */
1133+ public final <B > Option <Stream <B >> traverseOption (F <A , Option <B >> f ) {
1134+ return this .foldRight1 ((a , acc ) -> acc .bind (bs -> f .f (a ).map (b -> bs .cons (b ))), some (Stream .<B >nil ()));
1135+ }
1136+
10851137 /**
10861138 * Removes elements from the head of this stream that do not match the given predicate function
10871139 * until an element is found that does match or the stream is exhausted.
0 commit comments