apple / swift-async-algorithms

Async Algorithms for Swift
Apache License 2.0
3.06k stars 151 forks source link

Equivalent to Combine's prepend operator #323

Open Mika5652 opened 4 months ago

Mika5652 commented 4 months ago

Hi there, I was looking for some equivalent to Combine's prepend operator but I didn't find anything. Is there any plan to introduce this feature into the structured concurrency world? Thanks!

pyrtsa commented 4 months ago

You could use the chain(_:_:) free function for this, in fact see the first example on the documentation page where a preamble of 3 elements is prepended to another async sequence.

Mika5652 commented 4 months ago

@pyrtsa Seems like this chain(_:_:) replaces the prepend operator only partly, because prepend in Combine can be used in any part of the chain.

For instance:

struct Foo {
  let bar: String
}

let subject = PassthroughSubject<Foo, Never>()

subject
  .prepend(Foo(bar: "bar"))
  .map(\.bar)
  .prepend("foo")
...

I don't know if this is possible with the chain, but I will try it and maybe it fits my needs.

Thanks for the fast response anyway!