Generic Instance Method

prepend(_:)

Prefixes a publisher’s output with the specified sequence.

Declaration

func prepend<S>(_ elements: S) -> Publishers.Concatenate<Publishers.Sequence<S, Self.Failure>, Self> where S : Sequence, Self.Output == S.Element

Parameters

elements

A sequence of elements to publish before this publisher’s elements.

Return Value

A publisher that prefixes the sequence of elements prior to this publisher’s elements.

Discussion

Use prepend(_:) to publish values from two publishers when you need to prepend one publisher’s elements to another.

In this example the prepend(_:) operator publishes the provided sequence before republishing all elements from dataElements:

let prefixValues = [0, 1, 255]
let dataElements = (0...10)
cancellable = dataElements.publisher
    .prepend(prefixValues)
    .sink { print("\($0)", terminator: " ") }

// Prints: "0 1 255 0 1 2 3 4 5 6 7 8 9 10"

See Also

Applying Sequence Operations to Elements

func drop<P>(untilOutputFrom: P) -> Publishers.DropUntilOutput<Self, P>

Ignores elements from the upstream publisher until it receives an element from a second publisher.

func dropFirst(Int) -> Publishers.Drop<Self>

Omits the specified number of elements before republishing subsequent elements.

func drop(while: (Self.Output) -> Bool) -> Publishers.DropWhile<Self>

Omits elements from the upstream publisher until a given closure returns false, before republishing all remaining elements.

func tryDrop(while: (Self.Output) -> Bool) -> Publishers.TryDropWhile<Self>

Omits elements from the upstream publisher until an error-throwing closure returns false, before republishing all remaining elements.

func append<S>(S) -> Publishers.Concatenate<Self, Publishers.Sequence<S, Self.Failure>>

Appends a publisher’s output with the specified sequence.

func append<P>(P) -> Publishers.Concatenate<Self, P>

Appends the output of this publisher with the elements emitted by the given publisher.

func prepend<P>(P) -> Publishers.Concatenate<P, Self>

Prefixes the output of this publisher with the elements emitted by the given publisher.

func prefix(Int) -> Publishers.Output<Self>

Republishes elements up to the specified maximum count.

func prefix(while: (Self.Output) -> Bool) -> Publishers.PrefixWhile<Self>

Republishes elements while a predicate closure indicates publishing should continue.

func tryPrefix(while: (Self.Output) -> Bool) -> Publishers.TryPrefixWhile<Self>

Republishes elements while an error-throwing predicate closure indicates publishing should continue.

func prefix<P>(untilOutputFrom: P) -> Publishers.PrefixUntilOutput<Self, P>

Republishes elements until another publisher emits an element.