Stream
public enum Stream
A namespace for types and convenience methods related to streams.
For details on streams, see StreamProtocol
.
-
Declaration
Swift
public enum ReplayStrategy
-
Creates a stream that yields no elements and never completes.
var s = Stream.never(outputType: Void.self) s.next() // Will block forever
Declaration
Swift
@inlinable public static func never<T>(outputType _: T.Type = T.self) -> Stream._Private.Never<T>
Return Value
some StreamProtocol<Output == T>
-
Creates a stream that yields no elements and immediately completes.
var s = Stream.empty(outputType: Void.self) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func empty<T>(outputType _: T.Type = T.self) -> Stream._Private.Empty<T>
Return Value
some StreamProtocol<Output == T>
-
Creates a stream that yields zero or one element, depending on whether the provided optional is
nil
, and completes.let someInt: Int? = 42 var s = Stream.optional(someInt) assert(s.next() == 42) assert(s.next() == nil) let noInt: Int? = nil var s = Stream.optional(noInt) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func optional<T>(_ value: T?) -> Stream._Private.Optional<T>
Return Value
some StreamProtocol<Output == T>
-
Creates a stream that yields the provided element and completes.
var s = Stream.just(42) assert(s.next() == 42) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func just<T>(_ element: T) -> Stream._Private.Just<T>
Return Value
some StreamProtocol<Output == T>
-
Creates a stream that repeatedly yields the provided element and never completes.
var s = Stream.repeat(42) assert(s.next() == 42) assert(s.next() == 42) assert(s.next() == 42) // ...
Declaration
Swift
@inlinable public static func `repeat`<T>(_ element: T) -> Stream._Private.Repeat<T>
Return Value
some StreamProtocol<Output == T>
-
Creates a stream the yields the elements of the provided sequence and completes.
var s = Stream.sequence(0..<3) assert(s.next() == 0) assert(s.next() == 1) assert(s.next() == 2) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func sequence<C>(_ elements: C) -> Stream._Private.Sequence<C> where C : Sequence
Return Value
some StreamProtocol<Output == C.Element>
-
Creates a stream that yields the elements of the sequence formed from
first
and the results of repeated lazy applications ofnext
.The first element yielded by the stream is always
first
, and each successive element is the result of invokingnext
with the previous element. The stream ends whennext
returnsnil
. Ifnext
never returnsnil
, the stream is infinite.var s = Stream.generate(first: 1) { $0 < 3 ? $0 + 1 : nil } assert(s.next() == 1) assert(s.next() == 2) assert(s.next() == 3) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func generate<T>(first: T, _ next: @escaping (T) -> T?) -> Stream._Private.Generate<T>
Return Value
some StreamProtocol<Output == T>
-
Creates a stream that yields the elements of the sequence formed from
initial
and the results of the futures returned by repeated lazy applications ofnext
.The first element yielded by the stream is always
initial
, and each successive element is the output of the future returned by invokingnext
with the previous element. The stream ends whennext
returnsnil
. Ifnext
never returnsnil
, the sequence is infinite.var s = Stream.unfold(initial: 1) { $0 < 3 ? Future.ready($0 + 1) : nil } assert(s.next() == 1) assert(s.next() == 2) assert(s.next() == 3) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func unfold<T, U>(initial: T, _ next: @escaping (T) -> U?) -> Stream._Private.Unfold<U> where T == U.FutureType.Output
Return Value
some StreamProtocol<Output == T>
-
Creates a stream that lazily invokes the given closure and yields the elements from the returned stream.
var s = Stream.lazy { Stream.sequence(0..<3) } assert(s.wait() == 0) assert(s.wait() == 1) assert(s.wait() == 2) assert(s.wait() == nil)
Declaration
Swift
@inlinable public static func `lazy`<U>(_ body: @escaping () -> U) -> Stream._Private.Lazy<U>
Return Value
some StreamProtocol<Output == U.StreamType.Output>
-
Creates a stream that combines elements from a sequence of streams that yield elements of the same type, delivering an interleaved sequence of elements and completing when all streams complete.
This combinator can efficiently handle arbitrary numbers of streams but has more overhead than the simpler
merge(_:_:)
variants.var s = Stream.mergeAll([ Stream.sequence(0..<3), Stream.sequence(3..<6), Stream.sequence(6..<9), ]) assert(s.next() == 0) assert(s.next() == 3) assert(s.next() == 6) assert(s.next() == 1) assert(s.next() == 4) assert(s.next() == 7) assert(s.next() == 2) assert(s.next() == 5) assert(s.next() == 8) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func mergeAll<C>(_ streams: C) -> Stream._Private.MergeAll<C.Element.StreamType> where C : Sequence, C.Element : StreamConvertible
Return Value
some StreamProtocol<Output == C.Element.StreamType.Output>
-
Declaration
Swift
@inlinable public static func mergeAll<S>(_ streams: S...) -> Stream._Private.MergeAll<S> where S : StreamProtocol
Return Value
some StreamProtocol<Output == S.Output>
-
Creates a stream that combines elements from two streams that yield elements of the same type, delivering an interleaved sequence of elements and completing when both streams complete.
let a = Stream.sequence(0..<3) let b = Stream.sequence(3..<6) var s = Stream.merge(a, b) assert(s.next() == 0) assert(s.next() == 3) assert(s.next() == 1) assert(s.next() == 4) assert(s.next() == 2) assert(s.next() == 5) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func merge<A, B>(_ a: A, _ b: B) -> Stream._Private.Merge<A, B> where A : StreamProtocol, B : StreamProtocol, A.Output == B.Output
Return Value
some StreamProtocol<Output == A.Output>
-
Creates a stream that combines elements from three streams that yield elements of the same type, delivering an interleaved sequence of elements and completing when all streams complete.
let a = Stream.sequence(0..<3) let b = Stream.sequence(3..<6) let c = Stream.sequence(6..<9) var s = Stream.merge(a, b, c) assert(s.next() == 0) assert(s.next() == 3) assert(s.next() == 6) assert(s.next() == 1) assert(s.next() == 4) assert(s.next() == 7) assert(s.next() == 2) assert(s.next() == 5) assert(s.next() == 8) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func merge<A, B, C>(_ a: A, _ b: B, _ c: C) -> Stream._Private.Merge3<A, B, C> where A : StreamProtocol, B : StreamProtocol, C : StreamProtocol, A.Output == B.Output, B.Output == C.Output
Return Value
some StreamProtocol<Output == A.Output>
-
Creates a stream that combines elements from four streams that yield elements of the same type, delivering an interleaved sequence of elements and completing when all streams complete.
let a = Stream.sequence(0..<3) let b = Stream.sequence(3..<6) let c = Stream.sequence(6..<9) let d = Stream.sequence(9..<12) var s = Stream.merge(a, b, c, d) assert(s.next() == 0) assert(s.next() == 3) assert(s.next() == 6) assert(s.next() == 9) assert(s.next() == 1) assert(s.next() == 4) assert(s.next() == 7) assert(s.next() == 10) assert(s.next() == 2) assert(s.next() == 5) assert(s.next() == 8) assert(s.next() == 11) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func merge<A, B, C, D>(_ a: A, _ b: B, _ c: C, _ d: D) -> Stream._Private.Merge4<A, B, C, D> where A : StreamProtocol, B : StreamProtocol, C : StreamProtocol, D : StreamProtocol, A.Output == B.Output, B.Output == C.Output, C.Output == D.Output
Return Value
some StreamProtocol<Output == A.Output>
-
Creates a stream that combines elements from two streams by waiting until both streams have yielded an element and then yielding the oldest unconsumed element from each stream together as a tuple, completing when either of the streams completes.
let a = Stream.sequence([1, 2]) let b = Stream.sequence(["A", "B", "C"]) var s = Stream.zip(a, b) assert(s.next() == (1, "A")) assert(s.next() == (2, "B")) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func zip<A, B>(_ a: A, _ b: B) -> Stream._Private.Zip<A, B> where A : StreamProtocol, B : StreamProtocol
Return Value
some StreamProtocol<Output == (A.Output, B.Output)>
-
Creates a stream that combines elements from three streams by waiting until all streams have yielded an element and then yielding the oldest unconsumed element from each stream together as a tuple, completing when any of the streams completes.
let a = Stream.sequence([1, 2]) let b = Stream.sequence(["A", "B", "C"]) let c = Stream.sequence(["X", "Y", "Z"]) var s = Stream.zip(a, b, c) assert(s.next() == (1, "A", "X")) assert(s.next() == (2, "B", "Y")) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func zip<A, B, C>(_ a: A, _ b: B, _ c: C) -> Stream._Private.Zip3<A, B, C> where A : StreamProtocol, B : StreamProtocol, C : StreamProtocol
Return Value
some StreamProtocol<Output == (A.Output, B.Output, C.Output)>
-
Combines elements from four streams by waiting until all streams have yielded an element and then yielding the oldest unconsumed element from each stream together as a tuple, completing when any stream completes.
let a = Stream.sequence([1, 2]) let b = Stream.sequence(["A", "B", "C"]) let c = Stream.sequence(["X", "Y", "Z"]) let d = Stream.sequence(3..<6) var s = Stream.zip(a, b, c, d) assert(s.next() == (1, "A", "X", 3)) assert(s.next() == (2, "B", "Y", 4)) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func zip<A, B, C, D>(_ a: A, _ b: B, _ c: C, _ d: D) -> Stream._Private.Zip4<A, B, C, D> where A : StreamProtocol, B : StreamProtocol, C : StreamProtocol, D : StreamProtocol
Return Value
some StreamProtocol<Output == (A.Output, B.Output, C.Output, D.Output)>
-
Creates a stream that combines elements from two streams and delivers pairs of elements as tuples when either stream yields an element, completing when both streams complete.
This is the combinator typically called
combineLatest
in other frameworks.let a = Stream.sequence([1, 2]) let b = Stream.sequence(["A", "B", "C"]) var s = Stream.join(a, b) assert(s.next() == (1, "A")) assert(s.next() == (2, "B")) assert(s.next() == (2, "C")) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func join<A, B>(_ a: A, _ b: B) -> Stream._Private.Join<A, B> where A : StreamProtocol, B : StreamProtocol
Return Value
some StreamProtocol<Output == (A.Output, B.Output)>
-
Creates a stream that combines elements from three streams and delivers groups of elements as tuples when any stream yields an element, completing when all of the streams complete.
This is the combinator typically called
combineLatest
in other frameworks.let a = Stream.sequence([1, 2]) let b = Stream.sequence(["A", "B", "C"]) let c = Stream.sequence(["X", "Y", "Z"]) var s = Stream.join(a, b, c) assert(s.next() == (1, "A", "X")) assert(s.next() == (2, "B", "Y")) assert(s.next() == (2, "C", "Z")) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func join<A, B, C>(_ a: A, _ b: B, _ c: C) -> Stream._Private.Join3<A, B, C> where A : StreamProtocol, B : StreamProtocol, C : StreamProtocol
Return Value
some StreamProtocol<Output == (A.Output, B.Output, C.Output)>
-
Creates a stream that combines elements from four streams and delivers groups of elements as tuples when any stream yields an element, completing when all of the streams complete.
This is the combinator typically called
combineLatest
in other frameworks.let a = Stream.sequence([1, 2]) let b = Stream.sequence(["A", "B", "C"]) let c = Stream.sequence(["X", "Y", "Z"]) let d = Stream.sequence(3..<6) var s = Stream.join(a, b, c, d) assert(s.next() == (1, "A", "X", 3)) assert(s.next() == (2, "B", "Y", 4)) assert(s.next() == (2, "C", "Z", 5)) assert(s.next() == nil)
Declaration
Swift
@inlinable public static func join<A, B, C, D>(_ a: A, _ b: B, _ c: C, _ d: D) -> Stream._Private.Join4<A, B, C, D> where A : StreamProtocol, B : StreamProtocol, C : StreamProtocol, D : StreamProtocol
Return Value
some StreamProtocol<Output == (A.Output, B.Output, C.Output, D.Output)>