PrePitch: Introduce `(flat)mapLatest` Algorithms

Im working on a codebase that makes extensive use of RxSwift while gradually introducing Swifts Structured Concurrency i was looking for a equivalent of flatMapLatest

I think a good example is a search-field thats performs a server call to load results.
The desired behaviour would be:

  • When i type something into the field it immediately loads results from the server
  • When i then change the text while the server-call is still in progress
  • The first server call is cancelled (or its result simply ignored) and it issues a new server call using the new input

The could i would like to write for this would


func loadResults(searchTerm: String) async throws -> [SearchResult] {.... }

let querySequence: AsyncSequence<String> = ...
let resultsSequeunce = querySequence.mapLatest { 
    try await loadResults(searchTerm: $0)
}

for results in resultsSequeunce {
  populateUI(results)
}

I think adding two algorithms like this would be a useful addition

// signatures are simplified a bit 

extension AsyncSequence {
  func mapLatest(_ transform: (Element) async -> Transformed) -> AsyncSequence<Transformed> {
    ...
  }

  func flatMapLatest(_ transform: (Element) async -> AsyncSequence<Transformed>) -> AsyncSequence<Transformed> {
    ...
  }
}

What do you think?

2 Likes

Hi

I agree with the need.

You can give a look at an implementation here. I intend to write a proposal at some point to integrate it in this repo (it might need some adjustments code wise though regarding Sendability)

In general, +1 on this. It is very much needed. Right now the focus is on stabilising the current algorithms to a 1.0 release and this should be considered right after IMO.

1 Like