CombineCommunity / CombineExt

CombineExt provides a collection of operators, publishers and utilities for Combine, that are not provided by Apple themselves, but are common in other Reactive Frameworks and standards.
https://combine.community
MIT License
1.72k stars 151 forks source link

‘Replace’ Operator #125

Closed danhalliday closed 2 years ago

danhalliday commented 2 years ago

Just came across this library. Many thanks for all the contributions it’s super useful!

I’m looking for info on what I’ve always called 'replace'. I have some version of the following in a bunch of projects:

extension Publisher {
    public func replace<T>(_ transform: @autoclosure @escaping () -> T) -> Publishers.Map<Self, T> {
        map { _ in transform() }
    }
}

All it is is a map where you don’t have to manually ignore the transform argument. But it cleans things up just a little in a bunch of places, especially with the @autoclosure allowing a constant value to be passed in. Eg:

// Before

Navigation.Show
    .subscribe()
    .filter(\.destination.isModal)
    .map { _ in Sidebar.Hide(delay: 0.25) }

// After

Navigation.Show
    .subscribe()
    .filter(\.destination.isModal)
    .replace(Sidebar.Hide(delay: 0.25))

Is there any precedent for this in other reactive libraries? Or is it pointless sugar?

freak4pc commented 2 years ago

Hey @danhalliday, thanks for the suggestion :) In RxSwiftExt, for example, we usually call this map<T>(to: T), and then you can do map(to: Sidebar.Hide(delay: 0.25)). Is this what you're looking for, more or less?

danhalliday commented 2 years ago

Oh I totally overlooked that one. That’s great It’s a nicer spelling and good to know if I’m using it that others may be familiar with it from RX.

Is this project open to PRs for new operator suggestions? I don’t know whether others would find a trivial one like this useful but for me it’s quite nice for the Redux action use cases like the above.

freak4pc commented 2 years ago

Sure, happy to take a PR for this. It’s trivial but also a good QOL operator IMO.

Thanks! On 8 May 2022, 0:41 +0300, Dan Halliday @.***>, wrote:

Oh I totally overlooked that one. That’s great It’s a nicer spelling and good to know if I’m using it that others may be familiar with it from RX. Is this project open to PRs for new operator suggestions? I don’t know whether others would find a trivial one like this useful but for me it’s quite nice for the Redux action use cases like the above. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>